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?

有帮助吗?

解决方案 2

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

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

Thanks for your input though. John

其他提示

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

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

jQuery.trigger

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top