Use Requestanimationframe to create animations in JS

Source: Internet
Author: User


What is Requestanimationframe?

In the browser animation program, we usually use a timer to cycle every few milliseconds to move the object once, to make it move. Now that the good news is that the browser developers have provided this requestanimationframe () API function for animation, you can use it on a DOM style change or canvas animation or WEBGL.

Example


<! DOCTYPE html>
<meta name= "viewport" content= "Width=device-width,user-scalable=no, initial-scale=1, maximum-scale=1, User-scalable=0 "/>
<meta name= "apple-mobile-web-app-capable" content= "yes"/>
<meta name= "Apple-mobile-web-app-status-bar-style" content= "Black-translucent"/>
<title> How to use Requestanimationframe to create an animation </title>
<body>
<div id= "test" style= "Width:1px;height:17px;background: #0f0;" >0%</div>
<script>

Http://paulirish.com/2011/requestanimationframe-for-smart-animating
http://www.webhek.com/requestanimationframe/
Http://www.cnblogs.com/Wayou/p/requestAnimationFrame.html
Shim layer with settimeout fallback
Window.requestanimframe = (function () {
return Window.requestanimationframe | |
Window.webkitrequestanimationframe | |
Window.mozrequestanimationframe | |
Window.orequestanimationframe | |
Window.msrequestanimationframe | |
Function (callback) {
Window.settimeout (callback, 1000/60);
};
})();

var feng={
progress:0,
Ele:null,
Test:function () {

feng.progress++;
Feng.ele=document.getelementbyid ("test");
Feng.ele.style.width = feng.progress + "%";
feng.ele.innerhtml=feng.progress + "%";

if (feng.progress<100) {
Requestanimframe (feng.test);
}
},
Init:function () {
Feng.test ();
}
}

Window.addeventlistener (' Load ', Feng.init, false);

</script>

</body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.