How to scroll to an element inside a div using javascript?
Last Updated :
25 Jun, 2024
To scroll to an element within a div using JavaScript, set the parent div’s scrollTop`to the target element’s offsetTop. This method allows smooth navigation within a scrollable container. Various JavaScript methods are available for scrolling to an element.
The scrollIntoView() method is used to scroll to the specified element in the browser.Â
Syntax:Â
element.scrollIntoView()
Example: In this example, we will see the implementation Using scrollIntoView() to scroll to an element. Â
html
<!DOCTYPE html>
<html>
<head>
<style>
#condiv {
height: 500px;
width: 500px;
overflow: auto;
background: #82c93a;
}
#ele {
top: 70%;
height: 200px;
width: 200px;
background-color: green;
position: absolute;
}
</style>
</head>
<body>
<p>Click the button to scroll to the element.</p>
<button onclick="scrolldiv()">Scroll</button>
<div id="condiv">
<div id="ele">
GeeksforGeeks
</div>
</div>
<script>
function scrolldiv() {
var elem = document
.getElementById("ele");
elem
.scrollIntoView();
}
</script>
</body>
</html>
Output:Â

Output
The scroll() method is used to scroll to the specified element in the browser.Â
Syntax:
element.scroll(x-cord,y-cord)
Example: In this example, we will see the implementation Using scroll() to scroll to an element.Â
html
<!DOCTYPE html>
<html>
<head>
<style>
#condiv {
height: 500px;
width: 500px;
overflow: auto;
background: #82c93a;
}
#ele {
top: 70%;
height: 200px;
width: 200px;
background-color: green;
position: absolute;
}
</style>
</head>
<body>
<p>Click the button to scroll to the element.</p>
<button onclick="scrolldiv()">Scroll</button>
<div id="condiv">
<div id="ele">GeeksforGeeks</div>
</div>
<script>
function scrolldiv() {
window.scroll(0, findPosition(document.getElementById("ele")));
}
function findPosition(obj) {
var currenttop = 0;
if (obj.offsetParent) {
do {
currenttop += obj.offsetTop;
} while ((obj = obj.offsetParent));
return [currenttop];
}
}
</script>
</body>
</html>
Output:Â

Output
The scrollTo() method is used to scroll to the specified element in the browser.Â
Syntax:
element.scrollTo(x-cord,y-cord)
Example: In this example, we will see the implementation Using scrollTo() to scroll to an element.Â
html
<!DOCTYPE html>
<html>
<head>
<style>
#condiv {
height: 500px;
width: 500px;
overflow: auto;
background: #82c93a;
}
#ele {
top: 70%;
height: 200px;
width: 200px;
background-color: green;
position: absolute;
}
</style>
</head>
<body>
<p>Click the button to scroll to the element.</p>
<button onclick="scrolldiv()">Scroll</button>
<div id="condiv">
<div id="ele">GeeksforGeeks</div>
</div>
<script>
function scrolldiv()
{
window
.scrollTo(0, findPosition(document.getElementById("ele")));
}
function findPosition(obj) {
var currenttop = 0;
if (obj.offsetParent) {
do {
currenttop += obj.offsetTop;
} while ((obj = obj.offsetParent));
return [currenttop];
}
}
</script>
</body>
</html>
Output:Â

Output
4. Using animate() Method (jQuery)
The animate() method in jQuery can also be used for smooth scrolling animations to an element within a container. Ensure jQuery is included in your project for this approach.
Example: To demonstrate scrolling to an element inside a div using JQuery animate() method.
HTML
<!DOCTYPE html>
<html>
<head>
<style>
#condiv {
height: 300px;
width: 500px;
overflow: auto;
background: #f0f0f0;
}
#ele {
margin-top: 600px;
height: 200px;
width: 200px;
background-color: #4caf50;
}
</style>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<p>Click the button to scroll to the element.</p>
<button onclick="scrollToElement()">Scroll</button>
<div id="condiv">
<div id="ele">GeeksforGeeks</div>
</div>
<script>
function scrollToElement() {
$("#condiv").animate(
{
scrollTop: $("#ele").offset().top - $("#condiv").offset().top,
},
1000
);
}
</script>
</body>
</html>
Output:

Output
Please Login to comment...