All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

JavaScript Scroll To Top Of DIV

Last Updated : Mar 11, 2024

JavaScript Scroll To Top Of DIV

In this tutorial we will show you the solution of JavaScript scroll to top of div, here we needs to use scrollIntoView() method for scroll to top of div element by just one click.

The scrollIntoView() method scrolls an element into the visible area of the browser window.

So when we click that we can easily scrolled to top of div element by specifying that top element id with this method.

Step By Step Guide On JavaScript Scroll To Top Of DIV :-

Here we needs to define div element with id attribute for specify in script block to access and have to confirm one thing is sets it height value to long distance then only we can see the scroll process clearly.

In script block we needs to define div element by getElementById() method and with this have to specify scrollIntoView() method and sets its value to ‘true’ then only it will scrollable to defined element.

<!DOCTYPE html>
        <title>SCROLL TO TOP</title>
                height: 2000px;
        <div id="top">
            <h1>SCROLL BELOW</h1>
        <a href="javascript:scroll()">Jump2Top</a>
            function scroll(){
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. In style tag we specified div element ‘top’ height value to ‘2000px’ so it have some long height we can see the scrollable process clearly.
  7. Here we defined div tag with attribute ‘id’ with value ‘top’ and within that we defined text ‘SCROLL BELOW’ like instruction for users so their will move to bottom page. Anchor tag ‘Jump2Top’ defined with href attribute, we sets it value to javascript function scroll().
  8. In script we defined function ‘scroll()’, here we needs to access div element ‘top’ so we used ‘getElementById()’ method then we adding this with ‘scrollIntoView({behavior:'smooth'},true)’.
  9. So element ‘top’ will comes to view position when user clicks on link.
  10. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to scroll to top of div element using javascript.

When we executes program on browser we can see heading ‘SCROLL BELOW’ and after some distance we can see ‘Jump2Top’ link when user clicks that link immediately it will goes to top page automatically. We can also use scrollTo() method for move to top of page.

I hope this tutorial on JavaScript scroll to top of div helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪