Home > Blockchain >  Onclick open page and scroll down
Onclick open page and scroll down

Time:09-27

When particular page is opened by clicking on button which id is "button1", I want that page to be scrolled down.

Could that code be in JavaScript?

CodePudding user response:

This is the command you can use to scroll down:

window.scrollTo(0,document.body.scrollHeight);

Now if you want to go directly to the bottom you can put this do that while the page are loaded:

<body onl oad="window.scrollTo(0,document.body.scrollHeight);">

CodePudding user response:

Here is an example of what you are asking about:

<html>
    <head>
        <script>
            clicked(){
                var element = document.getElementById("scrollto");
                element.scrollIntoView();
            }
        </script>
    </head>
    <body>
        <button onclick="clicked()">Click Me</button>

        Your content...





        <div id="scrollto"></div>
    </body>
</html>

When you click the button, it should scroll down to the div.

If you are asking about navigating to a new page and automatically having it scroll to the right section, you can do something like:

<a href="mypage.html#scrollto"></a>

and the link will automatically open to that section of the page.

  • Related