Created
November 25, 2017 05:27
-
-
Save allenhwkim/83a99b30471a60382fb2b6c49d8bc9c7 to your computer and use it in GitHub Desktop.
get the speed of scroll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| this.prevScroll = {x:0,y:0,time: (new Date()).getTime()}; | |
| function getScrollSpeed(el) { | |
| let moveX, moveY, speedX, speedY, now = (new Date()).getTime(); | |
| moveX = Math.abs(el.scrollX - (this.prevScroll.x || el.scrollX)); | |
| moveY = Math.abs(el.scrollY - (this.prevScroll.y || el.scrollY)); | |
| speedX = moveX / (now - this.prevScroll.time); | |
| speedY = moveY / (now - this.prevScroll.time); | |
| this.prevScroll = {x: el.scrollX, y: el.scrollY, time:(new Date()).getTime()}; | |
| console.log(el.scrollY, moveY,speedY, this.prevScroll); | |
| } | |
| window.addEventListener('scroll', e => getScrollSpeed(e.currentTarget)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment