【问题标题】:Masonry callback in jQuery infinite scroll isn't working in Wordpress - and neither are infinite scroll pluginsjQuery 无限滚动中的砌体回调在 Wordpress 中不起作用 - 无限滚动插件也不是
【发布时间】:2014-08-23 02:22:22
【问题描述】:

我有很多关于在 Wordpress 中结合 Masonry 和无限滚动的问题,即on this page

我收集到让 Masonry 使用无限滚动需要在无限滚动 jQuery 代码中进行回调 - 这似乎已经确立。但是,我似乎只能在非常特定的情况下在我的 Wordpress 网站上进行无限滚动,鉴于这些情况,我不确定如何集成 Masonry。

这段代码是从 Masonry.js 网站的无限滚动演示中借来的,现在在页脚:

<script>
  $(function(){

    var $container = $('.et_pb_blog_grid_wrapper');

    $container.imagesLoaded(function(){
      $container.masonry({
        itemSelector: '.post',
        columnWidth: 100
      });
    });

    $container.infinitescroll({
      navSelector  : '#nextposts',    // selector for the paged navigation 
      nextSelector : '#nextposts a',  // selector for the NEXT link (to page 2)
      itemSelector : '.post',     // selector for all items you'll retrieve
      loading: {
          finishedMsg: 'No more pages to load.',
          img: 'http://dearjackalope.com/juliaproblems/wp-content/themes/juliamariani/images/ajax-loader.gif'
        }
      },
      // trigger Masonry as a callback
      function( newElements ) {
        // hide new items while they are loading
        var $newElems = $( newElements ).css({ opacity: 0 });
        // ensure that images load before adding to masonry layout
        $newElems.imagesLoaded(function(){
          // show elems now they're ready
          $newElems.animate({ opacity: 1 });
          $container.masonry( 'appended', $newElems, true ); 
        });
      }
    );

  });
</script>

问题是,即使它在静态 HTML 中工作,在这个页面上它也没有任何效果,我不知道为什么!这是什么工作:

<script>
    var infinite_scroll = {
        loading: {
            img: "<?php echo get_stylesheet_directory_uri(); ?>/images/ajax-loader.gif",
            msgText: "<?php _e( 'Loading the next set of posts...', 'custom' ); ?>",
            finishedMsg: "<?php _e( 'All posts loaded.', 'custom' ); ?>"
        },
        "nextSelector":"#nextposts a",
        "navSelector":"#nextposts",
        "itemSelector":".post",
        "contentSelector":".et_pb_blog_grid_wrapper"
    };
    jQuery( infinite_scroll.contentSelector ).infinitescroll( infinite_scroll );
    </script>

但我不确定如何将 Masonry 回调添加到此 - 在回调中声明变量的方式看起来与此完全不同(我知道美元符号是在 jQuery 中定义的,它出现在回调中但是不在原始代码中 - 我不确定这是否重要?),我不确定它需要在函数中的确切位置。我试过了:

<script>
var infinite_scroll = {
        loading: {
            img: "<?php echo get_stylesheet_directory_uri(); ?>/images/ajax-loader.gif",
            msgText: "<?php _e( 'Loading the next set of posts...', 'custom' ); ?>",
            finishedMsg: "<?php _e( 'All posts loaded.', 'custom' ); ?>"
        },
        "nextSelector":"#nextposts a",
        "navSelector":"#nextposts",
        "itemSelector":"article.post",
        "contentSelector":".et_pb_blog_grid_wrapper"
        },
        // hide new items while they are loading
        var $newElems = jQuery(newElements).css({ opacity: 0 });
        // ensure that images load before adding to masonry layout
        $newElems.imagesLoaded(function(){
        // show elems now they're ready
        $newElems.animate({ opacity: 1 });
        $container.masonry( 'appended', $newElems, true );
        });
    };
    jQuery( infinite_scroll.contentSelector ).infinitescroll( infinite_scroll );
</script>   

还有这个,声明一个新函数:

<script>
    var infinite_scroll = {
        loading: {
            img: "<?php echo get_stylesheet_directory_uri(); ?>/images/ajax-loader.gif",
            msgText: "<?php _e( 'Loading the next set of posts...', 'custom' ); ?>",
            finishedMsg: "<?php _e( 'All posts loaded.', 'custom' ); ?>"
        },
        "nextSelector":"#nextposts a",
        "navSelector":"#nextposts",
        "itemSelector":"article.post",
        "contentSelector":".et_pb_blog_grid_wrapper"        
    });
    };
    jQuery( infinite_scroll.contentSelector ).infinitescroll( infinite_scroll );
    function newElements()  // hide new items while they are loading
        var $newElems = jQuery(newElements).css({ opacity: 0 });
            // ensure that images load before adding to masonry layout
        $newElems.imagesLoaded(function(){
            // show elems now they're ready
        $newElems.animate({ opacity: 1 });
        $container.masonry( 'appended', $newElems, true );
    </script>

这些都不起作用,所以我不确定我应该将回调放在哪里,或者原始代码是否有某些东西意味着回调不起作用。我花了大约 8 个小时阅读 Javascript 教程和文档,但我不确定接下来要尝试什么。 :(

有一个现在不受支持但似乎仍在广泛使用的插件,名为 Infinite-Scroll,其中包含一个用于“砌体模式”的复选框,但是当我尝试安装它时,我什么也没得到 - 它似乎没有加载任何代码到页面中,所以这里似乎不是一个选项。可能值得注意的是,我还发现 Jetpack 无限滚动功能即使在为其完全设置主题后也没有将任何代码加载到页面中,因此我似乎仅限于此处的非插件代码。

我的主题是否存在根本性的问题导致所有这些问题?我的 Javascript 充其量只是初学者级别,我真的很想知道从哪里开始 - 任何帮助将不胜感激。

【问题讨论】:

    标签: javascript wordpress callback jquery-masonry infinite-scroll


    【解决方案1】:

    好的,所以看起来整个问题源于 Wordpress 不将 $ 理解为 jQuery 变量这一事实,因为其默认的 jQuery 排队版本在无冲突模式下运行。

    用 'jQuery' 替换所有的 '$' 符号可以解决问题,或者您可以将其包装在一个函数中并将其映射到 $ 就像我所做的那样 - http://digwp.com/2011/09/using-instead-of-jquery-in-wordpress/

    不知道我是怎么错过的,但如果其他人在 Wordpress 中无法无限滚动工作,那么经过数小时的困惑为什么它不起作用后,它就像一个魅力!

    【讨论】:

      【解决方案2】:

      万一这对其他人有帮助,我试图将函数添加到 infinite_scroll js 变量中而陷入困境。像这样添加它效果很好:

      var infinite_scroll = {
          loading: {
              img: "<?php echo get_template_directory_uri(); ?>/library/images/ajax-loader.gif",
              msgText: "<?php _e( 'Loading the next set of posts...', 'custom' ); ?>",
              finishedMsg: "<?php _e( 'All posts loaded.', 'custom' ); ?>"
          },
          "nextSelector":".pagination .next",
          "navSelector":".pagination",
          "itemSelector":".infinite-scroll-post",
          "contentSelector":"#infinite-scrollable",
          "debug":true
      }
      jQuery( infinite_scroll.contentSelector ).infinitescroll( infinite_scroll,function(arrayOfNewElems){
          picturefill({ reevaluate: true });
      } );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-08-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多