質問

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