Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • jQuery Tutorial
  • jQuery Selectors
  • jQuery Events
  • jQuery Effects
  • jQuery Traversing
  • jQuery HTML & CSS
  • jQuery AJAX
  • jQuery Properties
  • jQuery Examples
  • jQuery Interview Questions
  • jQuery Plugins
  • jQuery Cheat Sheet
  • jQuery UI
  • jQuery Mobile
  • jQWidgets
  • Easy UI
  • Web Technology
Open In App
Next Article:
How to change style of elements on scroll using jQuery ?
Next article icon

How to scroll automatically to a particular element using JQuery?

Last Updated : 03 Aug, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report
The task is to scroll to a particular element automatically. Below are the approaches: Approach 1:
  • Get the height of the element by .position().top property.
  • Use .scrollTop() method to set the vertical position of the scrollbar equal to the calculated height of the particular element.
Example 1: This example uses the approach discussed above. html
<!DOCTYPE HTML> <html>  <head>     <title>         How to scroll automatically to a particular element.     </title>     <style>         #GFG_DOWN {             margin-top: 370px;         }     </style>     <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">     </script> </head>  <body style="text-align:center;">     <h1 style="color:green;">               GeeksForGeeks           </h1>     <p id="GFG_UP"         style="font-size: 15px;               font-weight: bold;">     </p>     <button onclick="gfg_Run()">         Click Here     </button>     <p id="GFG_DOWN"        style="color:green;               font-size: 30px;                font-weight: bold;">         This is element.     </p>     <script>         var el_up = document.getElementById("GFG_UP");         var el_down = document.getElementById("GFG_DOWN");         el_up.innerHTML =  "Click on the button to scroll to the particular element.";          function gfg_Run() {             $(window).scrollTop($('#GFG_DOWN').position().top);         }     </script> </body>  </html> 
Output:
  • Before clicking on the button:
  • After clicking on the button:
Approach 2:
  • Get the height of the element by .offset().top property.
  • Use .animate() method to animate to the element.
Example 2: This example uses the approach discussed above. html
<!DOCTYPE HTML> <html>  <head>     <title>         How to scroll automatically to a particular element.     </title>     <style>         #GFG_DOWN {             margin-top: 400px;         }     </style>     <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">     </script> </head>  <body style="text-align:center;">     <h1 style="color:green;">               GeeksForGeeks           </h1>     <p id="GFG_UP"         style="font-size: 15px; font-weight: bold;">     </p>     <button onclick="gfg_Run()">         Click Here     </button>     <p id="GFG_DOWN"         style="color:green;                font-size: 30px;               font-weight: bold;">         This is element.     </p>     <script>         var el_up = document.getElementById("GFG_UP");         var el_down = document.getElementById("GFG_DOWN");         el_up.innerHTML =  "Click on the button to scroll to the particular element.";          function gfg_Run() {             $('html, body').animate({                 scrollTop: $("#GFG_DOWN").offset().top             }, 500);         }     </script> </body>  </html> 
Output:
  • Before clicking on the button:
  • After clicking on the button:

jQuery is an open source JavaScript library that simplifies the interactions between an HTML/CSS document, It is widely famous with it's philosophy of “Write less, do more". You can learn jQuery from the ground up by following this jQuery Tutorial and jQuery Examples.


Next Article
How to change style of elements on scroll using jQuery ?

P

PranchalKatiyar
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • JQuery
  • jQuery-Misc

Similar Reads

  • How to scroll automatically to the Bottom of the Page using jQuery?
    We are given an webpage and the task is to automatically scroll to the bottom of the page using jQuery. Below are the approaches to achieve this: Table of Content Using the scrollTop with height() methodUsing scrollTop with animate() methodUsing the scrollTop with height() methodIn this approach, we
    3 min read
  • How to Scroll to an Element Inside a Div using JavaScript?
    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. Below are the methods to scroll to an element inside a Div using JavaScript: Table of Content Using scrollIntoV
    3 min read
  • How to change style of elements on scroll using jQuery ?
    We have given an HTML document containing some CSS properties, and the task is to change the CSS property to a particular element after scrolling the page using jQuery. To change the style of element on scroll, we get the number of pixels by which the content of an element has been scrolled horizont
    2 min read
  • How to scroll to specific element using jQuery ?
    Many times, in our website we want to scroll automatically to a section of the webpage when we click on a button or a heading in a navbar or a list. So, to achieve this automatic scrolling to the required element, we need to take the help of jQuery. Using jQuery, we can achieve this in a very simple
    3 min read
  • How to scroll the page up or down using anchor element in jQuery ?
    The problem is to include a slide effect whenever we click a local anchor and we want to scroll up or down the page accordingly. Earlier we can do it natively by using CSS property. Syntax: a { scroll-behavior: smooth; } Now with the help of jQuery, we can do it by using the following two methods: j
    2 min read
  • How to scroll to a particular element or skip the content in CSS ?
    Sometimes we need the user to click on a button or link and take them to an element present on the same HTML page. Also, this needs to take the user to the element without any reload. Lots of times, users tend to skip to the main content, like on a gaming Website. They might want to skip the instruc
    4 min read
  • How to animate scrollTop using jQuery ?
    The scrollTop property in JavaScript is used to set or return the vertical scrollbar position of any element. Setting the scrollTop to any value will make the page scroll to that location. The scroll by default takes place immediately and abruptly scrolls to the value. This scrolling can be animated
    3 min read
  • How to scroll to a particular element or skip to content in React JS ?
    In this article, we will learn about how to scroll or skip a particular element in React JS, We can do this using React hook useRef and React styled-components. Prerequisite:ReactReact Styled Components.React useRef hooks.Approach to implement particular element scroll/skip: It is used to scroll to
    4 min read
  • How to attach a method to HTML element event using jQuery ?
    In this article, we are going to learn, how can we attach a method to an HTML element event using jQuery. There are various events like click(), mouseenter(), mouseout(), etc which are available in jQuery. For attaching a method to an HTML elements event, we will need a jQuery method on(), which wil
    3 min read
  • How to Get and Set Scroll Position of an Element using JavaScript ?
    In this article, we will learn how to get and set the scroll position of an HTML element using JavaScript. Approach: We will be using the HTML DOM querySelector() and addEventListener() methods and the HTML DOM innerHTML, scrollTop and scrollLeft properties. We create an HTML div element with an id
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences