I want to scroll to a div each time user presses j key. Here is the code for it.
$(function() {
function scroll(direction) {
var s
The problem is that you're always moving the scrollTop
value to 50 pixels before the first matched element, so it's always identifying that element as the one you need to scroll to in your if
statement because its position is greater than the current scrollTop
value.
Modify the relevant section of your code to this:
if (direction == 'next' && positions[i] > here + 50) {
scroll = collection.get(i);
break;
}
That way it accounts for the window being scrolled to 50 pixels above the current element.