I am scrolling the website content using arrow keys. I want to add scroll timings and scroll effects to this.
I know there are many libraries which uses jquery to do this. I want the solution in javascript. Please help.
var handler = function(e) {
e = e || window.event;
var k = e.keyCode || e.which;
switch (k) {
case 38:
document.body.scrollTop -= 1000;
document.documentElement.scrollTop -= 1000;
break;
case 40:
document.body.scrollTop += 1000;
document.documentElement.scrollTop += 1000;
break;
default:
return true;
}
if (e.preventDefault) e.preventDefault();
return false;
};
if (window.attachEvent) window.addEvent("onkeydown", handler, false);
else window.addEventListener("keydown", handler, false);
div {
height: 1000px;
width: 100%;
}
.red {
background: red;
}
.green {
background: green;
}
.yellow {
background: yellow;
}
<div class="red"></div>
<div class="green"></div>
<div class="yellow"></div>