当前位置:首页 > 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实现同步

vue实现同步

Vue 实现同步的方法 在 Vue 中实现同步操作通常涉及数据绑定、状态管理和异步操作的处理。以下是几种常见的方法: 使用计算属性(Computed Properties) 计算属性基于响应式依赖进…

vue实现切换

vue实现切换

Vue 实现切换功能的方法 在 Vue 中实现切换功能可以通过多种方式实现,以下是一些常见的方法: 使用 v-if 和 v-else 指令 通过条件渲染实现切换功能,适用于简单的显示/隐藏场景。…

vue实现表白

vue实现表白

Vue 实现表白页面 使用 Vue 可以快速创建一个动态、交互式的表白页面。以下是一个简单的实现方案: 基础结构 创建一个 Vue 项目或单文件组件,包含以下核心部分: <template&…

vue实现123456

vue实现123456

Vue 实现数字 123456 的显示 在 Vue 中显示数字 123456 非常简单,可以通过以下方法实现: 方法一:直接在模板中显示 <template> <div>…

vue实现webshell

vue实现webshell

Vue 实现 WebShell 使用 Vue 实现 WebShell 需要结合前端与后端的交互,通常通过 WebSocket 或 HTTP 协议与服务器通信。以下是一个基于 Vue 和 WebSock…

vue实现gridlayout

vue实现gridlayout

Vue 实现 Grid Layout 使用 CSS Grid 布局 CSS Grid 是一种强大的布局系统,可以直接在 Vue 组件的样式中使用。通过定义网格容器和网格项,可以快速实现复杂的布局结构。…