猿问

请问这哪里错了

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>透明度动画</title>
    <style type="text/css">
        .div{
            width: 300px;
            height: 300px;
            background-color: blue;
            opacity: 0.5;
        }
    </style>
    <script type="text/javascript">

        window.onload=function(){
            var div = document.getElementById("div")
            div.onmouseover=function(){
                startMove(1);
            }
            div.onmouseout=function(){
                startMove(0.5);
            }
        }
        var timer;
        var opacity=0.5;
        function startMove(itarget){
            clearInterval(timer)
            var div = document.getElementById("div")
            timer = setInterval(function(){
                var speed = 0;
                if ( div.style.opacity>itarget){
                    speed=-0.5;
                }else {
                    speed=0.5;
                }
                if(opacity==itarget){
                    clearInterval(timer)
                }else {
                    opacity +=speed;
                    div.style.opacity=opacity;
                }
            },30)
        }
    </script>
</head>
<body>
 <div class="div"></div>
</body>
</html>


梦要够疯才能变成英雄雄雄
浏览 1146回答 1
1回答

木子舟义

你既然用的 getElementById('div')  那你倒是给DIV一个ID值撒。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答