Https www.w3schools.com howto howto_js_scroll_to_top.asp năm 2024

Greetings! I was implementing this scroll-to-top button with animation scrolling feature in a website and was searching for an easy example. All examples that I found had a lot of extra customization. So, here in this post, I removed those customization and providing a real basic/minimal scroll-to-top with animation feature. You can have a look.

1. Prerequisite

  1. Basic knowledge of HTML, CSS, and JS.
  2. An HTML based page to work on.
  3. And of course a machine. 🙂

2. Add Basic HTML Button

We put this button near to the end of the webpage contents. But you may put this as per your requirement.

Back to Top

3. Add Basic JQuery Source & Script

Include this script in your footer. And that’s it!

4. Best Solution without animation

content here> Back to top

Source: https://stackoverflow.com/questions/32102747/how-to-make-a-back-to-top-button-using-css-and-html-only

5. More Customized Examples

  1. https://www.w3schools.com/howto/howto_js_scroll_to_top.asp
  2. https://getflywheel.com/layout/sticky-back-to-top-button-tutorial/
  3. https://html-online.com/articles/dynamic-scroll-back-top-page-button-javascript/
  4. Please comment below more examples, I’ll be happy to append them here in the list. 🙂

6. Conclusion

Hurray! It was easy and simple. Isn’t it? You can always customize the look of that button with CSS, but basic functionality can be achieve with HTML and JQuery.

Hi. I'd like to add a back to top button like the one from this wiki: https://www.sorumu.com/S%C3%B6zl%C3%BCk:Anasayfa

I've seen they've used the exact same example from here: https://www.w3schools.com/howto/howto_js_scroll_to_top.asp

I added what needed to be added to common.css and common.js, but I am not exactly sure where and how to add the HTML element. I have read this article and then added the HTML element inside common.js, as seen here under 'back to top': http://dragon-mania-legends-wiki.mobga.me/MediaWiki:Common.js

Would anyone please shed a little bit of light as to where I am going wrong?

Star Warden (talkcontribs)

Those topics only offer a back to top next to the headers. But I need a button that floats exactly like the one from the wiki I linked in my first reply. I've followed all the steps to adding the CSS and JS, but I don't know where to add the HTML part. I couldn't find it on that wiki either.

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Learn how to create a "scroll back to top" button with CSS.


Try it Yourself »


How To Create a Scroll To Top Button

Step 1) Add HTML:

Create a button that will take the user to the top of the page when clicked on:

Example


Step 2) Add CSS:

Style the button:

Example

myBtn {

display: none; /* Hidden by default */ position: fixed; /* Fixed/sticky position */ bottom: 20px; /* Place the button at the bottom of the page */ right: 30px; /* Place the button 30px from the right */ z-index: 99; /* Make sure it does not overlap */ border: none; /* Remove borders */ outline: none; /* Remove outline */ background-color: red; /* Set a background color */ color: white; /* Text color */ cursor: pointer; /* Add a mouse pointer on hover */ padding: 15px; /* Some padding */ border-radius: 10px; /* Rounded corners */ font-size: 18px; /* Increase font size */ }

myBtn:hover {

background-color:

555; /* Add a dark-grey background on hover */

}



Step 3) Add JavaScript:

Example

// When the user scrolls down 20px from the top of the document, show the button window.onscroll = function() {scrollFunction()};

function scrollFunction() { if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) { document.getElementById("myBtn").style.display = "block"; } else { document.getElementById("myBtn").style.display = "none"; } }

// When the user clicks on the button, scroll to the top of the document function topFunction() { document.body.scrollTop = 0; // For Safari document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera }

Try it Yourself »

How to scroll automatically to the top of the page using JavaScript?

To make the page jump on the top instantly can be done with the native JavaScript method, namely — window. scrollTo(x-coord, y-coord) .

How to scrollTo top using js?

You might need to trigger scrolling with JavaScript, in which case here are some options:.

window. scrollTo().

document. documentElement. scrollTop().

window. scroll().

document. documentElement. scrollIntoView().

How do I scrollTo the top of the page?

Method 1: Using window. The scrollTo() method of the window Interface can be used to scroll to a specified location on the page. It accepts 2 parameters the x and y coordinate of the page to scroll to. Passing both the parameters as 0 will scroll the page to the topmost and leftmost points.

How do you navigate to the top of the page in HTML?

As defined in the HTML specification, you can use href="

top" or href="#" to link to the top of the current page. Yes, it is easy to scroll to the top by using the html's tag.