HTML原生JavaScript实现页面通知,倒计时关闭,弹出数量限制

重复造轮子了,删了可惜记录一下吧!

<!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>通知</title>
</head>
<style>
    #PL_JS_Alerts {
        position: fixed;
        bottom: 16px;
        right: 16px;
    }

    .PL_JS_Alert {
        border-radius: 0.5rem;
        text-align: center;
        background-color: #fff;
        border: 1px solid #ccc;
        margin: 1rem;
        box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
    }

    .PL_JS_Title {
        color: white;
        background-color: #03A9F4;
        padding: .5rem;
        border-radius: 0.5rem 0.5rem 0rem 0rem;
        text-align: left;
        font-weight: bold;
        font-size: 1rem;
    }

    .PL_JS_Content {
        font-size: 1rem;
        padding-top: 1rem;
        padding-left: 1.5rem;
        padding-bottom: 1rem;
        padding-right: 1.5rem;
        border-bottom: 1px solid #eee;
    }

    .PL_JS_Countdown {
        text-align: right;
        font-size: 0.5rem;
    }
</style>

<body>
    <button id="myBtn">弹出提示</button>

    <script>
        // 获取DOM元素
        var btn = document.getElementById("myBtn");

        //--------------------------------------------------
        // 弹出提示框
        var alertsContainer = document.createElement("div");
        alertsContainer.id = "PL_JS_Alerts"
        //设置index
        const maxZIndex = Math.pow(2, 31) - 1;
        alertsContainer.style.zIndex = maxZIndex.toString();
        document.body.appendChild(alertsContainer);
        function JS_Alerts(options) {
            options = Object.assign({
                toast_title: "盘络程序",
                toast_content: "上传成功",
                toast_DestroyTime: '10'
            }, options);
            var alertEl = document.createElement("div");
            alertEl.classList.add("PL_JS_Alert");

            var titleEl = document.createElement("div");
            titleEl.classList.add("PL_JS_Title");
            titleEl.innerHTML = options.toast_title;

            var contentEl = document.createElement("div");
            contentEl.classList.add("PL_JS_Content");
            contentEl.innerHTML = options.toast_content;

            var countdownEl = document.createElement("div");
            countdownEl.classList.add("PL_JS_Countdown");
            countdownEl.innerHTML = options.toast_DestroyTime;

            alertEl.addEventListener("click", function () {
                clearInterval(countdownInterval);
                alertEl.remove();
            });
            alertEl.appendChild(titleEl);
            alertEl.appendChild(contentEl);
            alertEl.appendChild(countdownEl);

            // 添加到提示容器中
            alertsContainer.appendChild(alertEl);

            // 如果提示数量超过5个,删除最先弹出的
            var alerts = alertsContainer.querySelectorAll(".PL_JS_Alert");
            if (alerts.length > 5) {
                alerts[0].remove();
            }

            // 设置倒计时,每秒钟更新一次
            var countdown = options.toast_DestroyTime;
            var countdownInterval = setInterval(function () {
                countdown--;
                countdownEl.innerHTML = countdown;
                if (countdown <= 0) {
                    clearInterval(countdownInterval);
                    alertEl.remove();
                }
            }, 1000);
        }
        //--------------------------------------------------

        btn.addEventListener("click", function () {
            JS_Alerts({
                toast_title: "盘络程序",
                toast_content: "你好啊这是一段测试!",
                toast_DestroyTime: '10'
            })
        })


    </script>
</body>
</html>

发表新评论