scroll to specific div javascript code example

Example 1: js scrolling in div

var divElement = document.getElementById("div");
divElement.scroll({
	top: divElement.scrollHeight,//scroll to the bottom of the element
	behavior: 'smooth' //auto, smooth, initial, inherit
});

Example 2: how can when click buton scrool to another elemtn

$("button").click(function() {
    $('html,body').animate({
        scrollTop: $(".second").offset().top},
        'slow');
});

// example: http://jsfiddle.net/ryXFt/3/

Example 3: scroll to specific div

$(window).scroll(function() {
    $('html, body').animate({
        scrollTop: $("#myDiv").offset().top
    }, 2000);
});

Example 4: how to scroll specific element

1- Actions moveToElement method to
scroll down to specific web elements.

Do you know any other way to scroll?
	2- We can use JSExecutor.executescript
    method to scrool up and down, or left and right.

	JSExecutor --> executeScript --> scrollBy(0,250);
	JSExecutor --> executeScript --> scrollIntoView(true),
                                    WebElementWeWantToScroll;

Tags:

Misc Example