CHAT with me on luogo!

vue 全局背景音乐

转自 https://blog.csdn.net/m0_46820776/article/details/121286262?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164698683416780265487240%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=164698683416780265487240&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2

  1. <template>
  2. <div id="app">
  3. <router-view />
  4. <!-- 背景音乐 -->
  5. <audio src="./assets/bj.mp3"
  6. loop='true'
  7. autoplay="autoplay"
  8. ref="MusicPlay"
  9. hidden
  10. ></audio>
  11. </div>
  12. </template>

在app.vue里放音乐,然后使用ref="MusicPlay"

在首页/或者你想调用的页面

html部分

  1. <!-- 背景音乐 -->
  2. <div class="bjmc" @click="cyinpin()" >
  3. <img v-if="yinpin" src="../assets/laba/laba.png" alt="">
  4. <img v-else src="../assets/laba/laba-active.png" alt="">
  5. </div>

方法里

  1. cyinpin(){
  2. this.yinpin=!this.yinpin
  3. if(this.yinpin===true){
  4. this.$parent.$refs.MusicPlay.play();
  5. }else{
  6. this.$parent.$refs.MusicPlay.pause();
  7. }
  8. this.msg="稍等!"
  9. this.alertShows=true
  10. setTimeout(()=>{
  11. this.alertShows=false
  12. },500)
  13. },

posted @ 2022-04-03 14:09  qsad阿斯顿  阅读(292)  评论(0编辑  收藏  举报