How to scroll the page up or down using anchor element in jQuery ? Last Updated : 24 Apr, 2020 Comments Improve Suggest changes Like Article Like Report 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: jQuery.offset() method: This method allows us to retrieve the current position of any element relative to the document. This method is particularly useful when we want to perform drag and drop operations or when we want to position the new element on top of another existing element. jQuery.animate() method: This method allows to create animations on any numeric CSS property. CSS object is required in order to use the method. We can use this to animate both style and non-style properties. Example: html <!DOCTYPE html> <html> <head> <title> How to scroll the page up or down using anchor element in jQuery ? </title> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script> <script> $(document).ready(function () { // Add smooth scrolling to all links $("a").on('click', function (event) { // Make sure this.hash has a value // before overriding default behavior if (this.hash !== "") { // Prevent default anchor // click behavior event.preventDefault(); // Store hash var hash = this.hash; // Using jQuery's animate() method // to add smooth page scroll // The optional number (800) specifies // the number of milliseconds it takes // to scroll to the specified area $('html, body').animate({ scrollTop: $(hash).offset().top }, 500, function () { // Add hash (#) to URL when done // scrolling (default click behavior) window.location.hash = hash; }); } // End if }); }); </script> <style> body, html, .primary { height: 150%; } section { min-height: 150%; } </style> </head> <body> <div> <a href="#section1"> Click Me to Smooth Scroll to Section 1 Below </a> </div> <div> <a href="#section2"> Click Me to Smooth Scroll to Section 2 Below </a> </div> <div> <a href="#section3"> Click Me to Smooth Scroll to Section 3 Below </a> </div> <div class="primary"> <section></section> </div> <div class="primary" id="section1"> <section style="background-color:cyan"></section> </div> <div class="primary" id="section2"> <section style="background-color:yellow"></section> </div> <div class="primary" id="section3"> <section style="background-color:red"></section> </div> </body> </html> Output: Before scrolling effect on Section 1: After scrolling effect on Section 1 we go to Cyan Colored Box as in Code: Comment More infoAdvertise with us Next Article How to scroll the page up or down using anchor element in jQuery ? A ankurgokhale05 Follow Improve Article Tags : Web Technologies JQuery Write From Home jQuery-Questions Similar Reads 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 scrollIntoVi 3 min read 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 ContentUsing the scrollTop with height() methodUsing scrollTop with animate() methodUsing the scrollTop with height() methodIn this approach, we wi 3 min read Scroll to the top of the page using JavaScript/jQuery Given a webpage and the task is to scroll to the top of the page using jQuery.A scrollable page can be scrolled to the top using 2 approaches: Table of ContentUsing window.scrollTo()Using scrollTop() in jQueryMethod 1: Using window.scrollTo()The scrollTo() method of the window Interface can be used 3 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 automatically to a particular element using JQuery? 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: Thi 2 min read Like