当前位置:首页 > VUE

vue实现轮播视频

2026-02-19 07:23:20VUE

Vue 实现轮播视频的方法

使用第三方库(推荐)

安装 vue-awesome-swiperswiper 库,它们支持视频嵌入和轮播功能。以下是基于 vue-awesome-swiper 的实现:

<template>
  <swiper :options="swiperOptions">
    <swiper-slide v-for="(video, index) in videos" :key="index">
      <video controls :poster="video.poster" class="video-slide">
        <source :src="video.src" type="video/mp4">
      </video>
    </swiper-slide>
    <div class="swiper-pagination" slot="pagination"></div>
  </swiper>
</template>

<script>
import { Swiper, SwiperSlide } from 'vue-awesome-swiper';
import 'swiper/css/swiper.css';

export default {
  components: { Swiper, SwiperSlide },
  data() {
    return {
      videos: [
        { src: '/path/to/video1.mp4', poster: '/path/to/poster1.jpg' },
        { src: '/path/to/video2.mp4', poster: '/path/to/poster2.jpg' }
      ],
      swiperOptions: {
        pagination: { el: '.swiper-pagination' },
        loop: true,
        autoplay: { delay: 3000, disableOnInteraction: false }
      }
    };
  }
};
</script>

<style scoped>
.video-slide {
  width: 100%;
  height: auto;
}
</style>

自定义实现

通过 Vue 的 v-for 和动态样式控制,手动切换视频:

vue实现轮播视频

<template>
  <div class="video-carousel">
    <div class="video-container">
      <video 
        v-for="(video, index) in videos" 
        :key="index" 
        :ref="'video' + index"
        :class="{ active: currentIndex === index }"
        controls
        :poster="video.poster"
      >
        <source :src="video.src" type="video/mp4">
      </video>
    </div>
    <button @click="prev">上一页</button>
    <button @click="next">下一页</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      videos: [
        { src: '/path/to/video1.mp4', poster: '/path/to/poster1.jpg' },
        { src: '/path/to/video2.mp4', poster: '/path/to/poster2.jpg' }
      ],
      currentIndex: 0
    };
  },
  methods: {
    next() {
      this.currentIndex = (this.currentIndex + 1) % this.videos.length;
    },
    prev() {
      this.currentIndex = (this.currentIndex - 1 + this.videos.length) % this.videos.length;
    }
  }
};
</script>

<style scoped>
.video-container {
  position: relative;
  width: 100%;
  height: 400px;
  overflow: hidden;
}
video {
  position: absolute;
  width: 100%;
  height: 100%;
  opacity: 0;
  transition: opacity 0.5s;
}
video.active {
  opacity: 1;
}
</style>

关键点

  • 视频预加载:通过 poster 属性设置封面图,避免空白等待。
  • 自动播放:在 swiperOptions 中配置 autoplay,或通过 setInterval 控制自定义轮播。
  • 交互暂停:监听 mouseentermouseleave 事件暂停/恢复轮播。

注意事项

  • 移动端需添加 playsinline 属性避免全屏播放。
  • 视频格式优先选择兼容性强的 mp4,并提供备用格式(如 WebM)。

标签: 视频vue
分享给朋友:

相关文章

vue实现 toast

vue实现 toast

Vue 实现 Toast 组件 在 Vue 中实现 Toast 组件可以通过多种方式完成,以下是几种常见的方法: 使用全局组件和事件总线 创建一个全局的 Toast 组件,并通过事件总线或 Vue…

vue实现看板

vue实现看板

Vue 实现看板功能 使用 Vue 实现看板功能可以通过组件化开发、状态管理和拖拽库结合完成。以下是一个完整的实现方案: 基础项目结构 src/ ├── components/ │ ├── Ka…

vue实现开关

vue实现开关

Vue 实现开关组件的方法 使用原生HTML和Vue数据绑定 通过v-model绑定复选框的checked状态,结合CSS样式实现开关效果: <template> <label…

vue实现验证

vue实现验证

Vue 表单验证实现方法 在Vue中实现表单验证可以通过多种方式完成,以下是常见的几种方法: 使用Vuelidate库 Vuelidate是一个轻量级的Vue表单验证库,安装后可以通过简单的配置实现…

vue实现横滚

vue实现横滚

Vue 实现横滚效果 横滚效果通常指水平滚动内容,常见于轮播图、横向导航栏等场景。以下是几种实现方式: 使用 CSS 和 Vue 结合 通过 CSS 的 overflow-x 和 white-spa…

vue实现导航

vue实现导航

使用 Vue Router 实现导航 Vue Router 是 Vue.js 官方提供的路由管理器,用于构建单页应用(SPA)的导航系统。以下是实现导航的基本步骤。 安装 Vue Router n…