About the solution that the Web mobile end returns to the top

Source: Internet
Author: User

On the PC we can easily use scrolltop () to achieve the flow of the upward scrolling back to the top of the animation, and then use the Web mobile but no eggs, there will be scrolling is not smooth, lag, failure and so on.

This is because the mobile side of the scroll event trigger is not frequent, it is possible to detect a situation with <=0

To this end, the number of mobile judgment is better,

Here is the specific implementation code, compatible with the PC:

Return Top animation//gotop (+);//500ms roll back to top function gotop (times) {if (!!!  Times) {$ (window). scrolltop (0); Return } var sh=$ (' body '). scrolltop ();//Mobile Total distance var inter=13.333;//ms, each move interval var forcount=math.ceil (times/inter);//number of moves Var Stepl=math.ceil (Sh/forcount);//move step var timeid=null; function ani () {!!  Timeid&&cleartimeout (Timeid);  Timeid=null;  Console.log ($ (' body '). scrolltop ()); if ($ (' body '). ScrollTop () <=0| |   forcount<=0) {//Mobile judgment number is better, because the mobile side of the scroll event trigger is not frequent, it is possible to detect the <=0 of the case $ (' body '). scrolltop (0);  Return  } forcount--;  SH-=STEPL;  $ (' body '). scrolltop (SH); Timeid=settimeout (function () {ANI ();},inter); } ani ();}

About the solution that the Web mobile end returns to the top

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.