Skip to content

Instantly share code, notes, and snippets.

@kisildev
Created June 27, 2019 12:03
Show Gist options
  • Save kisildev/ea1cd1579f105c2d80777364e1047066 to your computer and use it in GitHub Desktop.
Save kisildev/ea1cd1579f105c2d80777364e1047066 to your computer and use it in GitHub Desktop.
Filtration and load more btn
// Technologies filtration
var $container = $('.js-technologies-grid');
var $termsBox = $('.js-technologies-terms');
var $termsNav = $('.js-technologies-term');
var $allTermItems = $('.js-technologies-item');
var $loadMoreBtn = $('.js-load-more');
$termsBox.on('click', function(e) {
var $target = $(e.target);
if ($target.hasClass('js-technologies-term')) {
// Get term name
var curretItem = $target.data('nav-term');
// Get current elems to load
var $itemsToLoad = $('[data-term="'+$target.data('nav-term')+'"]');
// Highlighting nav
$termsNav.removeClass('active');
$target.addClass('active');
// Displaing elems func
function displayItems($items) {
var allItemCount = $items.length;
$allTermItems.hide();
$items.each(function(index, el) {
if (index > 6) { return false; }
$(el).show();
});
if(allItemCount > 6) {
$loadMoreBtn.show();
} else {
$loadMoreBtn.hide();
}
}
// Displaing elems
if (curretItem == 'all') {
displayItems($allTermItems);
} else {
displayItems($itemsToLoad);
}
}
});
// Load more
if ($('.js-load-more').length) {
$loadMoreBtn.on('click', function(e) {
event.preventDefault();
// Get active item from nav
var activeTerm = $(this)
.closest($container)
.find('.js-technologies-term.active')
.data('nav-term');
// Loading current elems
if (activeTerm == 'all') {
$allTermItems.show();
$(this).hide();
} else {
$('[data-term="'+activeTerm+'"]').show();
$(this).hide();
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment