scroll to element javascript code example

Example 1: js scroll to id

// Js
document.getElementById('id').scrollIntoView();

// JQuery
$('#id')[0].scrollIntoView();

Example 2: javascript scroll to element

document.getElementById("divId").scrollIntoView();

Example 3: 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 4: javascript scrollintoview

var element = document.getElementById("box");

element.scrollIntoView();
element.scrollIntoView(false);
element.scrollIntoView({block: "end"});
element.scrollIntoView({behavior: "smooth", block: "end", inline: "nearest"});

Example 5: scroll through page with javascript

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

Example 6: scrollintoview

element.scrollIntoView();
element.scrollIntoView(alignToTop); // Boolean parameter
element.scrollIntoView(scrollIntoViewOptions); // Object parameter