这个代码里面什么地方错了啊?

来源:4-2 获取样式

Betsey

2016-02-19 16:11

<!DOCTYPE html>

<html>


<head>

<meta charset="UTF-8">

<title></title>

<style>

* {

margin: 0;

padding: 0;

}

ul {

list-style: none;

}

li {

width: 200px;

height: 100px;

background: yellow;

margin-bottom: 20px;

border: 1px solid #807A62;

}

</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, iTarget) {

clearInterval(obj.timer);

obj.timer = setInterval(function() {

var speed=(iTarget-obj.offsetWidth)/10;

speed=speed>0?Math.ceil(speed):Math.floor(speed);

if (obj.offsetWidth == iTarget) {

clearInterval(obj.timer);

} else {

obj.style.width = parseInt(getStyle(obj,'width'))+ speed + 'px';

}

}, 30)

}

function getStyle(obj,attr){

if(obj.currentStyle){

return obj.currentStyle[attr];

}else{

return obj.getComputedStyle(obj,false)[attr];

}

}


</script>

</head>


<body>

<ul>

<li></li>

<li></li>

<li></li>

</ul>

</body>


</html>


写回答 关注

2回答

  • weibo_懒人壮壮_0
    2016-04-11 18:11:23

    // aLi[i].alpha=30 不要注释掉

  • Betsey
    2016-02-19 16:15:05

    <!DOCTYPE html>

    <html>


    <head>

    <meta charset="UTF-8">

    <title></title>

    <style>

    * {

    margin: 0;

    padding: 0;

    }

    ul {

    list-style: none;

    }

    li {

    width: 200px;

    height: 100px;

    background: yellow;

    margin-bottom: 20px;

    /*filter:alpha(opacity:30);*/

    /*opacity: 0.3;*/

    border: 1px solid #807A62;

    }

    </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].alpha=30

    aLi[i].onmouseover = function() {

    startMove(this, 400);

    }

    aLi[i].onmouseout = function() {

    startMove(this, 200);

    }

    }

    }

    //var timer = null;


    function startMove(obj, iTarget) {

    clearInterval(obj.timer);

    obj.timer = setInterval(function() {

    var speed=(iTarget-parseInt(getStyle(obj,'width')))/10;

    speed=speed>0?Math.ceil(speed):Math.floor(speed);

    if (parseInt(getStyle(obj,'width')== iTarget) {

    clearInterval(obj.timer);

    } else {

    obj.style.width = parseInt(getStyle(obj,'width'))+ speed + 'px';

    // obj.alpha=obj.alpha+speed;

    // obj.style.filter="alpha(opacity:"+obj.alpha+")";

    // obj.style.opacity=obj.alpha/100;

    }

    }, 30)

    }

    function getStyle(obj,attr){

    if(obj.currentStyle){

    return obj.currentStyle[attr];

    }else{

    return obj.getComputedStyle(obj,false)[attr];

    }

    }


    </script>

    </head>


    <body>

    <ul>

    <li></li>

    <li></li>

    <li></li>

    </ul>

    </body>


    </html>

    这样也运行不正确。。。。。。求指导

JS动画效果

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

113924 学习 · 1443 问题

查看课程

相似问题