Vue+element-ui,实现表格渲染缩略图,鼠标悬浮缩略图放大,点击缩略图播放视频(一)

Vue+element-ui,实现表格渲染缩略图,鼠标悬浮缩略图放大,点击缩略图播放视频

前言

如标题,需要实现这样的业务

此处文章所实现的,是静态视频资源。

注意打包方式(关系路径写法):

  1. 现代打包项目(Vue/React):
    /favicon.ico 直接引用 public 目录资源
  2. 传统静态项目:
    ../public/favicon.ico../表示返回上一路径)

整体代码

template

html 复制代码
<el-table-column
  label="操作"
  align="center"
  class-name="small-padding fixed-width"
  width="310px"
>
  <template slot-scope="scope">
    <template v-if="scope.row.callType === 0">
      <audio
        :id="'audio-' + scope.row.sipCallId"
        controls
        :src="scope.row.audioUrl"
        @play="loadAudio(scope.row)"
        preload="none"
      >
        浏览器不支持播放
      </audio>
    </template>
    <template v-else>
      <!--    视频播放      -->
      <el-popover trigger="hover" placement="right">
        <img
          slot="reference"
          src="/test(1).png"
          alt="加载失败"
          class="image"
          style="width: 80px;height: 80px"
          @error="handleImageError"
        />
        <div class="video-wrapper" @mouseleave="closeVideo">
          <video
            controls
            src="/test.mp4"
            @play=""
            @pause="videoPause()"
            @ended="videoEnded()"
            poster="/test(1).png"
            preload="none"
            style="width: 400px;height:400px;background-color: rgba(0,0,0,0.18)"
            ref="videoRef"
          ></video>
          <div class="video-overlay" v-if="videoOverlayFlag">
            <div class="play-button" @click="handlePlay">
              {{ videoPlayTitle ? '暂停' : '播放' }}
            </div>
          </div>
        </div>
      </el-popover>
    </template>
  </template>
</el-table-column>

data()

js 复制代码
videoPlayTitle: false,
videoOverlayFlag: true,

method()

js 复制代码
    closeVideo() {
      const video = this.$refs.videoRef;
      video.pause()
    },
    videoEnded() {
      this.videoOverlayFlag = true
      this.videoPlayTitle = false;
    },
    videoPause() {
      this.videoOverlayFlag = true
      this.videoPlayTitle = true;
    },
    handlePlay() {
      const video = this.$refs.videoRef;
      video.play().then(() => {
        this.videoOverlayFlag = false;
        this.videoPlayTitle = false;
      }).catch(error => {
        this.videoOverlayFlag = true
        this.videoPlayTitle = false;
      });
    },
    handleImageError(event) {
      event.target.src = '/test(1).png'; // 加载失败时显示的图片
    },
    formatCallType(type) {
      return type === 0 ? '语音' : type === 1 ? '视频' : '未知';
    },

css

css 复制代码
<style rel="stylesheet/scss" lang="scss">

//全屏按钮
video::-webkit-media-controls-fullscreen-button {
  display: none;
}
//播放按钮
video::-webkit-media-controls-play-button {
  display: none;
}

.video-wrapper {
  position: relative;
  display: inline-block;
}

.video-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(255, 255, 240, 0.3); /* 半透明乳白色遮罩 */
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.play-button {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: rgba(0, 0, 0, 0.7);
  color: white;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}
</style>

预览图

具体分析

基础结构

html 复制代码
<el-table-column>
  <template slot-scope="scope">
    <template v-if="scope.row.callType === 0">
      <audio></audio>
    </template>
    <template v-else>
      <el-popover trigger="hover" placement="right">
        <img/>
        <div class="video-wrapper" @mouseleave="closeVideo">
          <video></video>
          <div class="video-overlay" v-if="videoOverlayFlag">
            <div class="play-button" @click="handlePlay">
              {{ videoPlayTitle ? '暂停' : '播放' }}
            </div>
          </div>
        </div>
      </el-popover>
    </template>
  </template>
</el-table-column>

主要标签作用

video

实现视频播放,使用的是原生的video标签

el-popover

相关推荐
尚学教辅学习资料20 小时前
Vue3从入门到精通: 4.5 数据持久化与同步策略深度解析
vue·数据持久化
IT毕设实战小研1 天前
Java毕业设计选题推荐 |基于SpringBoot的健身爱好线上互动与打卡社交平台系统 互动打卡小程序系统
java·开发语言·vue.js·spring boot·vue·毕业设计·课程设计
第七种黄昏2 天前
大事件项目拆解:登录访问拦截实现详解
前端框架·vue·js
进阶的小木桩2 天前
Vue 3 + Elementui + TypeScript 实现左侧菜单定位右侧内容
vue.js·elementui·typescript
har01d3 天前
在 uniapp 里使用 unocss,vue3 + vite 项目
前端·uni-app·vue·uniapp·unocss
har01d4 天前
【CSS3】录音中。。。
前端·css·vue.js·vue·vue3·css3
柯北(jvxiao)5 天前
Vue vs React 多维度剖析: 哪一个更适合大型项目?
前端·vue·react
北辰浮光5 天前
[Element-plus]动态设置组件的语言
javascript·vue.js·elementui
晓13135 天前
Vue2篇——第二章 Vue从指令修饰符到侦听器的全面解析(重点)
前端·javascript·vue
胡斌附体6 天前
elementui cascader 远程加载请求使用 选择单项等
前端·javascript·elementui·cascader·可独立选中单节点