mirror of
https://github.com/youzan/vant.git
synced 2025-12-20 01:01:34 +08:00
[Improvement] Tab animation fluency && position (#379)
This commit is contained in:
32
packages/utils/raf.js
Normal file
32
packages/utils/raf.js
Normal file
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* requestAnimationFrame polyfill
|
||||
*/
|
||||
|
||||
import { isServer } from './index';
|
||||
|
||||
let prev = Date.now();
|
||||
function fallback(fn) {
|
||||
const curr = Date.now();
|
||||
const ms = Math.max(0, 16 - (curr - prev));
|
||||
const id = setTimeout(fn, ms);
|
||||
prev = curr + ms;
|
||||
return id;
|
||||
}
|
||||
|
||||
const global = isServer ? global : window;
|
||||
const iRaf =
|
||||
global.requestAnimationFrame ||
|
||||
global.webkitRequestAnimationFrame ||
|
||||
fallback;
|
||||
const iCancel =
|
||||
global.cancelAnimationFrame ||
|
||||
global.webkitCancelAnimationFrame ||
|
||||
global.clearTimeout;
|
||||
|
||||
export function raf(fn) {
|
||||
return iRaf.call(global, fn);
|
||||
}
|
||||
|
||||
export function cancel(id) {
|
||||
iCancel.call(global, id);
|
||||
}
|
||||
Reference in New Issue
Block a user