每次使用电脑都看到的开机动画,你知道怎么做出来的吗

先看图,这个是不是很面熟呀!

直接上代码看效果

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            overflow: hidden;
        }
        
        .s {
            position: absolute;
            top: 376px;
            right: 0;
        }
        
        .x {
            position: absolute;
            top: 620px;
            right: 0;
        }
        
        .box span {
            display: block;
            width: 20px;
            height: 20px;
            position: absolute;
            bottom: 324px;
            right: 10px;
            background-color: transparent;
            z-index: 10;
        }
    </style>
</head>

<body>
    <div class="box">
        <span></span>
        <img src="../images/t.jpg" alt="" class="s">
        <img src="../images/b.jpg" alt="" class="x">
    </div>
    <script>
        var btn = document.querySelector('span');
        var shang = document.querySelector('.s');
        var xia = document.querySelector('.x');
        btn.addEventListener('click', function() {

            animate2(shang, shang.offsetTop + 143, function() {
                animate(shang, shang.offsetLeft + 325)
            });
            animate2(xia, xia.offsetTop + 101, function() {
                animate(xia, xia.offsetLeft + 325)
            });
        })

        //往下
        function animate2(obj, target, callback) {
            clearInterval(obj.timer);
            obj.timer = setInterval(function() {
                var step = (target - obj.offsetTop) / 10;
                step = step > 0 ? Math.ceil(step) : Math.floor(step);
                if (obj.offsetTop == target) {
                    clearInterval(obj.timer);
                    if (callback) {
                        //调用函数
                        callback();
                    }
                }
                obj.style.top = obj.offsetTop + step + 'px';
            }, 15)
        }


        //往右
        function animate(obj, target, callback) {
            clearInterval(obj.timer);
            obj.timer = setInterval(function() {
                var step = (target - obj.offsetLeft) / 10;
                step = step > 0 ? Math.ceil(step) : Math.floor(step);
                if (obj.offsetLeft == target) {
                    clearInterval(obj.timer);
                    if (callback) {
                        //调用函数
                        callback();
                    }
                }
                obj.style.left = obj.offsetLeft + step + 'px';
            }, 15)
        }
    </script>
</body>

</html>

效果你一定是见过的

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

super码力

么么哒,夏天来块儿冰西瓜!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值