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

练习js透明度动画的时候报了这样的错误:part2-opacity.html?_ijt=p2ei6la3k2jfia9gtb6od1s006:27 Uncaught TypeError: Cannot set property 'onmouseover' of null at window.onload

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>透明度动画</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        .block{
            width: 80px;
            height: 80px;
            filter:alpha(30);
            opacity: 0.3;
            background: #f00;
        }
    </style>
</head>
<body>
<div class="block"></div>

<script>
    window.onload = function() {
        var oDiv = document.getElementById('block');

        oDiv.onmouseover=function(){
            startMove(100);
        };
        oDiv.onmouseout=function(){
            startMove(30);
        };
    };

    var speed = 0;
    var alpha = 30;
    var timer = null;
    function startMove(iTarget){
        var oDiv = document.getElementById('block');
        clearInterval(timer);
        timer = setInterval(function(){
            if(oDiv.alpha > iTarget){
                speed = -10;
            }else{
                speed = 10;
            }
            if(oDiv.alpha == iTarget){
                clearInterval(timer);
            }else{
                alpha += speed;
                oDiv.style.filter = 'alpha(opacity:'+alpha+')'; // 兼容IE
 oDiv.style.opacity = alpha/100;  // Firefox Chrome
 }
        },30);
    }

</script>
</body>
</html>


正在回答

1 回答

大哥,你这是class="block",是类选择器啊,只能用document.getElementsByClassName("block")[0]来获取啊,还要看浏览器是否支持getElementsByClassName方法,不支持的话可以创建该方法

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

Number_1 提问者

怎么创建该方法呢
2018-06-14 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

练习js透明度动画的时候报了这样的错误:part2-opacity.html?_ijt=p2ei6la3k2jfia9gtb6od1s006:27 Uncaught TypeError: Cannot set property 'onmouseover' of null at window.onload

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