vue 全局背景音乐
- <template>
- <div id="app">
- <router-view />
- <!-- 背景音乐 -->
- <audio src="./assets/bj.mp3"
- loop='true'
- autoplay="autoplay"
- ref="MusicPlay"
- hidden
- ></audio>
- </div>
- </template>
在app.vue里放音乐,然后使用ref="MusicPlay"
在首页/或者你想调用的页面
html部分
- <!-- 背景音乐 -->
- <div class="bjmc" @click="cyinpin()" >
- <img v-if="yinpin" src="../assets/laba/laba.png" alt="">
- <img v-else src="../assets/laba/laba-active.png" alt="">
- </div>
方法里
- cyinpin(){
- this.yinpin=!this.yinpin
- if(this.yinpin===true){
- this.$parent.$refs.MusicPlay.play();
- }else{
- this.$parent.$refs.MusicPlay.pause();
- }
- this.msg="稍等!"
- this.alertShows=true
- setTimeout(()=>{
- this.alertShows=false
- },500)
- },