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!