Вопрос

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