为了账号安全,请及时绑定邮箱和手机立即绑定

为什么我的效果出不来啊?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{
            margin:0;
            padding:0;
        }
        ul{
            list-style: none;
        }
        ul li{
            width:200px;
            height: 80px;
            background: red;
            margin-bottom: 20px;
            border: 4px solid black;
            opacity: 0.3;
            filter: alpha(opacity:30);
        }
    </style>
    <script>
        window.onload=function(){
            var Li = document.getElementsByTagName("li");
                Li.onmouseover = function(){
                    startMove(this,'width',400);
                };
                Li.onmouseout = function() {
                    startMove(this, 'width', 200);
                };
            var timer = null;

            function startMove(obj,attr,target){
                    clearInterval(timer);
                    timer = setInterval(function(){
                    var icur = 0;
                        if(attr == 'opacity'){
                            icur = parseFloat(getStyle(obj,attr))*100;
                        }else{
                            icur = parseInt(getStyle(obj,attr));
                        }

                    var speed = (target - icur)/8;
                        speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
                    if(icur == target){
                        clearInterval(timer);
                    }
                    else{
                        if(attr == 'opacity'){
                            obj.style.filter == 'alpha(opacity:'+ (icur + speed)+')';
                            obj.style.opacity == (icur + speed)/100 ;
                        }else {
                            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 回答

可是当我把调用里的'width'该为'opacity'的时候,效果又出不来了。(target已改)

0 回复 有任何疑惑可以回复我~
#1

一朵无畏的小花

obj.style.filter = 'alpha(opacity:'+ (icur + speed)+')'; obj.style.opacity = (icur + speed)/100 ;是赋值不是相等
2015-12-31 回复 有任何疑惑可以回复我~

你好,document.getElementsByTagName(); 获取的是一组数,所以要像取数组里的元素来取,因此要这么写:

document.getElementsByTagName("li")[0];

望楼主采纳!

0 回复 有任何疑惑可以回复我~
var Li = document.getElementsByTagName("li")[0];       //少了个[0]


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

为什么我的效果出不来啊?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信