forked from ygs-code/vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequestAnimationFrame.html
64 lines (56 loc) · 1.77 KB
/
requestAnimationFrame.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<style>
#SomeElementYouWantToAnimate{
width: 100px;
height: 100px;
background: red;
}
</style>
<body>
<div id="SomeElementYouWantToAnimate">
</div>
<script>
var start = null;
var element = document.getElementById('SomeElementYouWantToAnimate');
element.style.position = 'absolute';
/*
window.requestAnimationFrame() 告诉浏览器——你希望执行一个动画,
并且要求浏览器在下次重绘之前调用指定的回调函数更新动画。
该方法需要传入一个回调函数作为参数,
该回调函数会在浏览器下一次重绘之前执行
注意:若你想在浏览器下次重绘之前继续更新下一帧动画,
那么回调函数自身必须再次调用window.requestAnimationFrame()
*/
function step(timestamp) {
console.log('step')
if (!start) start = timestamp;
var progress = timestamp - start;
element.style.left = Math.min(progress / 10, 200) + 'px';
if (progress < 2000) {
window.requestAnimationFrame.bind(window)(step);
}
}
window.requestAnimationFrame(step);
var inBrowser=true;
var raf = inBrowser ?
(window.requestAnimationFrame ? window.requestAnimationFrame.bind(window) : setTimeout)
: function (fn) {
return fn();
};
//下一帧
function nextFrame(fn) {
raf(function () {
raf(fn);
});
}
nextFrame(function () {
console.log('nextFrame')
})
</script>
</body>
</html>