I generate dynamically a html page and I'd like to be able to navigate through it up and down with the arrow keys, scrolling to the next anchor each time I press UP or DOWN.
Here's my code (doesn't work)
$(document).keydown(function(e){
if (e.keyCode == 40) {
console.log('keydown');
if ( next === undefined ) {
next = $('.js_anchor').next();
} else {
next = next.next();
}
$(document).animate({scrollTop: next}, 2000,'easeInOutCubic');
}
});
Any ideas ? Thanks !
Use a variable to store the anchor list and then the key index to the current one like so:
This have some bad effects if the user scrolls by himself and presses the down arrow it might scroll up... Use this function to determine which to scroll to:
And replace
by
Please take note that it has NOT been tested but should be close to working, if not already working.