JS ScrollTop arrive at the specified location!

Source: Internet
Author: User

Very early on to share this experience, Pyper can be completed here today, well, words do not say, into the topic:

The method mainly uses the ScrollTop value to do the movement, is used to arrive at the user to specify the position (for example returns the parameter target to the top to set to 0 can ), has handled various situations such as scrolltop > the target value upward movement, and so on 4 kinds of situation, the code and the usage paste,

GoTo = function (target) {var scrollt = document.body.scrolltop| | document.documentElement.scrollTopif (Scrollt > Target) {var timer = setinterval (function () {var scrollt = document.body.scrolltop| | Document.documentElement.scrollTopvar step = Math.floor (-SCROLLT/6);d Ocument.documentElement.scrollTop = Document.body.scrollTop = step + scrollt;if (scrollt <= target) {Document.body.scrollTop = Document.documentElement.scrollTop = target;cleartimeout (timer);}},20)}else if (scrollt = = 0) {var timer = setinterval ( function () {var scrollt = document.body.scrolltop| | Document.documentElement.scrollTopvar step = math.floor (300/3*0.7) ;d Ocument.documentElement.scrollTop = Document.body.scrollTop = step + scrollt;console.log (scrollt) if (Scrollt >= Target) {Document.body.scrollTop = Document.documentElement.scrollTop = target;cleartimeout (timer);}},20)}else if ( Scrollt < target) {var timer = setinterval (function () {var scrollt = document.body.scrolltop| | Document.documentElement.scrollTopvar Step = MAth.floor (SCROLLT/6);d ocument.documentElement.scrollTop = Document.body.scrollTop = step + scrollt;if (scrollt >= Target) {Document.body.scrollTop = Document.documentElement.scrollTop = target;cleartimeout (timer);}},20)}else if ( target = = Scrollt) {return false;}}

Usage function (target)//Current unique target (destination distance number),

On (GOPs, ' click ',function() {GoTo (2450)});//movement to the scrolltop value is 2450 position, the following is also the same, moving to the specified positionOn (gojob, ' click ',function() {GoTo (3373)}) on (Gotel,' Click ',function() {Buffer.goto (3373)}) on (GoMe,' Click ',function() {Buffer.goto (1539)}) on (GoHome,' Click ',function() {Buffer.goto (0}); On (Scrollgoone,' Click ',function() {Buffer.goto (2450}); On (Scrollgopc,' Click ',function() {Buffer.goto (2450}); On (Scrolljob,' Click ',function() {Buffer.goto (3373}); On (Scrollme,' Click ',function() {Buffer.goto (1539}); On (Gotop,' Click ',function() {Buffer.goto (0)})

JS ScrollTop arrive at the specified location!

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.