1 回答

TA貢獻1790條經驗 獲得超9個贊
這是一個可行的解決方案(更新視頻 URL 后):
<template>
<div>
<div v-for="video in videos" :key="video.ref">
<video :ref="video.ref" :src="video.url" type="video/mp4" @click="onClick(video)"></video>
</div>
</div>
</template>
<script>
export default {
computed: {
videos() {
return [
{ url: 'https://path/to/your/video.mp4', ref: 'video0' },
{ url: 'https://path/to/your/video.mp4', ref: 'video1' },
{ url: 'https://path/to/your/video.mp4', ref: 'video2' },
];
},
},
methods: {
onClick({ ref }) {
// Loop over all videos
this.videos.forEach((video) => {
// Get the DOM element for this video
const $video = this.$refs[video.ref][0];
// Play the video that the user clicked
if (video.ref === ref) $video.play();
// Pause all other videos
else $video.pause();
});
},
},
};
</script>
請注意,這里我將其定義videos為計算屬性。它可能是你的情況的一個支柱。此示例為每個視頻分配一個ref字符串,以便稍后可以在處理程序中直接操作該視頻onClick。
添加回答
舉報