一、一个DIV设置淡入淡出
代码:
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <title>运动--淡入淡出</title>
    <!-- <link href="test_css.css" rel="stylesheet" type="text/css">
   <script type="text/javascript" src="test.js"></script>-->
    <style>
        #div {
            width: 200px;
            height: 200px;
            background: #ff2e1d;
            position: absolute;
            left: 100px;
            top: 100px;
            opacity: 0.3;
            filter: alpha(opacity:30);
        <!-- 仅支持IE浏览器 -->
        }
    </style>
    <script>
        window.onload = function () {
            var oDiv = document.getElementById("div");
            oDiv.onmouseover = function () {
                startMove(100);
            };
            oDiv.onmouseout = function () {
                startMove(30);
            };
            var alpha = 30;
            var timer = null;
            function startMove(iTarget) {
                clearInterval(timer);
                timer = setInterval(function () {
                    var speed = 0;
                    if (alpha < iTarget) {
                        speed = 10;
                    } else {
                        speed = -10;
                    }
                    if (alpha == iTarget) {
                        clearInterval(timer);
                    } else {
                        alpha += speed;
                        oDiv.style.filter = 'alpha(opacity:' + alpha + ')';
                        oDiv.style.opacity = alpha / 100;
                    }
                }, 30);
            }
        }
    </script>
</head>
<body>
<div id="div"></div>
</body>
</html>
运行结果:
初始:
鼠标移入:
鼠标移出:
二、多个DIV设置淡入淡出
代码:
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <title>无标题文档</title>
    <style>
        div {width:200px; height:200px; margin:20px; float:left; background:red; filter:alpha(opacity:30); opacity:0.3;}
    </style>
    <script>
        window.onload=function ()
        {
            var aDiv=document.getElementsByClassName("e");
            for(var i=0;i<aDiv.length;i++)
            {
                aDiv[i].alpha=30;
                aDiv[i].onmouseover=function ()
                {
                    startMove(this, 100);
                };
                aDiv[i].onmouseout=function ()
                {
                    startMove(this, 30);
                };
            }
        };
        function startMove(obj, iTarget)
        {
            clearInterval(obj.timer);
            obj.timer=setInterval(function (){
                var speed=(iTarget-obj.alpha)/6;
                speed=speed>0?Math.ceil(speed):Math.floor(speed);
                if(obj.alpha==iTarget)
                {
                    clearInterval(obj.timer);
                }
                else
                {
                    obj.alpha+=speed;
                    obj.style.filter='alpha(opacity:'+obj.alpha+')';
                    obj.style.opacity=obj.alpha/100;
                }
            }, 30);
        }
    </script>
</head>
<body>
<div class="e"></div>
<div class="e"></div>
<div class="e"></div>
<div class="e"></div>
</body>
</html>
 
 
                    
                     
                    
                 
                    
                 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号