JS achieves the animation effect of returning to the top of the page

Source: Internet
Author: User
/*** Return to the top of the page * @ Param acceleration * @ Param time interval (MS) **/function gotop (acceleration, time ){
 
// Window. scrollto (0, 0); // you can directly use this function if you do not need the animation effect.
Acceleration = acceleration | 0.1; time = Time | 16; var X1 = 0; var Y1 = 0; var X2 = 0; var y2 = 0; var X3 = 0; vaR Y3 = 0; If (document.doc umentelement) {x1 = document.doc umentelement. scrollleft | 0; Y1 = document.doc umentelement. scrolltop | 0;} If (document. body) {X2 = document. body. scrollleft | 0; y2 = document. body. scrolltop | 0;} var X3 = Window. scrollx | 0; var Y3 = Window. scrolly | 0; // horizontal distance from the scroll bar to the top of the page var x = math. max (x1, math. max (X2, X3); // vertical distance from the scroll bar to the top of the page var y = math. max (Y1, math. max (Y2, Y3); // rolling distance = current distance/speed, because the smaller the distance, the speed is greater than 1, therefore, the rolling distance will be smaller and smaller. var speed = 1 + acceleration; window. scrollto (math. floor (x/speed), math. floor (y/speed); // if the distance is not zero, continue to call the iteration function if (x> 0 | Y> 0) {var invokefunction = "gotop (" + acceleration + "," + time + ")"; window. setTimeout (invokefunction, time );}}
Document.doc umentelement. scrolltop, document. Body. scrolltop, window. scrolly are actually the same, but they only play a role in some browsers. You can debug which browsers play a role.
<a href="#" onclick="goTop();return false;">TOP</a>


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.