RequestAnimationFrame在Safari angular 4中不可用

时间:2018-01-02 05:32:34

标签: angular animation

因为我在角4项目中使用材料设计。该项目未在safari中加载为抛出错误 - RequestAnimationFrame。

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您可以使用polyfillsafari中使其可用。这是代码片段。

// requestAnimationFrame polyfill by @rma4ok
!function (window) {
  var
      equestAnimationFrame = 'equestAnimationFrame',
      requestAnimationFrame = 'r' + equestAnimationFrame,

      ancelAnimationFrame = 'ancelAnimationFrame',
      cancelAnimationFrame = 'c' + ancelAnimationFrame,

      expectedTime = 0,
      vendors = ['moz', 'ms', 'o', 'webkit'],
      vendor;

  while (!window[requestAnimationFrame] && (vendor = vendors.pop())) {
    window[requestAnimationFrame] = window[vendor + 'R' + equestAnimationFrame];
    window[cancelAnimationFrame] =
        window[vendor + 'C' + ancelAnimationFrame] ||
            window[vendor + 'CancelR' + equestAnimationFrame];
  }

  if (!window[requestAnimationFrame]) {
    window[requestAnimationFrame] = function (callback) {
      var
          currentTime = +new Date,
          adjustedDelay = 16 - (currentTime - expectedTime),
          delay = adjustedDelay > 0 ? adjustedDelay : 0;

      expectedTime = currentTime + delay;

      return setTimeout(function () {
        callback(expectedTime);
      }, delay);
    };

    window[cancelAnimationFrame] = clearTimeout;
  }
}(this);