Problems during numerical calculation using an animation function written in native JavaScript
I tried to write a code when learning the MOOC online JavaScript animation effect. But I found several problems:
Non-uniform animation velocity
Animation duration was not able to be controlled
Other actions started before the animation reached the terminating condition (especially during transparency changes. The super-long decimal point calculation – as many as eight to nine decimal places – of JavaScript just left me speechless.)
Can somebody help me modify the code? Thanks so much.
If somebody can write a concise native JavaScript animation function that enables interaction of multiple attributes for reference, that would be even more awesome. (Please do make the animation duration controllable and animation termination strictly following the given conditions.)
Here is the code:
I tried to write a code when learning the MOOC online JavaScript animation effect. But I found several problems:
Non-uniform animation velocity
Animation duration was not able to be controlled
Other actions started before the animation reached the terminating condition (especially during transparency changes. The super-long decimal point calculation – as many as eight to nine decimal places – of JavaScript just left me speechless.)
Can somebody help me modify the code? Thanks so much.
If somebody can write a concise native JavaScript animation function that enables interaction of multiple attributes for reference, that would be even more awesome. (Please do make the animation duration controllable and animation termination strictly following the given conditions.)
Here is the code:
Code:
<!doctype html>
<html lang="en">
<head>
<meta http-equiv='content-type' content='text/html;charset=utf-8' />
<script>
/*--Get the ID--*/
function $id(id){
return document.getElementById(id);
}
/*----Get the element style attributes--*/
function $gs(ele,attr){
if (ele.currentStyle)
{
return ele.currentStyle[attr];
}else if (ele.style[attr]!==''){
return ele.style[attr];
}else{
if (window.getComputedStyle(ele,null)[attr]!=='auto')
{
return window.getComputedStyle(ele,null)[attr];
}else{
return 0;
}
}
}
// Multi-attribute animation
function animate(ele,time,json,fn){
window.clearInterval(ele.timer);
var fps=60/1000;
ele.timer=window.setInterval(function(){
for (var a in json)
{
var curVal=(a.search('opacity')!==-1) ? parseFloat($gs(ele,a)) :parseInt($gs(ele,a)); // $gs 是自定义的获取元素某样式值德函数
var unit=(a.search('opacity')!==-1) ? '' : 'px';
var speed=(json[a]-curVal)/(fps*time);
if (speed>=0){
if (curVal>=json[a]){
window.clearInterval(ele.timer);
ele.style[a]=json[a]+unit;
if (fn){
fn();
}
}else{
ele.style[a]=curVal+speed+unit;
}
}else{
if (curVal<=json[a]+0.05){
window.clearInterval(ele.timer);
ele.style[a]=json[a]+unit;
if (fn){
fn();
}
}else{
ele.style[a]=curVal+speed+unit;
}
}
}
},1/fps);
}
</script>
</head>
<body>
<style>
#animate{width:200px;height:200px;background-color:blue;}
</style>
<div id='animate'></div>
<button id='pos'>Start the multi-attribute interaction animation </button>
<script>
$id('pos').onclick=function(){ // $id(id) is the custom function for quick retrieval of elements
animate($id('animate'),1000,{width:500,opacity:0},function(){
animate($id('animate'),1000,{width:200,opacity:1});
});
}
</script>
</body>
</html>
Comment