我正在尝试设置一个带有多个过滤器的流沙,以及我在下面采用的方法,我不确定它是否是最好的。
$filteredItems = $containerClone.find('.portfolio-item-marketing').filter(function(index){
if ( $filterA != 'all' && $filterB != 'all' )
{
return jQuery(this).find('.portfolio-item-marketing[data-type~=' + $filterA + ']') || jQuery(this).find('.portfolio-item-marketing[data-type~=' + $filterB + ']');
}
if ( $filterA != 'all' && $filterB == 'all' )
{
return jQuery(this+'[data-type~='+$filterA+']') ;
}
if ( $filterA == 'all' && $filterB != 'all' )
{
return jQuery(this).find('.portfolio-item-marketing[data-type~=' + $filterB + ']');
}
if ( $filterA == 'all' && $filterB == 'all' )
{
return jQuery(this).find('.portfolio-item-marketing');
}
});
正如您在代码中看到的,我尝试了两种不同的方法(首先是“if”,第二种是“if”)。我不确定我是否可以在过滤器中使用find但我也不知道如何从jQuery对象获取属性。
我无法使用
jQuery(this).attr('data-type') == $filterA
因为该属性可能包含各种过滤器。这就是我尝试使用jQuery“〜=”
的原因我应该放弃并结束使用indexOf吗?
HTML标记例如:
<ul class="filterA filters">
<li class="active"><a href="javascript:void(0)" class="all">All</a></li>
<li><a href="javascript:void(0)" class="digital-books">Digital Books</a></li>
<li><a href="javascript:void(0)" class="pdf">Pdf</a></li>
</ul>
<ul class="filterB filters">
<li class="active"><a href="javascript:void(0)" class="all">All</a></li>
<li><a href="javascript:void(0)" class="3m">3M</a></li>
<li><a href="javascript:void(0)" class="mcdonald">Mc Donald</a></li>
<li><a href="javascript:void(0)" class="sony">Sony</a></li>
</ul>
<div id="portfolio-wrap" class="clearfix">
<div class="portfolio-item-marketing" data-id="1" data-type="digital-books 3m "></div>
<div class="portfolio-item-marketing" data-id="1" data-type="pdf 3m "></div>
<div class="portfolio-item-marketing" data-id="1" data-type="digital-books sony "></div>
<div class="portfolio-item-marketing" data-id="1" data-type="pdf mcdonald "></div>
</div>
Quicksand Filter脚本:
jQuery(document).ready(function(){
if(jQuery().quicksand) {
portfolio_quicksand();
}
});
function portfolio_quicksand() {
// Setting up our variables
var $filter;
var $container;
var $containerClone;
var $filterLink;
var $filteredItems
// Set our filter
$filterA = jQuery('.filterA li.active a').attr('class');
$filterB = jQuery('.filterB li.active a').attr('class');
// Set our filter link
$filterLink = jQuery('.filters li a');
// Set our container
$container = jQuery('#portfolio-wrap');
// Clone our container
$containerClone = $container.clone();
// Apply our Quicksand to work on a click function
// for each of the filter li link elements
$filterLink.click(function(e)
{
// Remove the active class
jQuery(this).parent().parent().find('li').removeClass('active');
// Apply the 'active' class to the clicked link
jQuery(this).parent().addClass('active');
// Set our filter
$filterA = jQuery('.filterA li.active a').attr('class');
$filterB = jQuery('.filterB li.active a').attr('class');
$filteredItems = $containerClone.find('.portfolio-item-marketing').filter(function(index){
if ( $filterA != 'all' && $filterB != 'all' )
{
return jQuery(this).find('.portfolio-item-marketing[data-type~=' + $filterA + ']') || jQuery(this).find('.portfolio-item-marketing[data-type~=' + $filterB + ']');
}
if ( $filterA != 'all' && $filterB == 'all' )
{
return jQuery(this+'[data-type~='+$filterA+']') ;
}
if ( $filterA == 'all' && $filterB != 'all' )
{
return jQuery(this).find('.portfolio-item-marketing[data-type~=' + $filterB + ']');
}
if ( $filterA == 'all' && $filterB == 'all' )
{
return jQuery(this).find('.portfolio-item-marketing');
}
});
// Finally call the Quicksand function
$container.quicksand($filteredItems,
{
// The duration for the animation
duration: 750,
// The easing effect when animating
easing: 'easeInOutCirc',
// Height adjustment set to dynamic
adjustHeight: 'dynamic'
});
});
}