菜单

HTML5 web公告API介绍

2019年10月10日 - 皇家前端

担保浏览器接济

一旦你在特定版本的浏览器上展费用付,那么作者建议您先到
caniuse
查看浏览器对Notification
API的支撑情形,制止你将贵重时间浪费在了三个不能利用的API上。

HTML5 web通知API介绍

2015/04/17 · HTML5 · 2
评论 ·
web通知

本文由 伯乐在线 –
ElvisKang
翻译,周进林
校稿。未经许可,禁绝转发!
匈牙利(Hungary)语出处:www.sevensignature.com。迎接参预翻译组。

图片 1

在使用网页版Gmail的时候,每当收到新邮件,荧屏的右下方都会弹出相应的提示框。借助HTML5提供的Notification
API,大家也得以轻巧达成如此的效应。

何以开始

JavaScript

var notification=new Notification(‘Notification Title’,{ body:’Your
Message’ });

1
2
3
var notification=new Notification(‘Notification Title’,{
    body:’Your Message’
});

上边的代码构造了二个简陋的文告栏。构造函数的第二个参数设定了通告栏的标题,而首个参数则是一个option
对象,该指标可设置以下属性:

至于作者:ElvisKang

图片 2

QQ:285273704新浪:@康恺暄邮箱:elviskang@foxmail.com
个人主页 ·
作者的稿子 ·
10

图片 3

得到权力

在显示文告栏以前需向顾客申请权限,独有客商同意,文告栏才可出现在荧屏中。对权力申请的拍卖将有以下重临值:

JavaScript

Notification.requestPermission(function(permission){ //display
notification here making use of constructor });

<table>
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><div class="crayon-nums-content" style="font-size: 13px !important; line-height: 15px !important;">
<div class="crayon-num" data-line="crayon-5b8f063d76a49818832322-1">
1
</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-5b8f063d76a49818832322-2">
2
</div>
<div class="crayon-num" data-line="crayon-5b8f063d76a49818832322-3">
3
</div>
</div></td>
<td><div class="crayon-pre" style="font-size: 13px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
<div id="crayon-5b8f063d76a49818832322-1" class="crayon-line">
Notification.requestPermission(function(permission){
</div>
<div id="crayon-5b8f063d76a49818832322-2" class="crayon-line crayon-striped-line">
//display notification here making use of constructor
</div>
<div id="crayon-5b8f063d76a49818832322-3" class="crayon-line">
});
</div>
</div></td>
</tr>
</tbody>
</table>

用HTML创立三个按键

XHTML

<button id=”button”>Read your notification</button>

1
<button id="button">Read your notification</button>

毫不遗忘了CSS

CSS

#button{ font-size:1.1rem; width:200px; height:60px; border:2px solid
#df7813; border-radius:20px/50px; background:#fff; color:#df7813; }
#button:hover{ background:#df7813; color:#fff; transition:0.4s ease;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#button{
  font-size:1.1rem;
  width:200px;
  height:60px;
  border:2px solid #df7813;
  border-radius:20px/50px;
  background:#fff;
  color:#df7813;
}
#button:hover{
  background:#df7813;
  color:#fff;
  transition:0.4s ease;
}

总体的Javascript代码如下:

JavaScript

document.addEventListener(‘DOMContentLoaded’,function(){
document.getElementById(‘button’).addEventListener(‘click’,function(){
if(! (‘Notification’ in window) ){ alert(‘Sorry bro, your browser is not
good enough to display notification’); return; }
Notification.requestPermission(function(permission){ var config = {
body:’Thanks for clicking that button. Hope you liked.’,
icon:”,
dir:’auto’ }; var notification = new Notification(“Here I am!”,config);
}); }); });

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
document.addEventListener(‘DOMContentLoaded’,function(){
        document.getElementById(‘button’).addEventListener(‘click’,function(){
            if(! (‘Notification’ in window) ){
                alert(‘Sorry bro, your browser is not good enough to display notification’);
                return;
            }    
            Notification.requestPermission(function(permission){
                var config = {
                              body:’Thanks for clicking that button. Hope you liked.’,
                              icon:’https://cdn2.iconfinder.com/data/icons/ios-7-style-metro-ui-icons/512/MetroUI_HTML5.png’,
                              dir:’auto’
                              };
                var notification = new Notification("Here I am!",config);
            });
        });
    });

从这段代码可以见见,假如浏览器不援助Notification API,在点击开关时将会出现警告“兄弟,很对不起。你的浏览器并不能够很好地支撑布告成效”(Sorry
bro, your browser is not good enough to display
notification)。不然,在获得了顾客的允许之后,大家自制的通告栏便能够出现在显示屏中间啦。

干什么要让客户手动关闭通告栏?

对于这些标题,我们得以依赖setTimeout函数设置叁个年华距离,使通告栏能定时关闭。

JavaScript

var config = { body:’Today too many guys got eyes on me, you did the
same thing. Thanks’, icon:’icon.png’, dir:’auto’ } var notification =
new Notification(“Here I am!”,config); setTimeout(function(){
notification.close(); //closes the notification },5000);

1
2
3
4
5
6
7
8
9
var config = {
               body:’Today too many guys got eyes on me, you did the same thing. Thanks’,
               icon:’icon.png’,
               dir:’auto’
             }
var notification = new Notification("Here I am!",config);
setTimeout(function(){
    notification.close(); //closes the notification
},5000);

该说的事物就那一个了。若是您意犹未尽,希望越来越深切地掌握Notification
API,能够翻阅以下的页面:

在CodePen上查看demo

你能够在CodePen上看见由Prakash
(@imprakash)编写的demo。

赞 收藏 2
评论

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图