继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

JavaScript动画--透明度

冲啊
关注TA
已关注
手记 12
粉丝 5
获赞 605
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>透明度</title>
<style type="text/css">
*{
    padding:0;
    margin:0;
}

#div1{
    width:200px;
    height:200px;
    background:red;
    filter:Alpha(opacity:30);
    opacity:0.3;
    position:relative;
    top:0;
    left:200px;
}

#div1 span{
    width:50px;
    height:50px;
    position:absolute;
    top:50px;
    left:50px;
    padding:25px;
    color:blue;
    font-weight:bold;
    text-align:center;
    line-height:50px;

}
</style>

<script type="text/javascript">
window.onload=function(){
    var ODiv = document.getElementById('div1');
    ODiv.onmouseover= function(){
        startMove(100);
    }
    ODiv.onmouseout = function(){
        startMove(30);
    }
}

var timer = null;
var alpha = 30;
function startMove(iTarget){
    var ODiv =  document.getElementById('div1');
    window.clearInterval(timer);
    timer = window.setInterval(function(){
            var speed = 0;
            if(alpha > iTarget){
                speed = -10;
            }else{
                speed = 10;
            }
            if(alpha  == iTarget){
                window.clearInterval(timer);
            }else{
                alpha+=speed;
                ODiv.style.filter='Alpha(opacity:' + alpha + ')';/*IE*/
                ODiv.style.opacity = alpha/100;                 /*chrome*/
            }
        },30);
}
</script>
</head>

<body>
<div id="div1"><span>Hello</span></div>
</body>
</html>
打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP