huanghongfeng
10 天以前 f3ec4fe9c98a87b42b00b6ac4790fe156a32aa6b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<template>
  <div class="centermap">
    <div class="mapwrap">
      <dv-border-box-13>
        <video 
          ref="videoPlayer" 
          class="screen-video" 
          src="../../assets/img/2.mp4" 
          loop 
          autoplay 
          preload="auto"
          muted
          @click="unmuteVideo"
        >
          您的浏览器不支持视频播放。
        </video>
        <div v-if="isMuted" class="mute-hint" @click="unmuteVideo">
          <span>点击取消静音(5秒后自动播放声音)</span>
        </div>
      </dv-border-box-13>
    </div>
  </div>
</template>
 
<script>
export default {
  data() {
    return {
      isMuted: false,
      unmuteTimeout: null // 存储定时器以便清理
    };
  },
  mounted() {
    this.initVideo();
    this.adjustVideoSize();
    window.addEventListener('resize', this.adjustVideoSize);
 
    // 5秒后自动取消静音
    this.unmuteTimeout = setTimeout(() => {
      this.unmuteVideo();
    }, 5000);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.adjustVideoSize);
    if (this.unmuteTimeout) {
      clearTimeout(this.unmuteTimeout); // 清除定时器避免内存泄漏
    }
  },
  methods: {
    initVideo() {
      const video = this.$refs.videoPlayer;
      if (video) {
        video.addEventListener('ended', () => {
          video.currentTime = 0;
          video.play();
        });
      }
      document.addEventListener('click', this.unmuteVideo, { once: true });
    },
    unmuteVideo() {
      const video = this.$refs.videoPlayer;
      if (video) {
        video.muted = false;
        video.volume = 1.0;
        this.isMuted = false;
      }
    },
    adjustVideoSize() {
      const video = this.$refs.videoPlayer;
      if (video) {
        video.style.width = '100%';
        video.style.height = '100%';
        video.style.objectFit = 'cover';
      }
    }
  }
};
</script>
 
<style scoped>
/* 样式保持不变 */
.centermap {
  width: 100%;
  height: 100%;
  
  .mapwrap {
    height: 960px;
    width: 100%;
    box-sizing: border-box;
    position: relative;
    margin-top: 10px;
    
    & > dv-border-box-13 {
      width: 100%;
      height: 100%;
      position: relative;
      overflow: hidden;
    }
  }
}
 
.screen-video {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
 
.mute-hint {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(0,0,0,0.7);
  color: white;
  padding: 10px 20px;
  border-radius: 5px;
  cursor: pointer;
  z-index: 10;
}
</style>