我正在尝试设置一个带有多个过滤器的流沙,以及我在下面采用的方法,我不确定它是否是最好的。
$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');
}
});
正如您在代码中看到的,我尝试了 2 种不同的方法(第一个 "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>
流沙过滤器脚本:
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'
});
});
}