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