diff --git a/components/_util/getRequestAnimationFrame.tsx b/components/_util/getRequestAnimationFrame.tsx new file mode 100644 index 0000000000..f75c84e289 --- /dev/null +++ b/components/_util/getRequestAnimationFrame.tsx @@ -0,0 +1,12 @@ +export default function getRequestAnimationFrame() { + if (typeof window === 'undefined') { + return () => {}; + } + if (window.requestAnimationFrame) { + return window.requestAnimationFrame; + } + const prefix = ['moz', 'ms', 'webkit'].filter(key => `${key}RequestAnimationFrame` in window)[0]; + return prefix + ? window[`${prefix}RequestAnimationFrame`] + : callback => setTimeout(callback, 1000 / 60); +} diff --git a/components/back-top/index.tsx b/components/back-top/index.tsx index e8fd369b0b..2cf436ac5d 100644 --- a/components/back-top/index.tsx +++ b/components/back-top/index.tsx @@ -5,19 +5,12 @@ import addEventListener from 'rc-util/lib/Dom/addEventListener'; import classNames from 'classnames'; import omit from 'omit.js'; import getScroll from '../_util/getScroll'; +import getRequestAnimationFrame from '../_util/getRequestAnimationFrame'; -const reqAnimFrame = (() => { - if (window.requestAnimationFrame) { - return window.requestAnimationFrame; - } - const a = ['moz', 'ms', 'webkit']; - const raf = a.filter(key => `${key}RequestAnimationFrame` in window); - return raf[0] ? window[`${raf[0]}RequestAnimationFrame`] : - ((callback) => window.setTimeout(callback, 1000 / 60)); -})(); +const reqAnimFrame = getRequestAnimationFrame(); const currentScrollTop = () => { - return window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop; + return window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop; }; const easeInOutCubic = (t, b, c, d) => {