[baozhangchao] 3 年前
父节点
当前提交
287207956b
共有 1 个文件被更改,包括 40 次插入18 次删除
  1. 40
    18
      src/components/BgMusic.vue

+ 40
- 18
src/components/BgMusic.vue 查看文件

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