?
Solved

Jquery Masonry and Infinite Overlay Error

Posted on 2012-08-13
4
Medium Priority
?
856 Views
Last Modified: 2012-08-14
I'm using infinite scroll along with jquery masonry to load more posts on my site.
However, the 1st time I scroll and load the php script, the output overlaps.

Here is my code below

<script type="text/javascript">
$(function(){

    var $container = $('#newscontainer');

   $container.masonry({
    // options
    itemSelector : '.box'
  });
function lastAddedLiveFunc()
    {
        $('div#lastPostsLoader').show();
 
        $.get("/wp-content/themes/maxtime/loadmore.php", function(data){
            if (data != "") {
                //console.log('add data..');
                $container.append(data);
                
            }
      // trigger Masonry as a callback
     
            $('div#lastPostsLoader').hide();
        });
    };
 
    //lastAddedLiveFunc();
    $(window).scroll(function(){
 
        var wintop = $(window).scrollTop(), docheight = $(document).height(), winheight = $(window).height();
        var  scrolltrigger = 0.95;
 
        if  ((wintop/(docheight-winheight)) > scrolltrigger) {
         //console.log('scroll bottom');
         lastAddedLiveFunc();
        }
    });
   
  });
</script>

Open in new window

0
Comment
Question by:m2ew
  • 3
4 Comments
 

Author Comment

by:m2ew
ID: 38289877
I managed to get this working in chrome, turns out I had to trigger masonry as a callback function with a timeout. When I just called it without timeout masonry couldn't calculate the image height fast enough.

   var $container = $('#newscontainer');
	var i = 1;
    var maxposts;
   $container.infinitescroll({
        debug           : false,
        nextSelector    : "div.navigation a:first",
        loadingImg      : "/js/ajax-loader.gif",
        navSelector     : "div.navigation",
        contentSelector : "#newscontainer",
        itemSelector    : "div.box"
        },function(newElements) {
            Cufon.refresh();
            setTimeout(function() {

                $container.masonry({
                    container: $(newElements)
                });

            }, 100);

    });
function lastAddedLiveFunc()
    {
        $('div#lastPostsLoader').show();
 		
 		//alert(i);
        $.post("/wp-content/themes/maxtime/loadmore.php?ajaxpaged="+i, function(data){
        
            if (data != "") {
                //console.log('add data..');
                
                $container.append(data);
                i=i+1;
                   $('div#lastPostsLoader').hide();
            } else{
            	if (maxposts != "END"){
            	$('div#lastPostsLoader').hide();
            	$('div#lastPostsLoader').html("<h1 class='entry-title' style='text-align:center; font-size:22px;'>Sorry, there are no more posts<br/><br/></h1>");
            	$('div#lastPostsLoader').fadeIn();
            		maxposts = "END";
            	}
            }
      // trigger Masonry as a callback
     
         
        });
    };
 
    //lastAddedLiveFunc();
    $(window).scroll(function(){
 
        var wintop = $(window).scrollTop(), docheight = $(document).height(), winheight = $(window).height();
        var  scrolltrigger = 0.95;
 
        if  ((wintop/(docheight-winheight)) > scrolltrigger) {
         //console.log('scroll bottom');
         lastAddedLiveFunc();
        }
    });

Open in new window

0
 

Author Comment

by:m2ew
ID: 38289882
0
 
LVL 60

Accepted Solution

by:
Julian Hansen earned 980 total points
ID: 38290881
Did you look at the docs here

http://masonry.desandro.com/docs/intro.html

There is somethinger there about using the imagesLoaded plugin which would seem to address your problem?
0
 

Author Closing Comment

by:m2ew
ID: 38293413
I added that, which helped fix the issue too. I still kept it within the setTimout() function so the height gets detected when the infinite scroll is queued.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month15 days, 21 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question