Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Infinite Scroll Plugin Conditional Callback

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #42798

    I’m using the Infinite Scroll plugin for WordPress, and the Isotope plugin for the layout of certain pages.

    Basically, you click a button and it loads more posts. I have this callback in the Infinite Scroll options:

    var $newElems = $( newElements ).css({ opacity: 0 });
    $newElems.imagesLoaded(function(){
    $newElems.animate({ opacity: 1 });
    $(‘#main-posts’).isotope( ‘appended’, $newElems );
    });

    However, there is one page where I’m NOT using Isotope, but still want to use infinite scroll. It still works as expected, but since Isotope isn’t used on that page, Infinite Scroll creates an error.

    Is there any way I can make the callback only on pages that are using Isotope?

    #124966

    Hold on, I think I fixed it. I’ll leave this here for anyone who might have this problem in the future.

    All I did was check if the isotope container exists during the callback. Like so:

    if ( $(‘.isotope’).length ) {
    var $newElems = $( newElements ).css({ opacity: 0 });
    $newElems.imagesLoaded(function(){
    $newElems.animate({ opacity: 1 });
    $(‘#main-posts’).isotope( ‘appended’, $newElems );
    });
    }

    If anyone has a better way, let me know, but this seems to work just fine.

    #124974
    TheDoc
    Member

    My gut instinct told me that doing `hasClass()` would be faster. Looks like that’s probably the case:

    http://lookalive.co.uk/blog/jquery-which-faster-hasclass-or-length

    So I would so something like:

    if( $(‘.your-div’).hasClass(‘isotope’) ) { }

    #124978

    Ah, cool. Thanks.

Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.