简体   繁体   English

具有无限滚动的masonry.js-内容正在消失

[英]masonry.js with infinite scroll - content is disappearing

I'm new to javascript/jquery and will admit I'm not sure how it all works at the moment, I've been doing a lot of googling to get this far! 我是javascript / jquery的新手,并且会承认我目前不确定它是如何工作的,我一直在做大量的搜索工作以力争做到这一点! So I have a site that is using masonry.js to display items in a pinterest type way. 所以我有一个使用masonry.js以pinterest类型方式显示项目的网站。 Now the first page loads fine and is laid out as expected. 现在,第一页可以正常加载并按预期进行布局。 When the user scrolls to the bottom of the page, my new content gets added in the right place on the screen BUT the old content is gone! 当用户滚动到页面底部时,我的新内容将添加到屏幕上的正确位置,但是旧内容消失了! The new content is added in the right place but everything before it is blank! 新内容将添加到正确的位置,但之前的所有内容均为空白! This so nearly works, I just can't see why the scroll function is appending content but removing the original! 这几乎可以正常工作,我只是看不到为什么滚动功能会添加内容但删除原始内容!

在此处输入图片说明

The code that loads the initial content is: 加载初始内容的代码是:

    <script type="text/javascript">
$(document).ready(function() {
    var $grid = $('.grid').masonry({
    // set itemSelector so .grid-sizer is not used in layout
    itemSelector: '.grid-item',
    // use element for option
    columnWidth: '.grid-sizer',
    gutter: 10
    })
});

    var track_load = 0; //total loaded record group(s)
    var loading  = false; //to prevents multiple ajax loads
    var total_groups = <?php echo $total_groups; ?>; //total record group(s)
    var leagueid = <?php echo "1";?>;
    var content = $('.grid').load("league_news1.php", {'group_no':track_load,'leagueid':leagueid}, function() {track_load++;
    $(".grid").imagesLoaded(function() {
    $(".grid").append(content).masonry("appended", content);
});


});
</script>

And then this is what I use to add more content when the user scrolls: 然后,这就是我用来在用户滚动时添加更多内容的方法:

    <script type='text/javascript'>
        $(window).scroll(function() { //detect page scroll

        if ($(window).scrollTop() >= $(document).height() - $(window).height() - 60) {

            if(track_load <= total_groups && loading==false) //theres more data to load
            {   


                loading = true; //prevent further ajax loading

                $('.animation_image').show(); //show loading image
                    //load data from the server using a HTTP POST request


                    var new_content = $('.grid').load("league_news1.php", {'group_no':track_load,'leagueid':leagueid}, function() {
                    $(".grid").imagesLoaded(function() {
                    $(".grid").append(new_content).masonry("appended",new_content);



                    //hide loading image
                    $('.animation_image').hide(); //hide loading image once data is received

                    track_load++; //loaded group increment
                    loading = false; 

                }).fail(function(xhr, ajaxOptions, thrownError) { //any errors?

                    alert(thrownError); //alert with HTTP error
                    $('.animation_image').hide(); //hide loading image
                    loading = false;

                });

            });
        }
    }
});
</script>

After speaking with the developer of the plugin (although it was my jquery that was at fault, not his excellent plugin) the answer was to wrap content in a jquery object like so: 与插件开发人员交谈后(尽管是我的jquery出了问题,而不是他出色的插件),答案是将内容包装在jquery对象中,如下所示:

$.get("league_news.php", {'group_no':track_load,'leagueid':leagueid}, function(data) {
        track_load++;
        var $content=$(data); 
        $content.imagesLoaded(function() {
        $(".grid").append($content).masonry("appended", $content);
    });


    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM