jquery同位素上的多个过滤器

时间:2018-05-25 20:43:40

标签: javascript php jquery html isotope

我正在尝试选择多个过滤器并将结果显示为这样。然后,当他们点击显示全部时,它将删除所有过滤器。我正在使用jquery同位素插件。

见这里:https://convention.test.yja.org/sessions

我希望有人能够选择年龄组和类别(可能还有多个年龄组和多个类别)

这是过滤器代码:

 <ul class="filter list-inline">
    <li><a class="active" href="#" data-filter="*">Show All</a></li>
    <li><a href="#" data-filter=".Education"><i class="fa fa-book" style="color:#bbbbbb"></i> Jain Education</a></li>
    <li><a href="#" data-filter=".Lifestyle"><i class="fa fa-hand-holding-heart" style="color:#bbbbbb"></i> Lifestyle</a></li>
    <li><a href="#" data-filter=".Career"><i class="fa fa-briefcase" style="color:#bbbbbb"></i> Career & Networking</a></li>
    <li><a href="#" data-filter=".DiversityInclusion"><i class="fa fa-globe" style="color:#bbbbbb"></i> Diversity & Inclusion</a></li>
    <li><a href="#" data-filter=".SocialIssues"><i class="fa fa-handshake" style="color:#bbbbbb"></i> Current Events & Social Issues</a></li>
</ul>
<ul class="filter list-inline">
    <li><a class="active" href="#" data-filter="*">Show All</a></li>
    <li><a href="#" data-filter=".HS">High School</a></li>
    <li><a href="#" data-filter=".College">College</a></li>
    <li><a href="#" data-filter=".JNF">JNF</a></li>
</ul> 

这是同位素js代码:

/*-------------------------------------------------*/
/* =  portfolio isotope
/*-------------------------------------------------*/

var winDow = $(window);
// Needed variables
var $container = $('.iso-call');
var $filter = $('.filter');

try {
    $container.imagesLoaded(function () {
        $container.trigger('resize');
        $container.isotope({
            filter: '*',
            layoutMode: 'masonry',
            animationOptions: {
                duration: 750,
                easing: 'linear'
            }
        });

        $('.triggerAnimation').waypoint(function () {
            var animation = $(this).attr('data-animate');
            $(this).css('opacity', '');
            $(this).addClass("animated " + animation);

        },
                {
                    offset: '75%',
                    triggerOnce: true
                }
        );
        setTimeout(1500);
    });
} catch (err) {
}

winDow.bind('resize', function () {
    var selector = $filter.find('a.active').attr('data-filter');

    try {
        $container.isotope({
            filter: selector,
            animationOptions: {
                duration: 750,
                easing: 'linear',
                queue: false
            }
        });
    } catch (err) {
    }
    return false;
});

// Isotope Filter 
$filter.find('a').click(function () {
    var selector = $(this).attr('data-filter');

    try {
        $container.isotope({
            filter: selector,
            animationOptions: {
                duration: 750,
                easing: 'linear',
                queue: false
            }
        });
    } catch (err) {

    }
    return false;
});


var filterItemA = $('.filter li a');

filterItemA.on('click', function () {
    var $this = $(this);
    if (!$this.hasClass('active')) {
        filterItemA.removeClass('active');
        $this.addClass('active');
    }
});

1 个答案:

答案 0 :(得分:0)

我注意到,您在同一个对象上有两个点击事件。 $filter.find('a').click(filterItemA.on('click',

第一个是艰苦的工作,第二个只是确保任何时候只点击一个按钮。

您应该将第二个更改为

  • 仅当您单击其中一个Show All按钮
  • 时,才会删除活动标记
  • 点击其他任何内容时停用Show All -

第一个到

  • 根据filterItemA.filter(".active")
  • 撰写选择器

filterItemA.off('click')
filterItemA.map( (x,i) => {
    if( $(i).attr('data-filter')=="*") {
    	$(i).addClass("showall")
    } else {
    	$(i).addClass("noshowall")
    }
})

filterItemA.on('click', function () {
    var $this = $(this);
    var selector = $this.attr('data-filter');
    var filterActive;
    var activeSelector;
    if (!$this.hasClass('active')) {
	if (selector=="*") {
	    filterItemA.filter(".noshowall").removeClass('active')
	    filterItemA.filter(".showall").addClass('active')
	} else {
	    filterItemA.filter(".showall").removeClass('active')
	    $this.addClass('active')
	}
    } else {
	if (selector=="*") {
		// select show all... what to do here?
	} else {
	    $this.removeClass('active')
	    filterActive=filterItemA.filter(".noshowall.active")
	    activeSelector = Array.join(filterActive.map( (x,i) => $(i).attr("data-filter")),'')
	    if (activeSelector== "") { 
		filterItemA.filter(".showall").addClass("active")
	    }

	}
    }
    filterActive=filterItemA.filter(".noshowall.active")
    activeSelector = Array.join(filterActive.map( (x,i) => $(i).attr("data-filter")),'')
    if (activeSelector== "") { activeSelector="*"; }
    try {
	$container.isotope({
	    filter: activeSelector,
	    animationOptions: {
		duration: 750,
		easing: 'linear',
		queue: false
	    }
	});
    } catch (err) {
    }
    return false;
});