qq_Justcomplete_04021004
2017-09-08 16:57
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
ul,li{
list-style: none;
}
li{
width: 200px;
height:100px;
background: yellow;
margin-bottom: 20px;
border:1px solid black;
filter:alpha(opacity:30);
opacity:0.3 ;
}
</style>
<script>
window.onload=function () {
// var ali=document.getElementsByTagName('li');
// for(var i=0;i<ali.length;i++){
// ali[i].timer=null;//各自加一个定时器
// ali[i].onmouseover=function () {
// startmove(this,'height',300);
// }
// ali[i].onmouseout=function () {
// startmove(this, 'height', 100);
// }
// }
var li1=document.getElementById('li1');
var li2=document.getElementById('li2');
li1.onmouseover=function () {
startmove(this,'height',300);
}
li1.onmouseout=function () {
startmove(this,'height',100);
}
li2.onmouseover=function () {
startmove(this,'width',300);
}
li2.onmouseout=function () {
startmove(this,'width',100);
}
}
//var timer=null;
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
}
else{
return getComputedStyle(obj,false)[attr];
}
}
function startmove(obj,attr,target) {
//clearInterval(timer)清空的是共同的定时器,没有清空各自的定时器
clearInterval(obj.timer);
obj.timer=setInterval(function () {
var icur=parseInt(getStyle(obj,attr));
var speed=(target-icur)/8;
speed=speed>0?Math.ceil(speed):Math.floor(speed);
if(icur==target){
clearInterval(obj.timer);
}
else{
obj.style.attr=icur+speed+'px';
}
},50)
}
</script>
</head>
<body>
</body>
<ul>
<li id="li1"></li>
<li id="li2"></li>
</ul>
</html>
obj.style.attr=icur+speed+'px';改为 obj.style[attr]=icur+speed+'px';自己找到问题所在了
JS动画效果
113925 学习 · 1443 问题
相似问题