JS使用window.requestAnimationFrame()实现逐帧动画

2022-07-14,,,

window.requestanimationframe() 方法告诉浏览器您希望执行动画,并请求浏览器调用指定的函数在下一次重绘之前更新动画。该方法使用一个回调函数作为参数,这个回调函数会在浏览器重绘之前调用。

如果你想做逐帧动画的时候,你应该用这个方法。这就要求你的动画函数执行会先于浏览器重绘动作。通常来说,被调用的频率是每秒60次,但是一般会遵循w3c标准规定的频率。如果是后台标签页面,重绘频率则会大大降低。

基本语法:

requestid = window.requestanimationframe(callback);  // firefox 23 / ie10 / chrome / safari 7 (incl. ios)
requestid = window.mozrequestanimationframe(callback);  // firefox < 23
requestid = window.webkitrequestanimationframe(callback); // older versions chrome/webkit 

浏览器支持情况:

下面通过两个简单的demo介绍下window.requestanimationframe() 方法的使用

返回顶部

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>返回顶部</title>
  <style>
    .box {
      margin: 0 auto;
      width: 100%;
      height: 5000px;
    }
 
    .box1 {
      background: #b94a48;
    }
 
    .box2 {
      background: #fb8c00;
    }
 
    .box3 {
      background: #669900;
    }
 
    .box4 {
      background: #c0a16b;
    }
 
    .top {
      position: fixed;
      right: 20px;
      bottom: 20px;
      width: 40px;
      height: 40px;
      background: #8dc7ff;
      border-radius: 50%;
      cursor: pointer;
    }
  </style>
  <script>
    window.requestanimationframe = (function () {
      return window.requestanimationframe ||
        window.webkitrequestanimationframe ||
        window.mozrequestanimationframe ||
        function (callback) {
          window.settimeout(callback, 6000 / 60)
        }
    })()
 
    window.cancelanimframe = (function () {
      return window.cancelanimationframe ||
        window.webkitcancelanimationframe ||
        window.mozcancelanimationframe ||
        window.ocancelanimationframe ||
        window.mscancelanimationframe ||
        function (callback) {
          window.cleartimeout(callback)
        }
    })()
 
    function scrolltotop() {
      let top = window.pageyoffset
      const duration = 320
      const step = top / (duration / (1000 / 60)) >> 0
      const fn = () => {
        if (top >= 0) {
          top -= step
          window.scrollto(0, top)
          fn.raftimer = window.requestanimationframe(fn)
        } else {
          window.scrollto(0, 0)
          window.cancelanimationframe(fn.raftimer)
        }
      }
      fn.raftimer = window.requestanimationframe(fn)
    }
  </script>
</head>
<body>
<div class="box box1"></div>
<div class="box box2"></div>
<div class="box box3"></div>
<div class="box box4"></div>
<div class="top" onclick="scrolltotop()"></div>
</body>
</html>

锚点定位

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>锚点定位</title>
  <style>
    .top {
      margin: 100px auto;
      cursor: pointer;
    }
 
    .top1 {
      color: #b94a48;
    }
 
    .top2 {
      color: #fb8c00;
    }
 
    .top3 {
      color: #669900;
    }
 
    .top4 {
      color: #c0a16b;
    }
 
    .box {
      margin: 0 auto;
      width: 100%;
      height: 5000px;
    }
 
    .box1 {
      background: #b94a48;
    }
 
    .box2 {
      background: #fb8c00;
    }
 
    .box3 {
      background: #669900;
    }
 
    .box4 {
      background: #c0a16b;
    }
 
  </style>
  <script>
    window.requestanimationframe = (function () {
      return window.requestanimationframe ||
        window.webkitrequestanimationframe ||
        window.mozrequestanimationframe ||
        function (callback) {
          window.settimeout(callback, 6000 / 60)
        }
    })()
 
    window.cancelanimframe = (function () {
      return window.cancelanimationframe ||
        window.webkitcancelanimationframe ||
        window.mozcancelanimationframe ||
        window.ocancelanimationframe ||
        window.mscancelanimationframe ||
        function (callback) {
          window.cleartimeout(callback)
        }
    })()
 
    function goposition(index) {
      let top = 0
      let distance = document.getelementbyid(index).offsettop
      const duration = 320
      const step = distance / (duration / (1000 / 60)) >> 0
      const fn = () => {
        if (distance >= top) {
          top += step
          window.scrollto(0, top)
          fn.raftimer = window.requestanimationframe(fn)
        } else {
          window.cancelanimationframe(fn.raftimer)
        }
      }
      fn.raftimer = window.requestanimationframe(fn)
    }
 
  </script>
</head>
<body>
<div class="top top1" onclick="goposition('box1')">跳到第一个</div>
<div class="top top2" onclick="goposition('box2')">跳到第二个</div>
<div class="top top3" onclick="goposition('box3')">跳到第三个</div>
<div class="top top4" onclick="goposition('box4')">跳到第四个</div>
<div id="box1" class="box box1"></div>
<div id="box2" class="box box2"></div>
<div id="box3" class="box box3"></div>
<div id="box4" class="box box4"></div>
</body>
</html>

网上技术大牛针对浏览器兼容封装源码:

// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
 
// requestanimationframe polyfill by erik möller. fixes from paul irish and tino zijdel
 
// mit license
 
(function() {
    var lasttime = 0;
    var vendors = ['ms', 'moz', 'webkit', 'o'];
    for(var x = 0; x < vendors.length && !window.requestanimationframe; ++x) {
        window.requestanimationframe = window[vendors[x]+'requestanimationframe'];
        window.cancelanimationframe = window[vendors[x]+'cancelanimationframe']
                                   || window[vendors[x]+'cancelrequestanimationframe'];
    }
  
    if (!window.requestanimationframe)
        window.requestanimationframe = function(callback, element) {
            var currtime = new date().gettime();
            var timetocall = math.max(0, 16 - (currtime - lasttime));
            var id = window.settimeout(function() { callback(currtime + timetocall); },
              timetocall);
            lasttime = currtime + timetocall;
            return id;
        };
  
    if (!window.cancelanimationframe)
        window.cancelanimationframe = function(id) {
            cleartimeout(id);
        };
}());

到此这篇关于js使用window.requestanimationframe()实现逐帧动画的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持。

《JS使用window.requestAnimationFrame()实现逐帧动画.doc》

下载本文的Word格式文档,以方便收藏与打印。