I need to run a function the first time .scrollTop() changes and then never again. I tried a couple things including the following, but no luck:
var view = $('#workspace');
view.bind("scroll resize", function() {
doThisEveryScroll();
if(view.scrollTop() == 3) {
doThisOnFirstScroll();
}
});
This is wrong for a number of reasons but I am not sure how to approach this any other way.
- This will only run if the user happens to hit
.scrollTop() == 3which isnt always the case with the scroll wheel. .scrollTop() == 3could be true multiple times.
Use
.one()to setup an event handler that detaches itself once it has been called:You can also provide a margin of error for your target scroll position by calculating the distance from your target:
This will return
trueif the user is within20pxof100px.