[baozhangchao] 3 年前
父节点
当前提交
351a3496c5
共有 2 个文件被更改,包括 24 次插入48 次删除
  1. 5
    7
      src/App.vue
  2. 19
    41
      src/components/BgMusic.vue

+ 5
- 7
src/App.vue 查看文件

@@ -1,18 +1,16 @@
1 1
 <template>
2 2
   <BgMusic />
3
-
4 3
   <router-view />
5 4
 </template>
6 5
 
7 6
 <script>
8
-import BgMusic from '@/components/BackPage.vue'
7
+import BgMusic from '@/components/BgMusic.vue'
9 8
 
10 9
 export default {
11
-  setup() {
12
-    return {
13
-      BgMusic
14
-    }
15
-  }
10
+  components: {
11
+    BgMusic
12
+  },
13
+  setup() {}
16 14
 }
17 15
 </script>
18 16
 

+ 19
- 41
src/components/BgMusic.vue 查看文件

@@ -1,44 +1,30 @@
1 1
 <template>
2
-  <div
3
-    class="bg-music"
4
-    :class="{ ['icon-playing']: playing }"
5
-    @click="handleClick"
6
-    v-show="data.value.show"
7
-  >
8
-    <audio
9
-      :src="data.value.url"
10
-      ref="audioRef"
11
-      loop
12
-      @loadedmetadata="data.value.show = true"
13
-      @play="handlePlay"
14
-    ></audio>
2
+  <div class="bg-music" :class="{ ['icon-playing']: playing }" @click="handleClick" v-show="show">
3
+    <audio :src="url" ref="audioRef" loop @loadedmetadata="show = true" @play="handlePlay"></audio>
15 4
   </div>
16 5
 </template>
17 6
 
18
-
19
-
20 7
 <script>
21
-import { onMounted, ref } from 'vue'
22
-
23 8
 export default {
24
-  setup() {
9
+  name: 'BgMusic',
10
+  data() {
25 11
     const { origin, pathname } = window.location
26 12
 
27
-    let data = ref({
13
+    return {
28 14
       show: false,
29 15
       playing: false,
30 16
       url: `${origin}${pathname}music/bg.mp3`
31
-    })
32
-
33
-    onMounted(() => {
34
-      document.body.addEventListener('click', this.handleClick)
17
+    }
18
+  },
19
+  mounted() {
20
+    document.body.addEventListener('click', this.handleClick)
35 21
 
36
-      this.$nextTick(() => {
37
-        this.handleIOSAudio()
38
-      })
22
+    this.$nextTick(() => {
23
+      this.handleIOSAudio()
39 24
     })
40
-
41
-    const handleIOSAudio = () => {
25
+  },
26
+  methods: {
27
+    handleIOSAudio() {
42 28
       const isiOS = !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)
43 29
       if (isiOS) {
44 30
         document.addEventListener(
@@ -49,8 +35,8 @@ export default {
49 35
           false
50 36
         )
51 37
       }
52
-    }
53
-    const handleClick = () => {
38
+    },
39
+    handleClick() {
54 40
       if (!this.show || !this.$refs.audioRef) return
55 41
 
56 42
       if (this.playing) {
@@ -60,23 +46,15 @@ export default {
60 46
       }
61 47
 
62 48
       this.playing = !this.playing
63
-    }
49
+    },
64 50
 
65
-    const handlePlay = () => {
51
+    handlePlay() {
66 52
       document.body.removeEventListener('click', this.handleClick)
67 53
     }
68
-
69
-    return {
70
-      data,
71
-      handleIOSAudio,
72
-      handleClick,
73
-      handlePlay
74
-    }
75 54
   }
76 55
 }
77 56
 </script>
78 57
 
79
-
80 58
 <style lang="less" scoped>
81 59
 .bg-music {
82 60
   position: fixed;
@@ -91,7 +69,7 @@ export default {
91 69
   background-size: 100% 100%;
92 70
 
93 71
   &.icon-playing {
94
-    background-image: url('~@/assets/indexImg/paused.png');
72
+    background-image: url('~@/assets/ButtonImage/paused.png');
95 73
     animation: musicrotate 3s linear infinite;
96 74
   }
97 75