Join TalkersCode Community And Recieve Exclusive Tutorials
On Web Development In Your Email

Smooth Scrolling To Div Using jQuery

Thursday, Jan 19, 2017

Tags:- jQuery HTML


Smooth Scrolling to div is a scroll to particular div with animations when user clicks on a particular button in this case.So, in this tutorial we will show you how to smooth scroll to div using jQuery.You may also like auto scrolling page using jQuery.


See Demo
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.

Join Us With Our 21000+ Subscribers And Get Our Latest Tutorials Update Via Email