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

JavaScript动画之透明度渐变

Leal_Gullden
关注TA
已关注
手记 15
粉丝 23
获赞 267

效果图
图片描述
图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>demo</title>
    <!-- <link rel="stylesheet" type="text/css" href="../css/demo4.css">
    <script src="../js/demo4.js"></script> -->
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
        }
        #box{
            width: 200px;
            height: 200px;
            margin: 0 auto;
            background-color: #6cccf2;
            opacity: 0.3;
        }
    </style>
    <script type="text/javascript">
        window.onload=function(){
            var box = document.getElementById('box');
            box.onmouseover=function(){
                startChange(100);
            };
            box.onmouseout=function(){
                startChange(30);
            };
        };
        var timer = null;
        function startChange(oTarget){
            var speed = 0;
            var box = document.getElementById('box');
            var alpha = box.style.opacity*100;
            if(alpha<oTarget){
                speed=10;
            }else if(alpha>oTarget){
                speed= -10;
            }
            clearInterval(timer);
            timer = setInterval(function(){
                alpha = alpha + speed;
                box.style.opacity = alpha/100;
                if(alpha===oTarget){
                    clearInterval(timer);
                }
            },30);
        }
    </script>
</head>
<body>
    <div class="wrap">
        <div id="box"></div>
    </div>
</body>
</html>
打开App,阅读手记
4人推荐
发表评论
随时随地看视频慕课网APP