求大神帮忙看一下,看了一下午就是不知道哪里出错了,求助!!!

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

qq_Mark单_0

2016-09-16 16:55

<!DOCTYPE html>
<html>
<head>
    <title>透明度的getSyle方法的切换</title>
    <style type="text/css">
    *{margin: 0;padding: 0;}
        ul,li{list-style: none;}
        ul li{
            width: 200px;
            height: 200px;
            background: yellow;
            filter: alpha(opacity:30);
            opacity: 0.3;
            }
    </style>
    <script type="text/javascript">
        window.onload=function(){
            var ali=document.getElementsByTagName('li');

            ali.onmouseover=function(){
                Move(this,100,'opacity');
            }
            ali.onmouseout=function(){
                Move(this,30,'opacity');
            }
        }

        function Move(obj,iTarget,attr){
            clearInterval(obj.timer);
            obj.timer=setInterval(function(){
                var icur=0;
                if(attr=='opacity'){
                    icur=parseFloat(getStyle(obj,attr))*100;
                }else{
                    icur=parseInt(getStyle(obj,attr));
                }
                var speed=(iTarget-icur)/8;
                speed=speed>0?Math.ceil(speed):Math.floor(speed);
                if(icur==iTarget){
                    clearInterval(obj.timer);
                }else{
                    if(attr=='opacity'){
                        obj.style.filter='alpha(opacity:'+(icur+speed)+')';
                        obj.style.opacity=(icur+speed)/100;
                    }ele{
                        obj.style[attr]=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>
</ul>
</body>
</html>


写回答 关注

3回答

  • QQ_hu
    2016-09-16 21:52:52
    已采纳

    1.45行的else拼写错了

    2.第20行的ali应该是一个数组,应该写ali[0],或者通过for循环遍历。

    qq_Mar...

    非常感谢!

    2016-09-17 00:22:42

    共 1 条回复 >

  • tracylqd
    2016-09-25 00:23:08

     if(attr=='opacity'){

                            obj.style.filter='alpha(opacity:'+(icur+speed)+')';

                            obj.style.opacity=(icur+speed)/100;

                        }ele{

                            obj.style[attr]=icur+speed+'px';

    少两个else 少了个s

  • awakeawake
    2016-09-16 22:37:09

    (1)ele{

                            obj.style[attr]=icur+speed+'px';

                        }

    else少了个s;

    (2)getElementsByTagName('li')获取到的是li的数组,使用ali[0]调用函数才行

JS动画效果

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

113931 学习 · 1443 问题

查看课程

相似问题