当前位置: 首页 > news >正文

Vue使用原生方式把视频当作背景

文章目录

  • 1. 首先我们先把布局写入:
  • 2.接下来我们要根据屏幕对视频进行自适应处理
  • 3. 此时有人会想Vue是不需要操作Dom元素的,那原生怎么适配,那么就来一个原生写法

  • 特别注意:由于视频不能写为背景,需用z-index来区分元素层级

1. 首先我们先把布局写入:

<div class="video-container"><video :style="fixStyle" autoplay loop muted class="fillWidth" :canplay="canplay" ><source src="../assets/test.mp4" type="video/mp4" /><!-- 浏览器不支持 video 标签,建议升级浏览器。 --><source src="../assets/test.mp4" type="video/webm"/><!-- 浏览器不支持 video 标签,建议升级浏览器。 --></video></div>
</div><style scoped>
.homepage-hero-module,
.video-container {position: relative;height: 100vh;overflow: hidden;
}.video-container {z-index: 0;position: absolute;
}.fillWidth {width: 100%;
}
</style>

2.接下来我们要根据屏幕对视频进行自适应处理

  mounted() {// 屏幕自适应// 获取屏幕宽高,根据不同屏幕调整视频的宽高自适应大小window.onresize = () => {const windowWidth = document.body.clientWidthconst windowHeight = document.body.clientHeightconst windowAspectRatio = windowHeight / windowWidthlet videoWidthlet videoHeightif (windowAspectRatio < 0.5625) {videoWidth = windowWidthvideoHeight = videoWidth * 0.5625this.fixStyle = {height: windowWidth * 0.5625 + 'px',width: windowWidth + 'px','margin-bottom': (windowHeight - videoHeight) / 2 + 'px','margin-left': 'initial'}} else {videoHeight = windowHeightvideoWidth = videoHeight / 0.5625this.fixStyle = {height: windowHeight + 'px',width: windowHeight / 0.5625 + 'px','margin-left': (windowWidth - videoWidth) / 2 + 'px','margin-bottom': 'initial'}}}}

3. 此时有人会想Vue是不需要操作Dom元素的,那原生怎么适配,那么就来一个原生写法

  • 书写基本样式,与Vue的基本一样,加入了object-fit: cover标签使音频自适应video宽高
<style >*{margin: 0;padding: 0; width: 100%;height: 100%;/* 清除浏览器边距用 */}.homepage-hero-module,.video-container {position: relative;heviight: 100vh;overflow: hidden;}.video-container {z-index: 0;position: absolute;}.fixStyle{object-fit: cover}.fillWidth {width: 100%;}</style><body><div class="video-container"><video class="fixStyle" autoplay loop muted class="fillWidth" :canplay="canplay" ><source src="./test.mp4" type="video/mp4" /><!-- 浏览器不支持 video 标签,建议升级浏览器。 --><source src="./test.mp4" type="video/webm"/><!-- 浏览器不支持 video 标签,建议升级浏览器。 --></video></div>
</div>
</body>
  • 重点关注下逻辑,因为原生的需要操纵Dom元素
  • 只需使用 document.querySelector('')即可,注:class使用document.querySelector('.样式名') id使用document.querySelector('#id名')
  • 由于想进入界面就看到效果,可以用立即执行函数(function () {})());
    // 屏幕自适应// 获取屏幕宽高,根据不同屏幕调整视频的宽高自适应大小
<script>(window.onresize = () => {const windowWidth = document.body.clientWidthconst windowHeight = document.body.clientHeightconst windowAspectRatio = windowHeight / windowWidth// 获取Dom节点const fixStyle = document.querySelector('.fixStyle')let videoWidthlet videoHeightif (windowAspectRatio < 0.5625) {videoWidth = windowWidthvideoHeight = videoWidth * 0.5625fixStyle.style.height = windowHeight + 'px',fixStyle.style.width =windowWidth + 'px',fixStyle.style.marginLeft = 'initial'fixStyle.style.marginBottom = (windowHeight - videoHeight) / 2 + 'px'} else {videoHeight = windowHeightvideoWidth = videoHeight / 0.5625fixStyle.style.height = windowHeight + 'px',fixStyle.style.width = windowHeight / 0.5625 + 'px',fixStyle.style.left = (windowWidth - videoWidth) / 2 + 'px',fixStyle.style.bottom = 'initial'}})()
</script>
http://www.dtcms.com/a/457625.html

相关文章:

  • 铜陵app网站做招聘信息wordpress第一篇文章id
  • 从玩具到工业:基于 CodeBuddy code CLI 构建电力变压器绕组短路智能诊断系统
  • wordpress 中英文网站模板手机创建网页
  • 基于 GEE 的 Sentinel-2 光谱、指数、纹理特征提取与 Sentinel-1 SAR 数据处理
  • 嘉兴网站排名优化价格windows 安装 wordpress
  • 2-C语言中的数据类型
  • 免费企业营销网站制作公司建网站有何意义
  • LeetCode算法日记 - Day 66: 衣橱整理、斐波那契数(含记忆化递归与动态规划总结)
  • 建行官方网站网站模块数据同步
  • HTTP 协议的基本格式
  • 【代码】洛谷 P6150 [USACO20FEB] Clock Tree S [思维]
  • 专业做网站的公司哪家好西宁网站建设公司
  • 信息安全基础知识:06认证技术
  • 哪一个网站做专栏作家好点橙色企业网站模板
  • 【区间DP】戳气球 题解
  • Ventoy下载和安装教程(图文并茂,非常详细)
  • 无向图的回路检测(广度优先并查集)
  • 磁悬浮轴承损耗:深度解析损耗机理与降耗之道
  • AI大模型赋能药物研发:破解“双十困局”的跨界革命
  • 哲林高拍仪网站开发宁波南部商务区网站建设
  • 经典的逻辑函数化简算法 Espresso
  • ZKEACMS:基于ASP.Net Core开发的开源免费内容管理系统
  • 【QT常用技术讲解】opencv实现摄像头图像检测并裁剪物体
  • 深圳建网站哪个好网页设计实训总结3000字大学篇
  • 【密码学实战】openHiTLS mac命令行:消息认证码工具
  • 沙井网站建设石林县工程建设个体交易网站
  • Chromium 138 编译指南 - Android 篇:安装构建依赖项(七)
  • asp 绿色环保企业网站源码 v1.1golang 做网站
  • 第4章 C++多线程系统编程精要
  • 绵阳网站怎样做网站设计要交税吗