|
@@ -1,6 +1,6 @@
|
1
|
1
|
<template>
|
2
|
|
- <div class="bg-music">
|
3
|
|
-
|
|
2
|
+ <div class="bg-music" :class="{ ['icon-playing']: playing }" @click="handleClick" v-show="show">
|
|
3
|
+ <audio :src="url" ref="audioRef" loop @canplay="show = true" @play="handlePlay"></audio>
|
4
|
4
|
</div>
|
5
|
5
|
</template>
|
6
|
6
|
|
|
@@ -11,15 +11,61 @@ export default {
|
11
|
11
|
const { origin, pathname } = window.location
|
12
|
12
|
|
13
|
13
|
return {
|
|
14
|
+ show: false,
|
|
15
|
+ playing: false,
|
14
|
16
|
url: `${origin}${pathname}/bg.mp3`
|
15
|
17
|
}
|
|
18
|
+ },
|
|
19
|
+ mounted() {
|
|
20
|
+ document.body.addEventListener("click", this.handleClick);
|
|
21
|
+ },
|
|
22
|
+ methods: {
|
|
23
|
+ handleClick() {
|
|
24
|
+ if (!this.show || !this.$refs.audioRef) return;
|
|
25
|
+
|
|
26
|
+ if (this.playing) {
|
|
27
|
+ this.$refs.audioRef.pause();
|
|
28
|
+ } else {
|
|
29
|
+ this.$refs.audioRef.play();
|
|
30
|
+ }
|
|
31
|
+
|
|
32
|
+ this.playing = !this.playing
|
|
33
|
+ },
|
|
34
|
+
|
|
35
|
+ handlePlay() {
|
|
36
|
+ document.body.removeEventListener("click", this.handleClick);
|
|
37
|
+ }
|
16
|
38
|
}
|
17
|
39
|
}
|
18
|
40
|
</script>
|
19
|
41
|
|
20
|
42
|
<style lang="less" scoped>
|
21
|
43
|
.bg-music {
|
22
|
|
- width: 64px;
|
23
|
|
- height: 64px;
|
|
44
|
+ position: fixed;
|
|
45
|
+ top: 1em;
|
|
46
|
+ right: 1em;
|
|
47
|
+ z-index: 10;
|
|
48
|
+
|
|
49
|
+ width: 2em;
|
|
50
|
+ height: 2em;
|
|
51
|
+ background-image: url('~@/assets/indexImg/play.png');
|
|
52
|
+ background-repeat: no-repeat;
|
|
53
|
+ background-size: 100% 100%;
|
|
54
|
+
|
|
55
|
+ &.icon-playing {
|
|
56
|
+ background-image: url('~@/assets/indexImg/paused.png');
|
|
57
|
+ animation: musicrotate 3s linear infinite;
|
|
58
|
+ }
|
|
59
|
+
|
|
60
|
+ & > audio {
|
|
61
|
+ max-width: 1px;
|
|
62
|
+ max-height: 1px;
|
|
63
|
+ }
|
|
64
|
+}
|
|
65
|
+
|
|
66
|
+@keyframes musicrotate {
|
|
67
|
+ 100% {
|
|
68
|
+ transform: rotate(360deg);
|
|
69
|
+ }
|
24
|
70
|
}
|
25
|
71
|
</style>
|