Code implementation for returning to the top of the page under js control _ javascript skills

Source: Internet
Author: User
When browsing the Web page, you should often see a floating stuff [back to the top] in the lower right corner. This article also needs to be implemented using js. If you are interested, refer to the following, I hope this will help you see many floating items [back to the top] in the bottom right corner of the page. If they do not exist at the beginning, they will appear as soon as they move the scroll bar, it disappears at the top.
For example:

The javascript code is as follows:

The Code is as follows:


Function goTop (){
Var _ btn = document. getElementById ("goTop ");
If (document.doc umentElement & document.doc umentElement. scrollTop ){
Var _ con = document.doc umentElement;
} Else if (document. body ){
Var _ con = document. body;
}
Window. onscroll = set;
_ Btn. style. left = document.doc umentElement. scrollWidth/2 + 500 + "px ";
_ Btn. onclick = function (){
_ Btn. style. display = "none ";
Window. onscroll = null;
This. timer = setInterval (function (){
_ Con. scrollTop-= Math. ceil (_ con. scrollTop * 0.3 );
If (_ con. scrollTop = 0) clearInterval (_ btn. timer, window. onscroll = set );
}, 10 );
};
Function set (){
_ Btn. style. display = _ con. scrollTop? 'Block': "none ";
}
};
Document. write ("

Return
Back
Top
Department

");
Window. onscroll = goTop;

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.