|
@@ -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>
|