mirror of
https://github.com/ant-design/ant-design.git
synced 2024-11-25 19:50:05 +08:00
3789e0cbbb
* Affix listener bug fix + close #4755 + close #4760 + clearScrollEventListeners before setTargetEventListeners + add tests for throttle + append affix test case * genMockFn -> fn()
45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
const availablePrefixs = ['moz', 'ms', 'webkit'];
|
|
|
|
function requestAnimationFramePolyfill() {
|
|
let lastTime = 0;
|
|
return function(callback) {
|
|
const currTime = new Date().getTime();
|
|
const timeToCall = Math.max(0, 16 - (currTime - lastTime));
|
|
const id = window.setTimeout(function() { callback(currTime + timeToCall); }, timeToCall);
|
|
lastTime = currTime + timeToCall;
|
|
return id;
|
|
};
|
|
}
|
|
|
|
export default function getRequestAnimationFrame() {
|
|
if (typeof window === 'undefined') {
|
|
return () => {};
|
|
}
|
|
if (window.requestAnimationFrame) {
|
|
return window.requestAnimationFrame;
|
|
}
|
|
|
|
const prefix = availablePrefixs.filter(key => `${key}RequestAnimationFrame` in window)[0];
|
|
|
|
return prefix
|
|
? window[`${prefix}RequestAnimationFrame`]
|
|
: requestAnimationFramePolyfill();
|
|
}
|
|
|
|
export function cancelRequestAnimationFrame(id) {
|
|
|
|
if (typeof window === 'undefined') {
|
|
return null;
|
|
}
|
|
if (window.cancelAnimationFrame) {
|
|
return window.cancelAnimationFrame(id);
|
|
}
|
|
const prefix = availablePrefixs.filter(key =>
|
|
`${key}CancelAnimationFrame` in window || `${key}CancelRequestAnimationFrame` in window
|
|
)[0];
|
|
|
|
return prefix ?
|
|
(window[`${prefix}CancelAnimationFrame`] || window[`${prefix}CancelRequestAnimationFrame`]).call(this, id)
|
|
: clearTimeout(id);
|
|
}
|