@megalo/api
Version:
重新封装各个端中的API,由 megalo 统一对外抛出方法名。目前微信小程序端提供的 API 最为丰富,所以 API 名称以微信小程序为准。
42 lines (37 loc) • 1.04 kB
JavaScript
import { getScrollTop, setScrollTop } from '../utils/index';
let _frameRequestId = null,
_from, _to, _start, _duration;
const supportPerformance = !!(window.performance && window.performance.now);
function runScroll() {
_frameRequestId = window.requestAnimationFrame((now) => {
if (!supportPerformance) {
now = Date.now();
}
const timeUsed = now - _start;
if (timeUsed >= _duration) {
setScrollTop(_to);
_frameRequestId = null;
return;
}
const progress = timeUsed / _duration;
const curScrollTop = (_to - _from) * progress + _from;
setScrollTop(curScrollTop);
runScroll();
});
}
function pageScrollTo({ scrollTop, duration = 300}) {
_from = getScrollTop();
_to = scrollTop;
_duration = duration;
_start = supportPerformance ? window.performance.now() : Date.now();
if (_frameRequestId == null) {
runScroll();
}
}
export default {
install(Megalo) {
Object.assign(Megalo, {
pageScrollTo
});
}
};