I'm trying to fire an event when scrolling down to a certain point/value and execute again when scroll back up and down again.
I tried:
$(window).scroll(function(){
var sTop = $(this).scrollTop();
if(sTop == 300){
//execute event - failed
}
});
The above does not fire anything with the both mousewheel and dragging the scrollbar. but when I changed the condition to greater than or equal to, the event is being executed but multiple times when scrolling further down.
if(sTop >= 300){
//execute event - success
}
I also tried changing the condition value and some times the event is being fired.
if(sTop == 333 /* or 431, 658, etc. */){
//execute event - sometimes success
}
Here is my test fiddle.
Could it be that scroll()
function sometimes skips a value if you scroll fast?
Can someone explain the issue and help me with a workaround on how to trigger an event when the $(window).scrollTop() == 'value'
. Thanks