当前位置:首页 > VUE

vue实现视频轮播功能

2026-01-22 01:44:12VUE

使用Swiper组件实现视频轮播

安装Swiper依赖包

npm install swiper vue-awesome-swiper

引入Swiper组件

import { Swiper, SwiperSlide } from 'swiper/vue'
import 'swiper/css'
import 'swiper/css/pagination'
import 'swiper/css/navigation'

基础模板结构

<template>
  <swiper
    :pagination="{
      clickable: true,
    }"
    :navigation="true"
    class="mySwiper"
  >
    <swiper-slide v-for="(video, index) in videos" :key="index">
      <video controls>
        <source :src="video.src" type="video/mp4">
      </video>
    </swiper-slide>
  </swiper>
</template>

数据定义

data() {
  return {
    videos: [
      { src: '/videos/video1.mp4' },
      { src: '/videos/video2.mp4' },
      { src: '/videos/video3.mp4' }
    ]
  }
}

自定义视频控制逻辑

添加自动播放功能

const swiperOptions = {
  autoplay: {
    delay: 3000,
    disableOnInteraction: false,
  },
  loop: true
}

视频播放状态管理

methods: {
  onSlideChange(swiper) {
    const activeSlide = swiper.slides[swiper.activeIndex]
    const video = activeSlide.querySelector('video')
    video.play()

    swiper.slides.forEach((slide, index) => {
      if(index !== swiper.activeIndex) {
        const v = slide.querySelector('video')
        v.pause()
        v.currentTime = 0
      }
    })
  }
}

响应式设计配置

设置不同断点的幻灯片数量

const swiperOptions = {
  breakpoints: {
    320: {
      slidesPerView: 1
    },
    768: {
      slidesPerView: 2
    },
    1024: {
      slidesPerView: 3
    }
  }
}

性能优化方案

懒加载视频资源

<video controls preload="none">
  <source :src="video.src" type="video/mp4">
</video>

添加加载状态指示

vue实现视频轮播功能

data() {
  return {
    loading: true
  }
},
methods: {
  videoLoaded() {
    this.loading = false
  }
}

完整组件示例

<template>
  <div class="video-carousel">
    <swiper
      :options="swiperOptions"
      @slideChange="onSlideChange"
    >
      <swiper-slide v-for="(video, index) in videos" :key="index">
        <div v-if="loading" class="loading-spinner"></div>
        <video
          controls
          preload="none"
          @loadeddata="videoLoaded"
        >
          <source :src="video.src" type="video/mp4">
        </video>
      </swiper-slide>
    </swiper>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: true,
      videos: [...],
      swiperOptions: {
        autoplay: { delay: 5000 },
        navigation: true,
        pagination: { clickable: true },
        breakpoints: {...}
      }
    }
  },
  methods: {...}
}
</script>

<style>
.video-carousel {
  max-width: 800px;
  margin: 0 auto;
}
.swiper-slide video {
  width: 100%;
  height: auto;
}
</style>

标签: 功能视频
分享给朋友:

相关文章

h5实现视频通话

h5实现视频通话

WebRTC 基础实现 H5 视频通话主要通过 WebRTC(Web Real-Time Communication)技术实现。现代浏览器原生支持该 API,无需插件即可完成点对点音视频传输。 安装…

h5怎么实现上传视频

h5怎么实现上传视频

使用HTML5实现视频上传 HTML5提供了<input type="file">元素用于文件上传,结合<video>标签可以实现视频上传和预览功能。 <input t…

php分页功能的实现

php分页功能的实现

分页功能的基本原理 分页功能的核心是通过SQL的LIMIT子句实现数据分段查询。LIMIT接受两个参数:起始位置偏移量和每页记录数。例如查询第2页(每页10条)的SQL语句为: SELECT * F…

vue实现缓存视频

vue实现缓存视频

Vue 实现视频缓存的方法 在 Vue 中实现视频缓存可以通过多种方式完成,包括使用浏览器的缓存机制、Service Worker 或者借助第三方库。以下是几种常见的方法: 使用 Service W…

vue 实现备注功能

vue 实现备注功能

Vue 实现备注功能的方法 数据绑定与基础表单 在 Vue 中实现备注功能,可以通过 v-model 实现双向数据绑定。创建一个文本输入框或文本域,绑定到组件的 data 属性: <templ…

vue实现网页功能

vue实现网页功能

Vue 实现网页功能的核心方法 创建 Vue 项目 使用 Vue CLI 或 Vite 快速初始化项目。Vue CLI 适合传统项目,Vite 适合现代轻量级应用。 npm init vue@lat…