Created
March 27, 2015 10:49
-
-
Save luxiaojian/3f03e1ef9f825fe34b12 to your computer and use it in GitHub Desktop.
JS动画 // source http://jsbin.com/dotoga
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>JS动画</title> | |
<style id="jsbin-css"> | |
body{ | |
padding-top:40px; | |
} | |
.square{ | |
width:40px; | |
height:40px; | |
background: red; | |
position:relative; | |
} | |
.run{ | |
position: relative; | |
top:15px; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="square j_square"></div> | |
<button class="j_run run">点击运行</button> | |
<script id="jsbin-javascript"> | |
var startTime, | |
duration=2000, | |
startVal=0, | |
targetVal=200, | |
block=document.querySelector('.square'); | |
function runAnimation(){ | |
var currentTime=performance.now(), | |
passedTime=currentTime-startTime, | |
animateProgress=passedTime/duration, | |
currentVal=startVal+(targetVal-startVal)*animateProgress; | |
block.style.left=Math.round(currentVal)+'px'; | |
if(currentVal < targetVal){ | |
requestAnimationFrame(runAnimation); | |
} | |
} | |
document.querySelector('.j_run').addEventListener('click',function(e){ | |
startTime=performance.now(); | |
runAnimation(); | |
// requestAnimationFrame(runAnimation); | |
}); | |
</script> | |
<script id="jsbin-source-css" type="text/css">body{ | |
padding-top:40px; | |
} | |
.square{ | |
width:40px; | |
height:40px; | |
background: red; | |
position:relative; | |
} | |
.run{ | |
position: relative; | |
top:15px; | |
}</script> | |
<script id="jsbin-source-javascript" type="text/javascript">var startTime, | |
duration=2000, | |
startVal=0, | |
targetVal=200, | |
block=document.querySelector('.square'); | |
function runAnimation(){ | |
var currentTime=performance.now(), | |
passedTime=currentTime-startTime, | |
animateProgress=passedTime/duration, | |
currentVal=startVal+(targetVal-startVal)*animateProgress; | |
block.style.left=Math.round(currentVal)+'px'; | |
if(currentVal < targetVal){ | |
requestAnimationFrame(runAnimation); | |
} | |
} | |
document.querySelector('.j_run').addEventListener('click',function(e){ | |
startTime=performance.now(); | |
runAnimation(); | |
// requestAnimationFrame(runAnimation); | |
});</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body{ | |
padding-top:40px; | |
} | |
.square{ | |
width:40px; | |
height:40px; | |
background: red; | |
position:relative; | |
} | |
.run{ | |
position: relative; | |
top:15px; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var startTime, | |
duration=2000, | |
startVal=0, | |
targetVal=200, | |
block=document.querySelector('.square'); | |
function runAnimation(){ | |
var currentTime=performance.now(), | |
passedTime=currentTime-startTime, | |
animateProgress=passedTime/duration, | |
currentVal=startVal+(targetVal-startVal)*animateProgress; | |
block.style.left=Math.round(currentVal)+'px'; | |
if(currentVal < targetVal){ | |
requestAnimationFrame(runAnimation); | |
} | |
} | |
document.querySelector('.j_run').addEventListener('click',function(e){ | |
startTime=performance.now(); | |
runAnimation(); | |
// requestAnimationFrame(runAnimation); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment