how to scroll up in javascript code example
Example 1: scroll down up js
var lastScrollTop = 0;
// element should be replaced with the actual target element on which you have applied scroll, use window in case of no target element.
element.addEventListener("scroll", function(){ // or window.addEventListener("scroll"....
var st = window.pageYOffset || document.documentElement.scrollTop; // Credits: "https://github.com/qeremy/so/blob/master/so.dom.js#L426"
if (st > lastScrollTop){
// downscroll code
} else {
// upscroll code
}
lastScrollTop = st <= 0 ? 0 : st; // For Mobile or negative scrolling
}, false);
Example 2: scroll through page with javascript
window.scroll({
top: 0,
left: 0,
behavior: 'smooth'
});
Example 3: javascript scroll down
window.scrollTo(300, 500); //X=300 and Y=500