• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / jQuery / Using jQuery to Scroll to Div

Using jQuery to Scroll to Div

April 2, 2022 Leave a Comment

We can use jQuery to scroll to a div by using the jQuery animate method along with the jQuery offset() method.

$("html, body").animate({ scrollTop: $("#div1").offset().top }, "1000");

Note that the number 1000 above is the speed at which we want the scroll to happen in milliseconds. We can change this number to whatever spend we want to scroll at.


Let’s say we have the following HTML:

<div id="div1">This is a div</div>

To scroll to top of the div, #div1, we can use the jQuery animate method combined with the jQuery offset method. We will use the offset method to get the coordinates of the div, and get the top position the div from this method.

$("html, body").animate({ scrollTop: $("#div1").offset().top }, "1000");

If you are using WordPress, don’t forget to change the $ to jQuery as below:

jQuery$("html, body").animate({ scrollTop: $("#div1").offset().top }, "1000");

Using jQuery to Scroll to a Div with a Click

Let’s say we have the following HTML code and we want to scroll smoothly to the div with id #scroll-to-me.

<div id="div">
  <div id="scroll-to-me">This is the div that we will scroll to on click.</div>
  <div id="click-me">Scroll</div>
</div>

We can utilize both the jQuery click() method and jQuery animate() method to scroll to the top of the div.

We will also use the offset() method to get the top position of the div so that we can scroll to it.

Here is the jQuery code:

$("#click-me").click(function() {
  $('html, body').animate({ scrollTop: $("#scroll-to-me").offset().top }, '1000');
});

The final code and output for this example of how to scroll to a div using jQuery and Javascript is below.

Code Output:

This is the div that we will scroll to on click.

Scroll

Full Code:

<div id="div">
  <div id="scroll-to-me">This is the div that we will scroll to on click.</div>
  <div id="click-me">Scroll</div>
</div>

<script>

$("#click-me").click(function() {
  $('html, body').animate({ scrollTop: $("#scroll-to-me").offset().top }, '1000');
});

</script>

Hopefully this article has been useful for you to understand how to use jQuery to scroll to a div.

Other Articles You'll Also Like:

  • 1.  Using jQuery to Disable Input
  • 2.  Using jQuery to Remove the Id of a Div
  • 3.  Using jQuery to Get the Next Sibling of an Element
  • 4.  Using jQuery to Get Text Length
  • 5.  Using jQuery to Show and Hide a Div
  • 6.  jQuery has – Check if an Element Has Other Elements
  • 7.  jQuery get first child
  • 8.  Using jQuery to Check if Checkbox is Checked
  • 9.  Using jQuery to Wait 1 Second Before Executing Code
  • 10.  Using jQuery to Change the Font Size of a Paragraph

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy