HTML5 web公告API介绍

HTML5 web通知API介绍

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

本文由 伯乐在线 –
ElvisKang
翻译,周进林
校稿。未经许可,禁止转载!
英文出处:www.sevensignature.com。应接到场翻译组。

图片 1

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

管教浏览器帮助

假诺您在特定版本的浏览器上实行支付,那么小编建议你先到
caniuse
查看浏览器对Notification
API的支撑情形,幸免你将贵重时间浪费在了贰个不可能选取的API上。

什么开首

JavaScript

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

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

地点的代码构造了三个简陋的布告栏。构造函数的首先个参数设定了布告栏的题目,而第二个参数则是八个option
对象,该指标可设置以下属性:

  • body :设置公告栏的正文内容。
  • dir
    :定义公告栏文本的突显方向,可设为auto(自动)、ltr(从左到右)、rtl(从右到左)。
  • lang
    :注解通告栏内文本所使用的语种。(译注:该属性的值必须属于BCP 47
    language tag。)
  • tag:为公告栏分配一个ID值,便于寻觅、替换或移除通知栏。
  • iconHTML5 web公告API介绍。 :设置作为公告栏icon的图样的U福特ExplorerL

得到权力

HTML5 web公告API介绍。在体现布告栏在此以前需向用户申请权限,唯有用户同意,布告栏才可出现在显示屏中。对权力申请的拍卖将有以下重返值:

  • default:用户管理结果未知,因而浏览器将视为用户拒绝弹出文告栏。(“浏览器:你没需求照拂,作者就不打招呼你了”)
  • deniedHTML5 web公告API介绍。:用户拒绝弹出文告栏。(“用户:从本人的显示屏里滚开”)
  • HTML5 web公告API介绍。granted:用户同意弹出布告栏。(“用户:接待!作者很欢快能够选取那几个公告效能”)
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
#HTML5 web公告API介绍。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,能够翻阅以下的页面:

  • MDN
  • Paul lund’s tutorial on notification
    API

在CodePen上查看demo

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

赞 收藏 2
评论

至于小编:ElvisKang

图片 2

QQ:285273704微博:@康恺暄邮箱:elviskang@foxmail.com
个人主页 ·
我的稿子 ·
10

图片 3

发表评论

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