Skip to content

Instantly share code, notes, and snippets.

@oliversisson
Created August 26, 2021 10:41
Show Gist options
  • Save oliversisson/5b84cb3bc9ea74ae4873b025aa122310 to your computer and use it in GitHub Desktop.
Save oliversisson/5b84cb3bc9ea74ae4873b025aa122310 to your computer and use it in GitHub Desktop.
scroll by half
(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