Achieving this can be done using pure javascript, making it feasible with vue as well
Take a look at this example
Start by focusing on this method:
scrollView(amount) {
let scrollFromTop = window.pageYOffset;
const viewHeight = Math.round(window.innerHeight * Math.abs(amount));
if (scrollFromTop % viewHeight === 0) scrollFromTop += Math.sign(amount);
let targetPos;
if (amount > 0)
targetPos = Math.ceil(scrollFromTop / viewHeight) * viewHeight;
else
targetPos = Math.floor(scrollFromTop / viewHeight) * viewHeight;
window.scrollTo({
top: targetPos,
behavior: "smooth"
});
}
It calculates the view height and current scroll position from the top. Then, it scrolls the page up or down based on the specified amount and relative to the view size.
Note: smooth scrolling may not be supported in all browsers, so consider using smoothscroll-polyfill if you choose to implement this solution.
Next, examine these hooks that handle key events:
mounted() {
window.addEventListener("keydown", this.keyHandler);
},
beforeUnmount() {
window.removeEventListener("keydown", this.keyHandler);
}
And finally, the event handling logic:
keyHandler(e) {
if (e.key === 'ArrowDown') {
e.preventDefault();
this.scrollView(this.scrollAmount);
}
if (e.key === 'ArrowUp') {
e.preventDefault();
this.scrollView(this.scrollAmount * -1);
}
},