Skip to content

Instantly share code, notes, and snippets.

@luxiaojian
Created March 27, 2015 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luxiaojian/3f03e1ef9f825fe34b12 to your computer and use it in GitHub Desktop.
Save luxiaojian/3f03e1ef9f825fe34b12 to your computer and use it in GitHub Desktop.
JS动画 // source http://jsbin.com/dotoga
<!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>
body{
padding-top:40px;
}
.square{
width:40px;
height:40px;
background: red;
position:relative;
}
.run{
position: relative;
top:15px;
}
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