Skip to content

Instantly share code, notes, and snippets.

@worldoptimizer
Created May 6, 2022 12:43
Show Gist options
  • Save worldoptimizer/1a9ac8cb210d513a747ad313ddb93748 to your computer and use it in GitHub Desktop.
Save worldoptimizer/1a9ac8cb210d513a747ad313ddb93748 to your computer and use it in GitHub Desktop.
var scrollSpeed = 0;
var isScrolling = false;
document.addEventListener('keydown', function(e) {
if (sidebar.classList.contains('open')) {
if (e.shiftKey && e.keyCode === 38) {
scrollSpeed *= 1.5;
scrollSpeed = Math.min(Math.max(scrollSpeed, -10), -1);
if (!isScrolling) {
isScrolling = requestAnimationFrame(scroll);
}
}
if (e.shiftKey && e.keyCode === 40) {
if (Math.abs(scrollSpeed) < 1) scrollSpeed = 1;
scrollSpeed *= 1.5;
scrollSpeed = Math.min(Math.max(scrollSpeed, 1), 10);
console.log(scrollSpeed)
if (!isScrolling) {
isScrolling = requestAnimationFrame(scroll);
}
}
}
});
function scroll() {
if (sidebar.classList.contains('open')) {
if (Math.abs(scrollSpeed) > 0.1) {
sidebar.scrollTop += scrollSpeed;
scrollSpeed *= 0.95;
isScrolling = requestAnimationFrame(scroll);
} else {
scrollSpeed=0;
isScrolling = null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment