Scrollable div: how to determine the scroll direction from an OnScroll event for a scrollable div?

How to determine the scroll direction for a scrollable div from an OnScroll event? I want to add 15 to scrollTop if the user scrolls and subtracts 15 if the user scrolls down. Can i use jQuery?

+4
source share
1 answer

JSFIDDLE

/** * Calculate incremental amounts of pixels scrolled in each axis. * Value is signed to its direction. */ function incrementalScroll(e) { var scrollX = (this.x || window.pageXOffset) - window.pageXOffset; var scrollY = (this.y || window.pageYOffset) - window.pageYOffset; this.x = window.pageXOffset; this.y = window.pageYOffset; test(scrollX, scrollY); } window.onscroll = incrementalScroll; 

Use it however you want.

 function test(scrollX, scrollY){ var directionX = !scrollX ? "NONE" : scrollX>0 ? "LEFT" : "RIGHT"; var directionY = !scrollY ? "NONE" : scrollY>0 ? "UP" : "DOWN"; console.log(directionX, scrollX, directionY, scrollY); } 
+2
source

Source: https://habr.com/ru/post/1307412/


All Articles