Is there a way to prevent $(window).scroll()
from firing on page load?
Testing the following code in Firefox 4, it fires even when I unplug the mouse.
jQuery(document).ready(function($){
$(window).scroll(function(){
console.log("Scroll Fired");
});
});
The scroll event does not fire on every load, only when refreshing a page that was scrolled, or when navigating to an anchor directly.
Many of the answers suggest ignore the first time it's called, which would ignore a valid scroll if the page doesn't get scrolled initially.
The
scroll
event is unrelated to the mouse, it is called whenever a new document scrolling position is set. And arguably that position is set when the document loads (you might load it with an anchor after all), also if the user presses a cursor key on his keyboard. I don't know why you need to ignore the initialscroll
event but I guess that you only want to do it ifpageYOffset
is zero. That's easy:Note: MSIE doesn't have
window.pageYOffset
property so the above will need to be adjusted. Maybe jQuery offers a cross-browser alternative.This was the solution I went for. Any improvements gratefully received.
sure, don't load it until after the load. make it a call back for sleep for 500 millis.