为什么目标值是400,可是元素只到393就一直死循环,
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
ul li {
list-style: none;
}
ul li {
width: 200px;
height: 100px;
background: yellow;
margin-bottom: 20px;
border: 4px solid #00ffff;
}
</style>
<script type="text/javascript">
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, 400);
}
aLi[i].onmouseout = function() {
startMove(this, 200);
}
}
}
// var timer=null;
function startMove(obj, target) {
clearInterval(obj.timer);
obj.timer = setInterval(function() {
var icur = parseInt(getStyle(obj, 'width'));
var speed = (target - icur) / 8;
speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
if(icur == target) {
clearInterval(obj.timer);
console.log(obj.style.width)
} else {
obj.style.width = icur + speed + 'px';
}
}, 30)
}
function getStyle(obj, attr) {
if(obj.currentStyle) {
return obj.currentStyle[attr];
} else {
return getComputedStyle(obj, false)[attr];
}
}
</script>
</head>
<body>
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</body>
</html>