Created
August 26, 2021 10:41
-
-
Save oliversisson/5b84cb3bc9ea74ae4873b025aa122310 to your computer and use it in GitHub Desktop.
scroll by half
This file contains 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
(function() { | |
var currentScroll = window.scrollY; | |
var currentlyScrolling = false; | |
var runOnScroll = function(evt) { | |
if (currentlyScrolling) { | |
// preventDefault doesn't seem to work | |
window.scrollTo(window.X, currentScroll); | |
} | |
}; | |
var scrollHalf = function() { | |
currentScroll = currentScroll + window.innerHeight / 2; | |
currentlyScrolling = false; | |
window.scrollTo({ | |
behavior: 'smooth', | |
left: window.scrollX, | |
top: currentScroll, | |
}); | |
}; | |
window.addEventListener("scroll", runOnScroll); | |
document.onkeypress = function (e) { | |
e = e || window.event; | |
if (e.keyCode = 32) { | |
currentScroll = window.scrollY; | |
currentlyScrolling = true; | |
setTimeout(scrollHalf, 50); | |
} | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment