页面加载完成页面右下角出现小窗口,倒计时5秒关闭,也可自行手动关闭
代码如下:
<!DOCTYPE html>
<html lang="en">

 

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .banner {
            width: 260px;
            height: 200px;
            background-color: #999;
            position: fixed;
            right: 0;
            bottom: 0;
        }

 

        em {
            margin-right: 195px;
        }
    </style>
</head>

 

<body>
    <div class="banner">
        <em>5s</em>
        <input type="button" value="关闭" id="stop">
    </div>
    <script>
        let banner = document.querySelector(".banner");
        let em = document.querySelector("em");
        let stop0 = document.querySelector("#stop");

 

        let y = -200;
        let timer = setInterval(function () {
            y++;
            banner.style.cssText = `bottom: ${y}px;`;
            if (y >= 0) {
                time();
                clearInterval(timer);
            }
        }, 5);

 

        // 按钮关闭
        stop0.onclick = function () {
            Object.assign(banner.style, {
                display: "none"
            })
        };
        // 5秒倒计时关闭
        function time() {
            let second = 5;
            let timer = setInterval(function () {
                second--
                em.innerHTML = `${second}s`;
                if (second == 0) {
                    clearInterval(timer);
                    Object.assign(banner.style, {
                        display: "none"
                    })
                }
            }, 1000);
        }
    </script>
</body>
</html>
posted on 2020-08-21 14:59  A山巅一寺一壶酒A  阅读(499)  评论(0编辑  收藏  举报