I know .scroll() detects scrolling but what if I want something to happen only when the user scrolls down or vice versa?
Asked
Active
Viewed 535 times
1 Answers
0
There is not a listener for this, but you can have a variable with last position scrolled and check against new one. So it would be something like this:
Keep a variable, say, last_scroll_position
, and when you have a scroll, if last_scroll_position - current_position > 0
, the user scrolled up, and down if it's less than 0.
For further reference:
Differentiate between scroll up/down in jquery?
Example:
<div style='height:10000px;'>
var last_scroll=0;
$(window).scroll(function() {
var direction='none';
var current_scroll=$(this).scrollTop();
if(current_scroll < last_scroll) direction='up';
else if(current_scroll > last_scroll) direction = 'down';
last_scroll=current_scroll;
console.log(direction)
});
last_scroll_position
. I have a function that capturesscrollTop
so creating the variable for the current scroll position is pretty easy. Just can't wrap my head around how to capture the previous scroll position. – tripstar Apr 28 '14 at 14:17