琴&
2019-01-16 20:03
采纳率: 75%
浏览 498
已采纳

为什么设置透明度的功能并没有实现?

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        #div1 {
            width: 100px;
            height: 100px;
            background: red;
            filter: alpha(opacity=30);
            opacity: 0.3;
        }
    </style>
    <script>
        window.onload = function () {
            var oDiv = document.getElementById('div');
            var i = 0;

            oDiv.timer = null;

            oDiv.onmouseover = function () {
                startMove(oDiv, 'opacity', 100)
            }
            oDiv.onmouseout = function () {
                startMove(oDiv, 'opacity', 30)
            }
        }

        function getStyle(obj, attr) {
            if (obj.currentStyle) {
                return obj.currentStyle[attr];
            } else {
                return getComputedStyle(obj, false)[attr];
            }
        }

        function startMove(obj, attr, iTarget) {
            clearInterval(obj.timer);
            obj.timer = setInterval(function () {
                var iCur = 0;

                if (atrr == 'opacity') {
                    iCur == parseInt(parseFloat(getStyle(obj, attr)) * 100);
                } else {
                    iCur == parseInt(getStyle(obj, attr));
                }
                var iSpeed = (iTarget - iCur) / 8;
                iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);

                if (iCur == iTarget) {
                    clearInterval(obj.timer);
                } else {
                    if (attr == 'opacity') {
                        obj.style.filter = 'alpha(opacity=' + (iCur + iSpeed) + ')'; //优先级
                        obj.style.opacity = (iCur + iSpeed) / 100;
                    } else {
                        obj.style[attr] = iCur + iSpeed + 'px';
                    }
                }
            }, 30)
        }
    </script>
</head>

<body>
    <div id="div1"></div>
</body>

</html>
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • 天际的海浪 2019-01-16 20:54
    最佳回答

    写代码不认真,又不懂看浏览器错误信息

            window.onload = function () {
                var oDiv = document.getElementById('div1');//-----------少 1
                var i = 0;
    
                oDiv.timer = null;
    
                oDiv.onmouseover = function () {
                    startMove(oDiv, 'opacity', 100)
                }
                oDiv.onmouseout = function () {
                    startMove(oDiv, 'opacity', 30)
                }
            }
    
            function getStyle(obj, attr) {
                if (obj.currentStyle) {
                    return obj.currentStyle[attr];
                } else {
                    return getComputedStyle(obj, false)[attr];
                }
            }
    
            function startMove(obj, attr, iTarget) {
                clearInterval(obj.timer);
                obj.timer = setInterval(function () {
                    var iCur = 0;
    
                    if (attr == 'opacity') {//---------------你写成 atrr
                        iCur = parseInt(parseFloat(getStyle(obj, attr)) * 100);//---------------赋值是一个 =
                    } else {
                        iCur = parseInt(getStyle(obj, attr));//--------------------赋值是一个 =
                    }
                    var iSpeed = (iTarget - iCur) / 8;
                    iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
    
                    if (iCur == iTarget) {
                        clearInterval(obj.timer);
                    } else {
                        if (attr == 'opacity') {
                            obj.style.filter = 'alpha(opacity=' + (iCur + iSpeed) + ')'; //优先级
                            obj.style.opacity = (iCur + iSpeed) / 100;
                        } else {
                            obj.style[attr] = iCur + iSpeed + 'px';
                        }
                    }
                }, 30)
            }
    
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题