requestAnimationFrame 降级写法

1,058 阅读1分钟

在写 js 动画的时候经常使用 requestAnimationFrame 这个函数,但会有一定的 兼容问题。今天在 ecomfe/zrender 看到一种降级写法,特此记录:

// requestAnimationFrame.js
export default (
  typeof window !== 'undefined'
  && (
    (window.requestAnimationFrame && window.requestAnimationFrame.bind(window))
    || (window.msRequestAnimationFrame && window.msRequestAnimationFrame.bind(window))
    || window.mozRequestAnimationFrame
    || window.webkitRequestAnimationFrame
  )
) || function (func) {
  setTimeout(func, 16);
};