【问题标题】:Combining jQuery code - ImagesLoaded + Shuffle.js结合 jQuery 代码 - ImagesLoaded + Shuffle.js
【发布时间】:2015-05-16 17:15:51
【问题描述】:

我的目标是将setupSorting 函数放在ImageLoad 函数中。它们在分开时工作 (here),但在组合时排序失败 (here)。

这个:

      $(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/

【问题讨论】:

  • 你永远不会调用 setupSorting... $(document).ready(function() { ImageLoad.init(); ImageLoad.setupSorting(); }); ?

标签: jquery merge shuffle imagesloaded


【解决方案1】:

经过几个小时的试验,我终于搞定了。你是对的,我没有打电话。我也忘了在合并时将$grid.shuffle 更改为$shuffle.shuffle

那仍然没有成功,所以我更改了 setupSorting 函数(以及随后的一些 html)以更接近作者的 (here)

生成的代码有效!

///////////////////////  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();
}); 

您可以看到完整的小提琴 (here)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多