JS 實現瀏覽器的 title 閃爍、滾動、聲音提示、chrome、Firefox、Safari等系統彈出通知。它沒有依賴,壓縮只有只有4.66kb(gzipped: 1.70kb),demo 實例預覽。javascript
# v2.x $ npm install @wcjiang/notify --save # v1.x $ npm install title-notify --save
import Notify from '@wcjiang/notify'; const notify = new Notify({ message: '有消息了。', // 標題 effect: 'flash', // flash | scroll 閃爍仍是滾動 openurl:'https://github.com/jaywcjlove/iNotify', // 點擊彈窗打開鏈接地址 onclick: () => { // 點擊彈出的窗之行事件 console.log('---') }, // 可選播放聲音 audio:{ // 能夠使用數組傳多種格式的聲音文件 file: ['msg.mp4','msg.mp3','msg.wav'] // 下面也是能夠的哦 // file: 'msg.mp4' }, // 標題閃爍,或者滾動速度 interval: 1000, // 可選,默認綠底白字的 Favicon updateFavicon:{ // favicon 字體顏色 textColor: '#fff', // 背景顏色,設置背景顏色透明,將值設置爲「transparent」 backgroundColor: '#2F9A00' }, // 可選chrome瀏覽器通知,默認不填寫就是下面的內容 notification:{ title:'通知!', // 設置標題 icon:'', // 設置圖標 icon 默認爲 Favicon body:'您來了一條新消息', // 設置消息內容 } }); notify.player();
在您的HTML中手動下載並引入 notify.js,你也能夠經過 UNPKG 進行下載:html
<script src="https://unpkg.com/@wcjiang/notify/dist/notify.min.js"></script> <script type="text/javascript"> var notify = new Notify({ effect: 'flash', interval: 500, }); notify.setFavicon('1'); </script>
audio: 可選播放聲音java
updateFavicon: 設置 Favicon 圖標顏色git
transparent
notification: 可選chrome瀏覽器通知,默認不填寫就是下面的內容github
通知!
判斷瀏覽器彈框通知是否被阻止。chrome
iNotify.isPermission()
播放聲音npm
iNotify.player()
自動播放聲音數組
iNotify.loopPlay()
中止播放聲音瀏覽器
iNotify.stopPlay()
設置播放聲音URLbash
iNotify.setURL('msg.mp3') // 設置一個 iNotify.setURL(['msg.mp3','msg.ogg','msg.mp4']) // 設置多個
最新的版本默認不播放標題閃爍動畫,初始化以後須要調用 setTitle(true)
方法才播放標題動畫。
設置標題,
iNotify.setTitle(true) // 播放動畫 iNotify.setTitle('新標題') // 閃爍新標題 iNotify.setTitle() // 清除閃爍 顯示原來的標題
設置時間間隔
iNotify.setInterval(2000)
添加計數器
iNotify.addTimer()
清除計數器
iNotify.clearTimer()
設置 icon 顯示數字或者文本
iNotify.setFavicon(10)
設置 icon 顯示文本顏色
iNotify.setFaviconColor('#0043ff')
設置 icon 顯示文本顏色
iNotify.setFaviconBackgroundColor('#0043ff') // 設置字體和背景顏色 iNotify.setFaviconColor('#f5ff00').setFaviconBackgroundColor('red');
清除數字顯示原來的icon
iNotify.faviconClear()
彈出chrome通知,不傳參數爲預設值...
iNotify.notify(); iNotify.notify({ title: '新通知', body: '打雷啦,下雨啦...', openurl: 'http://www.bing.com', onclick: function() { console.log('on click') }, onshow: function() { console.log('on show') }, });
iNotify.init().title;
獲取標題
function iconNotify(num){ if(!notify) { var notify = new Notify({ effect: 'flash', interval: 500 }); } if(num===0){ notify.faviconClear() notify.setTitle(); } else if (num < 100){ notify.setFavicon(num) notify.setTitle('有新消息!'); } else if (num > 99){ notify.setFavicon('..') notify.setTitle('有新消息!'); } }
var notify = new Notify({ effect: 'flash', interval: 500, }); notify.setFavicon('1');
var iN = new Notify({ effect: 'flash', interval: 500, message: '有消息拉!', updateFavicon:{ // 可選,默認綠底白字 textColor: '#fff',// favicon 字體顏色 backgroundColor: '#2F9A00', // 背景顏色 } }).setFavicon(10);
var iN = new Notify().setFavicon(5);
var iN = new Notify({ effect: 'flash', interval: 500, message: "有消息拉!", audio:{ file: 'msg.mp4', } }).setFavicon(10).player();
var iN = new Notify({ effect: 'flash', interval: 500, message: '有消息拉!', audio:{ file: 'msg.mp4'//能夠使用數組傳多種格式的聲音文件 }, notification:{ title: '通知!', icon: '', body: '您來了一條新消息' } }).setFavicon(10).player(); //彈出chrome通知,不傳參數爲預設值... iN.notify(); iN.notify({ title: '新通知', body: '打雷啦,下雨啦...' });
var iN = new Notify({ effect: 'flash', interval: 500, message: '有消息拉!', audio:{ file: ['msg.mp4', 'msg.mp3', 'msg.wav'] }, notification:{ title: '通知!', body:'您來了一條新消息' } }) iN.setFavicon(10).player(); var n = new Notify() n.init({ effect: 'flash', interval: 500, message: '有消息拉!', audio:{ file: ['openSub.mp4', 'openSub.mp3', 'openSub.wav'], }, notification:{ title:'通知!', icon: '', body:'您來了一個客戶', } }) n.setFavicon(10).player();
MIT © Kenny Wong