[ACCEPTED]-$(window).scroll() firing on page load-jquery-events
The scroll
event is unrelated to the mouse, it 11 is called whenever a new document scrolling 10 position is set. And arguably that position 9 is set when the document loads (you might 8 load it with an anchor after all), also 7 if the user presses a cursor key on his 6 keyboard. I don't know why you need to ignore 5 the initial scroll
event but I guess that you 4 only want to do it if pageYOffset
is zero. That's easy:
var oldPageYOffset = 0;
$(window).scroll(function(){
if (window.pageYOffset != oldPageYOffset)
{
oldPageYOffset = window.pageYOffset;
console.log("Window scrolling changed");
}
});
Note: MSIE 3 doesn't have window.pageYOffset
property so the above will 2 need to be adjusted. Maybe jQuery offers 1 a cross-browser alternative.
This was the solution I went for. Any improvements 1 gratefully received.
var scroll = 0;
$(window).scroll(function(){
if (scroll>0){
console.log("Scroll Fired");
}
scroll++;
});
The scroll event does not fire on every 6 load, only when refreshing a page that was 5 scrolled, or when navigating to an anchor 4 directly.
Many of the answers suggest ignore 3 the first time it's called, which would 2 ignore a valid scroll if the page doesn't 1 get scrolled initially.
//Scroll the page and then reload just the iframe (right click, reload frame)
//Timeout of 1 was not reliable, 10 seemed to be where I tested it, but again, this is not very elegant.
//This will not fire initially
setTimeout(function(){
$(window).scroll(function(){
console.log('delayed scroll handler');
});
}, 10);
//This will fire initially when reloading the page and re-establishing the scroll position
$(window).scroll(function(){
console.log('regular scroll handler');
});
div {
height: 2000px;
border: 1px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
</div>
This seems to have worked for me.
$(window).bind("load", function() {
$(window).on("scroll", function () {
console.log('scroll');
});
});
0
sure, don't load it until after the load. make 1 it a call back for sleep for 500 millis.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.