12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- Math.easeInOutQuad = function(t, b, c, d) {
- t /= d / 2
- if (t < 1) {
- return c / 2 * t * t + b
- }
- t--
- return -c / 2 * (t * (t - 2) - 1) + b
- }
- var requestAnimFrame = (function() {
- return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function(callback) { window.setTimeout(callback, 1000 / 60) }
- })()
- function move(amount) {
- document.documentElement.scrollTop = amount
- document.body.parentNode.scrollTop = amount
- document.body.scrollTop = amount
- }
- function position() {
- return document.documentElement.scrollTop || document.body.parentNode.scrollTop || document.body.scrollTop
- }
- export function scrollTo(to, duration, callback) {
- const start = position()
- const change = to - start
- const increment = 20
- let currentTime = 0
- duration = (typeof (duration) === 'undefined') ? 500 : duration
- var animateScroll = function() {
-
- currentTime += increment
-
- var val = Math.easeInOutQuad(currentTime, start, change, duration)
-
- move(val)
-
- if (currentTime < duration) {
- requestAnimFrame(animateScroll)
- } else {
- if (callback && typeof (callback) === 'function') {
-
- callback()
- }
- }
- }
- animateScroll()
- }
|