当前位置:首页 > VUE

vue实现视频列表代码

2026-02-23 23:16:22VUE

以下是一个基于Vue.js实现视频列表的示例代码,包含基础功能如视频展示、播放控制等:

vue实现视频列表代码

视频列表组件基础结构

<template>
  <div class="video-list">
    <div v-for="(video, index) in videos" :key="index" class="video-item">
      <video 
        :ref="'videoPlayer'+index"
        :poster="video.cover"
        @click="togglePlay(index)"
      >
        <source :src="video.url" type="video/mp4">
      </video>
      <div class="video-info">
        <h3>{{ video.title }}</h3>
        <p>{{ video.description }}</p>
      </div>
    </div>
  </div>
</template>

数据与状态管理

<script>
export default {
  data() {
    return {
      videos: [
        {
          id: 1,
          title: '示例视频1',
          description: '这是第一个示例视频',
          url: '/path/to/video1.mp4',
          cover: '/path/to/cover1.jpg'
        },
        // 更多视频数据...
      ],
      currentPlaying: null
    }
  }
}
</script>

播放控制方法

methods: {
  togglePlay(index) {
    const video = this.$refs[`videoPlayer${index}`][0]
    if (this.currentPlaying !== index) {
      if (this.currentPlaying !== null) {
        this.$refs[`videoPlayer${this.currentPlaying}`][0].pause()
      }
      video.play()
      this.currentPlaying = index
    } else {
      if (video.paused) {
        video.play()
      } else {
        video.pause()
      }
    }
  }
}

基础样式

<style scoped>
.video-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
}

.video-item video {
  width: 100%;
  cursor: pointer;
  border-radius: 8px;
}

.video-info {
  padding: 10px 0;
}
</style>

功能扩展建议

添加视频加载状态指示器

vue实现视频列表代码

<div v-if="loading" class="loading-indicator">
  加载中...
</div>

实现懒加载优化性能

const observer = new IntersectionObserver((entries) => {
  entries.forEach(entry => {
    if (entry.isIntersecting) {
      const video = entry.target
      video.setAttribute('src', video.dataset.src)
      observer.unobserve(video)
    }
  })
})

this.$nextTick(() => {
  document.querySelectorAll('video').forEach(video => {
    observer.observe(video)
  })
})

添加视频进度条控制

<input 
  type="range" 
  :value="currentProgress" 
  @input="seekVideo"
  min="0" 
  :max="duration"
>

该实现包含视频列表展示、播放控制等基础功能,可根据实际需求进一步扩展如分页加载、分类筛选等功能。

标签: 代码列表
分享给朋友:

相关文章

vue 实现列表

vue 实现列表

Vue 实现列表的方法 在 Vue 中实现列表通常使用 v-for 指令,结合数组或对象进行渲染。以下是几种常见的实现方式: 基础列表渲染 通过 v-for 指令遍历数组,动态生成列表项。 &…

uniapp商品列表

uniapp商品列表

商品列表实现方法 在uniapp中实现商品列表功能,可以通过多种方式完成,以下是几种常见的方法: 使用scroll-view组件实现滚动列表 <scroll-view scroll-y="t…

uniapp 内容列表

uniapp 内容列表

uniapp 内容列表的实现方法 在uniapp中实现内容列表通常需要结合组件和数据绑定。以下是一个常见的方法: 模板部分 <template> <view class="c…

vue实现列表组件

vue实现列表组件

Vue 列表组件实现方法 基础列表渲染 使用 v-for 指令渲染数组数据,需配合 :key 提升性能: <template> <ul> <li v-for…

vue实现列表页面

vue实现列表页面

Vue 列表页面实现方案 基础列表渲染 使用 v-for 指令渲染数组数据,配合 :key 提升性能: <template> <ul> <li v-for=…

vue代码核心实现

vue代码核心实现

Vue 核心实现原理 Vue.js 的核心实现主要包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是关键实现要点: 响应式系统 通过 Object.defineProperty(Vue 2)…