首页 > web前端 > Vue.js > 正文

分享一个VUE页面声音+标题闪烁通知的组件

藏色散人
发布: 2023-03-14 17:28:16
转载
1576人浏览过

本篇文章给大家带来了关于vue的相关知识,其中主要跟大家分享一个vue页面声音+标题闪烁通知的组件,感兴趣的朋友下面一起来看一下吧,希望对大家有帮助。

分享一个VUE页面声音+标题闪烁通知的组件

一个VUE页面声音+标题闪烁通知的组件

1、使用方法

1.1 组件模板引用

立即学习前端免费学习笔记(深入)”;

<PageNotice ref="pageNotice" sound="/xxx/new_message.mp3" />
登录后复制

1.2 支持的参数

sound: 通知时播放的声音

1.3 动态调用方法

$refs.pageNotice.tip('你好','新消息') $refs.pageNotice.tip('有新客户访问')
登录后复制

2、组件源码

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.js视频教程

以上就是分享一个VUE页面声音+标题闪烁通知的组件的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
相关标签:
来源:learnku网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号