js scroll down code example

Example 1: automatically scroll to bottom of page

window.scrollTo(0,document.body.scrollHeight);

Example 2: 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 3: scroll through page with javascript

window.scroll({
 top: 0, 
 left: 0, 
 behavior: 'smooth' 
});

Example 4: javascript scroll down

window.scrollTo(300, 500);	//X=300 and Y=500

Example 5: window.scroll

scroll function

window.scroll({
 top: 0, 
 left: 0, 
 behavior: 'smooth' 
});

window.scroll(x-coord, y-coord)
window.scroll(options)

Example 6: scroll js

window.scroll(x-coord, y-coord)
window.scroll(options)