为什么目标值是400,可是元素只到393就一直死循环,

来源:4-3 任意属性值(一)

_半人

2018-08-23 09:42

<!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>


写回答 关注

4回答

  • 请让我安静一会儿
    2018-12-15 13:17:07

    你在浏览器里调试一下,就很容易发现问题的,我写代码时,第一遍,总出点毛病,调式多了,就会了;

  • 请让我安静一会儿
    2018-12-15 13:15:22
    没问题啊,可以运行啊


  • 用户1109480
    2018-09-04 10:29:50

    这代码没问题,在我这运行正常呀

  • 一叶秋才
    2018-08-23 14:20:29

    你这代码没问题啊

JS动画效果

通过本课程JS动画的学习,从简单动画开始,逐步深入各种动画框架封装

113923 学习 · 1443 问题

查看课程

相似问题