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

相关推荐
左手吻左脸。1 小时前
解决el-select因为弹出层层级问题,不展示下拉选
javascript·vue.js·elementui
左手吻左脸。1 小时前
Element UI表格中根据数值动态设置字体颜色
vue.js·ui·elementui
知识分享小能手1 小时前
微信小程序入门学习教程,从入门到精通,微信小程序核心 API 详解与案例(13)
前端·javascript·学习·react.js·微信小程序·小程序·vue
小灰灰的可爱无人可替代2 小时前
记录一次使用docker和docker-compose更新vue前端项目问题
nginx·docker·vue
java水泥工1 天前
酒店客房管理系统|基于SpringBoot和Vue的酒店客房管理系统(源码+数据库+文档)
spring boot·vue·酒店管理系统·酒店客房管理系统
爱看书的小沐2 天前
【小沐学WebGIS】基于Three.JS绘制飞行轨迹Flight Tracker(Three.JS/ vue / react / WebGL)
javascript·vue·webgl·three.js·航班·航迹·飞行轨迹
知识分享小能手4 天前
微信小程序入门学习教程,从入门到精通,WXS语法详解(10)
前端·javascript·学习·微信小程序·小程序·vue·团队开发
知识分享小能手4 天前
微信小程序入门学习教程,从入门到精通,WXSS样式处理语法基础(9)
前端·javascript·vscode·学习·微信小程序·小程序·vue
sniper_fandc4 天前
Vue3双向数据绑定v-model
前端·vue
知识分享小能手5 天前
微信小程序入门学习教程,从入门到精通,WXML(WeiXin Markup Language)语法基础(8)
前端·学习·react.js·微信小程序·小程序·vue·个人开发