我遇到了shuffle.js,这对我正在做的项目来说似乎很完美。
我所拥有的是一个复选框列表,以及一个用户应该可以点击以过滤结果的无序列表。
我见过许多使用shuffle.js的好例子,但它们看起来都很抽象。
// JavaScript Document
var shuffleme = (function( $ ) {
'use strict';
var $grid = $('#grid'), //locate what we want to sort
$filterOptions = $('.portfolio-sorting li'), //locate the filter categories
$sizer = $grid.find('.shuffle_sizer'), //sizer stores the size of the items
init = function() {
// None of these need to be executed synchronously
setTimeout(function() {
listen();
setupFilters();
}, 100);
// instantiate the plugin
$grid.shuffle({
itemSelector: '[class*="col-"]',
sizer: $sizer
});
},
// Set up button clicks
setupFilters = function() {
var $btns = $filterOptions.children();
$btns.on('click', function(e) {
e.preventDefault();
var $this = $(this),
isActive = $this.hasClass( 'active' ),
group = isActive ? 'all' : $this.data('group');
// Hide current label, show current label in title
if ( !isActive ) {
$('.portfolio-sorting li a').removeClass('active');
}
$this.toggleClass('active');
// Filter elements
$grid.shuffle( 'shuffle', group );
});
$btns = null;
},
// Re layout shuffle when images load. This is only needed
// below 768 pixels because the .picture-item height is auto and therefore
// the height of the picture-item is dependent on the image
// I recommend using imagesloaded to determine when an image is loaded
// but that doesn't support IE7
listen = function() {
var debouncedLayout = $.throttle( 300, function() {
$grid.shuffle('update');
});
// Get all images inside shuffle
$grid.find('img').each(function() {
var proxyImage;
// Image already loaded
if ( this.complete && this.naturalWidth !== undefined ) {
return;
}
// If none of the checks above matched, simulate loading on detached element.
proxyImage = new Image();
$( proxyImage ).on('load', function() {
$(this).off('load');
debouncedLayout();
});
proxyImage.src = this.src;
});
// Because this method doesn't seem to be perfect.
setTimeout(function() {
debouncedLayout();
}, 500);
};
return {
init: init
};
}( jQuery ));
$(document).ready(function()
{
shuffleme.init(); //filter portfolio
});
所以这是我在网上看过的代码示例,我一直在研究它。
如果我想使用复选框,我可以将每个复选框的值设置为我过滤的组名称吗?
有趣的是,它适用于HTML页面,但不适用于PHP。