Skip to content

Instantly share code, notes, and snippets.

@olragon
Last active December 15, 2015 14:09
Show Gist options
  • Save olragon/5272356 to your computer and use it in GitHub Desktop.
Save olragon/5272356 to your computer and use it in GitHub Desktop.
Hide item in list if we have more than specified items.
(function($) {
$.fn.limitedList = function(options) {
var settings = $.extend({
limit: 5,
moreText: '> Xem thêm',
lessText: '< Rút gọn'
}, options);
return this.each(function() {
if($('li', this).length > settings.limit) {
$(this).find('li:gt('+ parseInt(settings.limit) - 1 +')').hide();
$link = $('<a style="position:absolute;left:20px;" href="#" class="more">'+ settings.moreText +'</a>');
$link.data('isShowing', false);
$(this).closest('ul').append($link);
$link.click(function(e) {
if($(this).data('isShowing')) {
$(this).data('isShowing', false);
$(this).closest('ul').find('li:gt('+parseInt(settings.limit) - 1+')').slideUp();
$(this).text(settings.moreText);
}
else {
$(this).data('isShowing', true);
$(this).closest('ul').find('li').show('slow');
$(this).text(settings.lessText);
}
e.preventDefault();
});
//showNext($(this));
}
});
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment