Question

I have a function that I want to run on load and also run on scroll.

$(window).scroll(function() {
    $('.cre-animate').each(function(index, value){ STUFF HERE});
});

I have the scrolling function running when elements are at points within the viewport. Problem is that when the page loads, if an element is already past it's trigger point in the viewport it won't run until I scroll. Not a huge issue unless the page is short and there is no scrolling to take place.

I tried adding:

$('html, body').animate({scrollTop: 1});

which only worked if the page was long enough to have a scroll bar.

Any suggestions?

Was it helpful?

Solution 2

So I ended up using this option that I came across:

$(window).bind('scroll load', function()

Thanks for your input though. John

OTHER TIPS

Just use .trigger(...) to trigger the function once:

$(window).scroll(function() {
    $('.cre-animate').each(function(index, value){ STUFF HERE});
}).trigger('scroll');

jQuery.trigger

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top