视频

<video>

素材准备

点击下载素材 宽屏竖屏 或自行准备
视频素材推荐:潮点视频

简介 Introduction

标签属性 video attributes

标签属性 Attribute
item desc
src 视频的URL,推荐使用 mp4 格式
autoplay 自动播放
controls 播放控制条
loop 重复播放
muted 静音播放
preload 预加载;如果使用 autoplay,则忽略该属性
poster 视频封面图像;可以使用 gif;大小应该和视频保持一致

CSS

初始化样式 Initialization

You can use the object-position property to adjust the positioning of the video within the element's frame, and the object-fit property to control how the video's size is adjusted to fit within the frame.
There are no special considerations for styling <video>; a common strategy is to give it a display value of block to make it easier to position, size, etc., and then provide styling and layout information as required.
[] 视频的可视化使用

JavaScript

媒体属性

媒体事件

案例 Cases

[] 首页全屏背景视频
[] 视频播放
方案1:引导用户互动打开播放
方案2:先静音播放,再根据播放状态,如是否能自动播放,取消静音
[] 微课
[] 视频预览和播放
[] 使用字幕
  1. 准备视频
  2. <video src="/utils/video/flag.mp4" controls muted autoplay loop>
    </video>
  3. 准备字幕 - 手动或下载
    • 后缀名是.vtt,如 demo.vtt
    • 必须以 WEBVTT 开始;建议全部大写
    • 可以使用定位指令指定字幕出现的位置 line 和对齐 align;默认是底部、居中对齐
    WEBVTT
    
    00:00:01.000 --> 00:00:04.000
    这是第一段字幕
    
    00:00:05.000 --> 00:00:08.000 line:100%
    这是第二段字幕
    
    00:00:09.000 --> 00:00:12.000 line:90% align:left
    这是第三段字幕
  4. 引入字幕 - 使用 <track>,指定 src 等属性;default 默认开启字幕
  5. <video src="/utils/video/flag.mp4" controls muted autoplay loop>
      <track kind="captions" src="./demo.vtt" default />
    </video>
  6. 设置字幕样式 - 使用伪元素 ::cue;默认是白底黑字
  7. ::cue {
      background-color: transparent;
      font-size: 20px;
      color: #fff;
    }
  8. 优化字幕 - 提高多语言支持;需要多个语言字幕
  9. <video src="/utils/video/flag.mp4" controls muted autoplay loop>
      <track kind="subtitles" src="./demo_en.vtt" default label="English" srclang="en" />
      <track kind="subtitles" src="./demo_zh.vtt" default label="Chinese" srclang="zh" />
    </video>

总结与作业 Summary & Homework

总结 Summary

. 视频 <video>的使用
为了首页加载速度,通常不直接在主页放置视频;而是通过链接的方式转到单独的页面播放
如果坚持在首页展示,一般设置 mute和 preload,同时禁止 autoplay
JavaScript 控制时,不允许未经任何交互直接播放:play() failed because the user didn't interact with the document first.

作业 Homework

  1. 制作网站开场视频秀
  2. 制作个人视频素材库,扩充并完善到个人网站
  3. 如何使用字幕?