李志伟 hace 3 años
padre
commit
d7da0944c6
Se han modificado 4 ficheros con 89 adiciones y 1 borrados
  1. 5
    1
      src/App.vue
  2. BIN
      src/assets/indexImg/paused.png
  3. BIN
      src/assets/indexImg/play.png
  4. 84
    0
      src/components/BgMusic.vue

+ 5
- 1
src/App.vue Ver fichero

@@ -1,12 +1,16 @@
1 1
 <template>
2 2
   <div id="app">
3
+    <bg-music />
3 4
     <router-view />
4 5
   </div>
5 6
 </template>
6 7
 
7 8
 <script>
8 9
 export default {
9
-  name: 'App'
10
+  name: 'App',
11
+  components: {
12
+    BgMusic: () => import('./components/BgMusic.vue')
13
+  }
10 14
 }
11 15
 </script>
12 16
 

BIN
src/assets/indexImg/paused.png Ver fichero


BIN
src/assets/indexImg/play.png Ver fichero


+ 84
- 0
src/components/BgMusic.vue Ver fichero

@@ -0,0 +1,84 @@
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>
4
+  </div>
5
+</template>
6
+
7
+<script>
8
+export default {
9
+  name: 'BgMusic',
10
+  data() {
11
+    const { origin, pathname } = window.location
12
+
13
+    return {
14
+      show: false,
15
+      playing: false,
16
+      url: `${origin}${pathname}bg.mp3`
17
+    }
18
+  },
19
+  mounted() {
20
+    document.body.addEventListener("click", this.handleClick);
21
+
22
+    this.$nextTick(() => {
23
+      this.handleIOSAudio()
24
+    })
25
+
26
+  },
27
+  methods: {
28
+    handleIOSAudio() {
29
+      const isiOS = !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)
30
+      if (isiOS) {
31
+        document.addEventListener('WeixinJSBridgeReady', () => {
32
+          this.$refs.audioRef.load()
33
+        }, false)
34
+      }
35
+    },
36
+    handleClick() {
37
+      if (!this.show || !this.$refs.audioRef) return;
38
+
39
+      if (this.playing) {
40
+        this.$refs.audioRef.pause();
41
+      } else {
42
+        this.$refs.audioRef.play();
43
+      }
44
+
45
+      this.playing = !this.playing
46
+    },
47
+
48
+    handlePlay() {
49
+      document.body.removeEventListener("click", this.handleClick);
50
+    }
51
+  }
52
+}
53
+</script>
54
+
55
+<style lang="scss" scoped>
56
+.bg-music {
57
+  position: fixed;
58
+  top: 1em;
59
+  right: 1em;
60
+  z-index: 10;
61
+
62
+  width: 2em;
63
+  height: 2em;
64
+  background-image: url('../assets/indexImg/play.png');
65
+  background-repeat: no-repeat;
66
+  background-size: 100% 100%;
67
+
68
+  &.icon-playing {
69
+    background-image: url('../assets/indexImg/paused.png');
70
+    animation: musicrotate 3s linear infinite;
71
+  }
72
+
73
+  & > audio {
74
+    max-width: 1px;
75
+    max-height: 1px;
76
+  }
77
+}
78
+
79
+@keyframes musicrotate {
80
+  100% {
81
+    transform: rotate(360deg);
82
+  }
83
+}
84
+</style>