我的目标是将setupSorting函数放在ImageLoad函数内部。 它们在分离时( 在此 )工作,但在组合时( )则排序失败。

这个:

      $(document).ready (function(){
            /* reshuffle when user clicks a filter item */
            $('.filter-tags a').click(function (e) {
                e.preventDefault();

                // set active class
                $('.filter-tags a').removeClass('active');
                $(this).addClass('active');

                // get group name from clicked item
                var groupName = $(this).attr('data-group');

                // reshuffle grid
                $grid.shuffle('shuffle', groupName );
            });  

需要深入了解以下内容:

var ImageLoad = (function( $, imagesLoaded ) {

  var $shuffle = $('.masonry-gallery'),
      $imgs = $shuffle.find('img'),
      $loader = $('#loader'),
      sizer = document.getElementById('#shuffle-sizer'),
      imgLoad,

  init = function() {

    // Create a new imagesLoaded instance
    imgLoad = new imagesLoaded( $imgs.get() );

    // Listen for when all images are done
    // will be executed even if some images fail
    imgLoad.on( 'always', onAllImagesFinished );
  },

  onAllImagesFinished = function( instance ) {

    if ( window.console && window.console.log ) {
      console.log( instance );
    }

    // Hide loader
    $loader.addClass('hidden');

    // Adds visibility: visible;
    $shuffle.addClass('images-loaded');

    // Initialize shuffle
    $shuffle.shuffle({
      sizer: sizer,
      itemSelector: '.gallery-photo'
    });
  };

  return {
    init: init
  };
}( jQuery, window.imagesLoaded ));

$(document).ready(function() {
  ImageLoad.init();
});  

最终目的是像这样的页面,只有排序: http : //vestride.github.io/Shuffle/images/

===============>>#1 票数:1 已采纳

经过几个小时的实验,我终于明白了。 没错,我没打电话。 我还忘记了合并时将$shuffle.shuffle更改$grid.shuffle $shuffle.shuffle

那仍然没有解决问题,因此我更改了setupSorting函数(以及随后的一些html),使其更接近作者的( 此处

结果代码有效!

///////////////////////  IMAGESLOADED & SHUFFLE  //////////////////////

var ImageLoad = (function( $, imagesLoaded ) {

  var $shuffle = $('.masonry-gallery'),
      $filterOptions = $('.filter-list'),
      $imgs = $shuffle.find('.gallery-photo img'),
      $loader = $('#loader'),
      sizer = document.getElementById('#shuffle-sizer'),
      imgLoad,

  init = function() {

      // None of these need to be executed synchronously
    setTimeout(function() {
      setupSorting();
    }, 10);

    // Create a new imagesLoaded instance
    imgLoad = new imagesLoaded( $imgs.get() );

    // Listen for when all images are done
    // will be executed even if some images fail
    imgLoad.on( 'always', onAllImagesFinished );
  },

  onAllImagesFinished = function( instance ) {

    if ( window.console && window.console.log ) {
      console.log( instance );
    }

    // Hide loader
    $loader.addClass('hidden');

    // Adds visibility: visible;
    $shuffle.addClass('images-loaded');

    // Initialize shuffle
    $shuffle.shuffle({
      sizer: sizer,
      itemSelector: '.gallery-photo'
    });
  },

       // Set up button clicks
  setupSorting = function() {
    var $btns = $filterOptions.children();
    $btns.on('click', function() {
      var $this = $(this),
          isActive = $this.hasClass( 'active' ),
          group = isActive ? 'all' : $this.data('group');

      // Hide current label, show current label in title
      if ( !isActive ) {
        $('.filter-list .active').removeClass('active');
      }

      $this.toggleClass('active');

      // Filter elements
      $shuffle.shuffle( 'shuffle', group );
    });

    $btns = null;
  };

  return {
    init: init
  };
}( jQuery, window.imagesLoaded ));

$(document).ready(function() {
    ImageLoad.init();
}); 

您可以在此处看到完整的小提琴。

  ask by BlueHelmet translate from so

未解决问题?本站智能推荐: