前端(vue)入門到精通課程,老師線上輔導:聯絡老師
Apipost = Postman + Swagger + Mock + Jmeter 超好用的API偵錯工具:
【相關推薦:】
1.1 元件模板參照
<PageNotice ref="pageNotice" sound="/xxx/new_message.mp3" />
登入後複製
1.2 支援的引數
sound: 通知時播放的聲音
1.3 動態呼叫方法
$refs.pageNotice.tip('你好','新訊息') $refs.pageNotice.tip('有新客戶存取')
登入後複製
PageNotice 元件原始碼如下
<template>
<div>
<audio ref="audio" :src="sound"></audio>
</div>
</template>
<script>
export default {
name: "PageNotice",
props: {
sound: {
type: String,
default: ''
},
},
data() {
return {
tipTimer: null,
tipTimerCount: 0,
titleOld: null,
}
},
methods: {
tip(msg, type) {
this.doPageTitle(msg, type)
if (this.sound) {
this.doPlaySound()
}
},
doClearTimer() {
clearInterval(this.tipTimer)
this.tipTimer = null
if (this.titleOld) {
window.document.title = this.titleOld
}
this.tipTimerCount = 0
},
doPageTitle(msg, type) {
type = type || '提醒'
if (this.tipTimer) {
this.doClearTimer()
}
this.titleOld = document.title
this.tipTimerCount = 0
this.tipTimer = setInterval(() => {
this.tipTimerCount++
if (this.tipTimerCount % 2 === 0) {
window.document.title = '【' + type + '】' + msg
} else {
window.document.title = '' + msg
}
if (this.tipTimerCount > 6) {
this.doClearTimer()
}
}, 500)
},
doPlaySound() {
let audio = this.$refs.audio
if (!audio) {
return
}
try {
audio.pause()
audio.play()
} catch (e) {
}
}
}
}
</script>
登入後複製
以上就是分享一個VUE頁面聲音+標題閃爍通知的元件(附使用方法)的詳細內容,更多請關注TW511.COM其它相關文章!