原生js方式写个Notification组件 (类似elementui通知效果)

Notification - Web API 接口参考 | MDNNotifications API 的接口 Notification 用于配置以向用户显示桌面通知。https://developer.mozilla.org/zh-CN/docs/Web/API/notification

1,Notification  api的通知接口,用于显示桌面通知;

2,代码:

        

  function Notice() {

        if (!("Notification" in window)) {

            // Check if the browser supports notifications

            alert("This browser does not support desktop notification");

        } else if (Notification.permission === "granted") {

            // Check whether notification permissions have already been granted;

            // if so, create a notification

            const notification = new Notification("通知!");

            console.log(notification);

        } else if (Notification.permission !== "denied") {

            // We need to ask the user for permission

            Notification.requestPermission().then((permission) => {

                // If the user accepts, let's create a notification

                if (permission === "granted") {

                    const notification = new Notification("通知!");

                }

            });

        }

        // At last, if the user has denied notifications, and you

        // want to be respectful there is no need to bother them anymore.

    }

Supongo que te gusta

Origin blog.csdn.net/zhh_5/article/details/128373593
Recomendado
Clasificación