js运动之"焦点图"

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>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容