All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Smooth Scrolling To Div Using jQuery

Last Updated : Jul 1, 2023

IN - jQuery HTML | Written & Updated By - Pragati

In this tutorial we will show you how to smooth scroll to div using jQuery, smooth Scrolling to div is a scroll to particular div with animations when user clicks on a particular button in this case.

You may also like auto scrolling page using jQuery.

Smooth Scrolling To Div Using jQuery

To Smooth Scrolling To Div It Takes Only Two Steps:-

  1. Make a HTML file and define markup and scripting
  2. Make a CSS file and define styling

Step 1. Make a HTML file and define markup and scripting

We make a HTML file and save it with a name scroll.html

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<link rel="stylesheet" href="scroll.css">
<script type="text/javascript">
function scroll_to_div(div_id)
{
 $('html,body').animate(
 {
  scrollTop: $("#"+div_id).offset().top
 },
 'slow');
}
</script>
</head>
<body>
<div id="wrapper">

<input type="button" class="scroll_button" value="Scroll To Div1" onclick="scroll_to_div('div1')">
<input type="button" class="scroll_button" value="Scroll To Div2" onclick="scroll_to_div('div2')">
<input type="button" class="scroll_button" value="Scroll To Div3" onclick="scroll_to_div('div3')">

<div id="div1" class="scroll_div">
<h2>DIV 1</h2>
<p>
This is a sample content and This is a demo of Smooth Scrolling Div Using jQuery
This is a sample content and This is a demo of Smooth Scrolling Div Using jQuery
</p>
</div>

<div id="div2" class="scroll_div">
<h2>DIV 2</h2>
<p>
This is a sample content and This is a demo of Smooth Scrolling Div Using jQuery
This is a sample content and This is a demo of Smooth Scrolling Div Using jQuery
</p>
</div>

<div id="div3" class="scroll_div">
<h2>DIV 3</h2>
<p>
This is a sample content and This is a demo of Smooth Scrolling Div Using jQuery
This is a sample content and This is a demo of Smooth Scrolling Div Using jQuery
</p>
</div>
</div>
</body>
</html>

In this step we create three div for scrolling and create three buttons to scroll to particular div.

We also create scroll_to_div() function which uses jQuery animate() function and under this we do smooth scrolling using jQuery scrollTop by getting difference between page top and particular div using offset().top function and then we add slow to scrolling slowly to div.

You can use your own value like fast,normal and you can also give any integer value. You may also like page scroll to top using jQuery.

Step 2. Make a CSS file and define styling

We make a CSS file and save it with a name scroll.css

body
{
 text-align:center;
 width:100%;
 margin:0 auto;
 padding:0px;
 font-family:helvetica;
 background-color:#E6E6E6;
}
#wrapper
{
 text-align:center;
 margin:0 auto;
 padding:0px;
 width:995px;
 height:2000px;
}
.scroll_button
{
 background:none;
 border:1px solid #585858;
 color:#585858;
 width:150px;
 height:35px;
 font-size:16px;
 cursor:pointer;
}
.scroll_div
{
 width:500px;
 border:1px solid;
 margin-left:245px;
 margin-top:50px;
}

Thats all, this is how to do smooth scrolling to div using jQuery. You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

I hope this tutorial on smooth scrolling using jQuery helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪