Javascript implementation Back to Top special effect _ javascript tips-js tutorial

Source: Internet
Author: User
When the page is particularly long, you must scroll back to the top of the page several times before returning to the top. If there is a "Back to Top" button in the bottom right corner of the page, click, you can return to the top, which is a good experience for users. HTML:

 

CSS:

#btn1{position:fixed;bottom:10px;right:10px;}

JS:

Window. onload = funcition () {var oBtn = document. getElementById ("btn"); var timer = null; // declare a variable to see if it is a system or if the user rolls var sBys = true; window. onscroll = funcition () {if (! SBys) {clearInterval (timer);} sBys = false;} oBtn. onclick = funcition () {timer = setInterval (funcition () {// obtain scrollTop var scrollTop=document.doc umentElement. scrollTop | document. body. scrollTop; // calculate the buffer speed var ispeed = Math when the button is clicked back to the top. floor (-scrollTop/8); if (scrollTop = 0) {clearInterval (timer)} sBys = true; document.doc umentElement. scrollTop = document. body. scrollTop = scrollTop + ispeed;}, 30 )}}

Knowledge point:

1. computation speed (buffer) rounded down
2. When scrollTop = 0, the timer needs to be cleared.
3. You need to determine whether it is a user or js operation scroll bar. If it is a user operation, clear the timer.

The above is all the content of this article. I hope you will like it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.