js运动之"焦点图"

2018-01-17  本文已影响0人  RL空RLR空L
ed.jpg
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #div1 {
            width: 200px;
            height: 200px;
            background:red;
        /*兼容性问题,filter:alpha(opacity:30);给IE。opacity:0.3;给火狐谷歌用。*/
            filter:alpha(opacity:30);
            opacity:0.3;
        }
    </style>
    <script>
        window.onload=function(){
            var oDiv=document.getElementById('div1');
            oDiv.onmouseover=function(){
                startMove(100);
            };
            oDiv.onmouseout=function(){
                startMove(30);
            };
        };
        var alpha=30;
        var timer=null;
        function startMove(iTarget){
            var oDiv=document.getElementById('div1');
            clearInterval(timer);
            timer=setInterval(function(){
                var speed=0;
                if(alpha<iTarget){
                    speed=5;
                }else{
                    speed=-5;
                }
                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="div1"></div>
</body>
</html>
上一篇下一篇

猜你喜欢

热点阅读