requestAnimationFrame : 调用这个方法,就是告诉浏览器要执行动画了,从而浏览器自动计算动画时间间隔,从而在恰当的时候刷新UI, 动画更加平滑。

他的用法和setTimeout() 一致,只是不用告诉浏览器循环间隔,而是让浏览器自己决定,各大浏览器都会充分利用自己的性能。

创建一个简单的动画,

function animation(){
     var div = document.getElementById("div1");
     div.style.left = div.offsetLeft + 10 + "px";
}

然后调用requestionAnimationFrame 方法,他接受一个参数,就是动画函数。

requestAnimationFrame(animation);

执行函数后,浏览器重绘DOM, div 向右移动10px,这时就停止了。如果再次调用 这个函数,div 又向右移动10px; 也就是说每执行一个requestAnimationFrame(animation); div 就向右移动10px;

因此创建循环动画,就需要在 动画函数中调用 requestAnimationFrame(animation);

function animation(){
    var div =document.getElementById("div1");
    div.style.left = div.offsetLeft +10 +"px";
    requestAnimationFrame(animation);
}
 requestAnimationFrame(animation);  

其次 还提供了一个cancelAnimationFrame方法,它接受一个参数,用于取消动画。 参数就是 requestAnimationFrame(animation) 的返回值,和setTimeout 返回值一样。

完整的requestAnimationFrame动画。

<div id="div1">
    ddd
</div>
<script>
    var timer = null;
    var div = document.getElementById("div1");
    function animation(){
        div.style.left = div.offsetLeft + 10 + "px";
        if(div.offsetLeft > 800){
            cancelAnimationFrame(timer);
        }else {
            requestAnimationFrame(animation);
        }
    }
    timer =requestAnimationFrame(animation);
</script>

requestAnimationFrame 动画的更多相关文章

  1. 移动端 transition动画函数的封装(仿Zepto)以及 requestAnimationFrame动画函数封装(仿jQuery)

    移动端 css3 transition 动画 ,requestAnimationFrame 动画  对于性能的要求,h5优先考虑: 移动端 单页有时候 制作只用到简单的css3动画即可,我们封装一下, ...

  2. requestAnimationFrame动画方法

    一.动画方式 在HTML5/CSS3时代,实现动画的方式有许多种: 你可以用css3的animation和@keyframes: 可以用css3的transition: 还可以用原始的setTimeo ...

  3. Window.requestAnimationFrame()动画更新

    概述 Window.requestAnimationFrame()方法告诉浏览器你希望执行动画,并且再下一次重绘之前要求浏览器调用一个特定的函数去更新动画.该方法把一个回调函数作为参数,该回调函数会在 ...

  4. requestAnimationFrame动画封装

    function Animator(duration, progress) { this.duration = duration; this.progress = progress; this.nex ...

  5. 简单的requestAnimationFrame动画

    html部分 <div id="test" style="width:1px;height:17px;background:#0f0;">0%< ...

  6. 移动端 touchmove高频事件与requestAnimationFrame的结合优化

    移动端最高频耗内存的的操作  莫属 touchmove 与scroll事件  两者需要 微观的 优化,使用 requestAnimationFrame性能优化 H5性能优化requestAnimati ...

  7. Html5游戏开发-图形与动画(一)

    最近研究了一下出来了很久的HTML5,总结了一下,准备来个系列,文中也许有很多问题,欢迎大家指正. Canvas介绍 canvas用于在网页中绘制图形的一个元素,具体内容请查看 -> HTML5 ...

  8. Canvas动画基础之碰撞检测

    在Canvas中进行碰撞检测,大家往往直接采用游戏引擎(Cocos2d-JS.Egret)或物理引擎(Box2D)内置的碰撞检测功能,好奇的你有思考过它们的内部运行机制吗?下面将针对基本的碰撞检测技术 ...

  9. Html5 Canvas动画基础碰撞检测的实现

    在Canvas中进行碰撞检测,大家往往直接采用游戏引擎(Cocos2d-JS.Egret)或物理引擎(Box2D)内置的碰撞检测功能,好奇的你有思考过它们的内部运行机制吗?下面将针对基本的碰撞检测技术 ...

随机推荐

  1. 任务中使用wget,不保存文件

    */20 * * * * wget --output-document=/dev/null http://www.domain.com 使用wget每过20分钟访问一次,不保存访问文件内容

  2. Given a compiled machine-language program, which statements in the source language cause the execution of the most machine-language instructions and what is the execution time of these instr

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION A  variety  of  studi ...

  3. iOS图片压缩处理

    理解概念 首先,我们必须明确图片的压缩其实是两个概念: “压” 是指文件体积变小,但是像素数不变,长宽尺寸不变,那么质量可能下降. “缩” 是指文件的尺寸变小,也就是像素数减少,而长宽尺寸变小,文件体 ...

  4. Maven-010-maven 编译报错:Failure to ... in ... was cached in the local repository, resolution will not be reattempted until the update interval of nexus has elapsed or updates are forced.

    今晚在编译 maven 项目的时候,命令行报错,出现 Failure to ... in ... 类似错误,详细的错误信息如下所示: [INFO] -------------------------- ...

  5. angularJs之template指令

    template: 如果我们只需要在ng-view 中插入简单的HTML 内容,则使用该参数: .when('/computers',{template:'这是电脑分类页面'}) templateUr ...

  6. dump redo日志文件的信息

    通常会用到以下两个命令:1.'alter session'命令用来dump redo日志的文件头2.'alter system dump logfile'命令用来dump redo文件的内容 以上命令 ...

  7. LeetCode: Nim Game

    这题其实不太好想,用到了博弈论 1,2,3 能赢 4 输 5,6,7 赢 8 输 9,10,11 赢 12 输 那么结论就是4的倍数就是输,其他情况就能赢. 为什么会是这样呢?很好解释,根源就在4会输 ...

  8. 安装第三方库出现 Python version 2.7 required, which was not found in the registry

    安装第三方库出现 Python version 2.7 required, which was not found in the registry 建立一个文件 register.py 内容如下. 然 ...

  9. Qt之qt4.7 和qt 4.8.4 交叉实践

    开发机环境搭建: 测试环境:CentOs7.1  Ubuntu 12.0.4 操作流程: 一.编译Qt4.7.0 1)CentOS上实践 1.tar xzvf qt-everywhere-openso ...

  10. dataURI V.S. CSS Sprites 移动端

    英文原文:http://www.mobify.com/blog/css-sprites-vs-data-uris-which-is-faster-on-mobile/ 中文翻译:http://www. ...