Nút phát/tạm dừng tùy chỉnh video html5

❮ Tham chiếu DOM âm thanh/video HTML

Thí dụ

Video có các nút phát và tạm dừng

var vid = tài liệu. getElementById("Video của tôi");

function playVid() {
    vid. phát();
}

function pauseVid() {
    vid. tạm dừng();
}

Tự mình thử »


Định nghĩa và cách sử dụng

Phương thức pause() tạm dừng (tạm dừng) âm thanh hoặc video đang phát

Mẹo. Sử dụng phương thức play() để bắt đầu phát âm thanh/video hiện tại


Hỗ trợ trình duyệt

Các số trong bảng chỉ định phiên bản trình duyệt đầu tiên hỗ trợ đầy đủ phương thức

Tạm dừng phương thức() Có9. 0CóCóCó

cú pháp

Thông số

Giá trị trả về


❮ Tham chiếu DOM âm thanh/video HTML

Hướng dẫn xây dựng video HTML5. Mô-đun video tiêu chuẩn

Bước 2. Thêm điều khiển video tùy chỉnh

Tiếp theo. Bước 3. Lắng nghe các sự kiện video

Thêm các điều khiển video bằng cách thiết lập các nút tùy chỉnh trong quảng cáo của bạn

Bước 1. Thêm các nút và kiểu dáng

Tạo một

.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
6 cho mỗi nút điều khiển video (phát, tạm dừng, v.v. ) trong tệp HTML và cung cấp cho mỗi tệp một ID duy nhất. Sau đó tạo kiểu cho chúng trong tệp CSS

Các thẻ mẫu trong tệp HTML

.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
7

Kiểu dáng mẫu trong tệp CSS

.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
8

Bước 2. Thêm mã JavaScript để xử lý các sự kiện

Trong tệp JavaScript của quảng cáo, hãy chỉ định các biến cho từng nút điều khiển video, sau đó thêm trình xử lý sự kiện. Tiếp theo, thêm mã để phản hồi khi mỗi nút được nhấp vào

Trong bài viết trước về trình phát video trên nhiều trình duyệt, chúng tôi đã mô tả cách xây dựng trình phát video HTML trên nhiều trình duyệt bằng cách sử dụng API Phương tiện và Toàn màn hình. Bài viết tiếp theo này xem xét cách tạo kiểu cho trình phát tùy chỉnh này, bao gồm cả việc làm cho nó phản hồi nhanh

Ví dụ trong hành động

Nút phát/tạm dừng tùy chỉnh video html5

Bạn có thể tìm mã cho ví dụ được cập nhật, theo kiểu trên GitHub và xem trực tiếp

Sửa đổi sơ bộ từ ví dụ ban đầu

Phần này tóm tắt các sửa đổi đã được thực hiện đối với ví dụ trình phát video ban đầu để giúp tác vụ tạo kiểu dễ dàng hơn trước khi phần lớn công việc được bắt đầu

Đánh dấu HTML

Có một vài thay đổi đã được thực hiện đối với đánh dấu HTML được hiển thị trong bài viết trước. Các điều khiển video tùy chỉnh và phần tử

.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
2 hiện được chứa trong phần tử
.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
3, thay vì nằm bên trong các mục danh sách không có thứ tự

Đánh dấu cho các điều khiển tùy chỉnh bây giờ trông như sau

<div id="video-controls" class="controls" data-state="hidden">
  <button id="playpause" type="button" data-state="play">Play/Pausebutton>
  <button id="stop" type="button" data-state="stop">Stopbutton>
  <div class="progress">
    <progress id="progress" value="0" min="0">
      <span id="progress-bar">span>
    progress>
  div>
  <button id="mute" type="button" data-state="mute">Mute/Unmutebutton>
  <button id="volinc" type="button" data-state="volup">Vol+button>
  <button id="voldec" type="button" data-state="voldown">Vol-button>
  <button id="fs" type="button" data-state="go-fullscreen">Fullscreenbutton>
div>

Thay đổi CSS liên quan

Bài viết trước đặt thuộc tính

.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
4 của điều khiển video thành
.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
5 để hiển thị chúng. Điều này hiện đã được thay đổi để sử dụng thuộc tính
.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
6 mà mã này đã sử dụng để xử lý việc triển khai toàn màn hình của nó

Ý tưởng "trạng thái dữ liệu" này cũng được sử dụng để đặt trạng thái hiện tại của các nút trong bộ điều khiển video, cho phép tạo kiểu trạng thái cụ thể

JavaScript

Như đã đề cập ở trên, thuộc tính

.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
6 được sử dụng ở nhiều nơi khác nhau cho mục đích tạo kiểu và những thuộc tính này được đặt bằng JavaScript. Việc triển khai cụ thể sẽ được đề cập ở những nơi thích hợp dưới đây

tạo kiểu

Kiểu trình phát video kết quả được sử dụng ở đây khá cơ bản — điều này là có chủ ý, vì mục đích là để cho thấy cách một trình phát video như vậy có thể được tạo kiểu và đáp ứng

Ghi chú. trong một số trường hợp, một số CSS cơ bản bị bỏ qua trong các mã ví dụ ở đây vì việc sử dụng nó rõ ràng hoặc không liên quan cụ thể đến việc tạo kiểu cho trình phát video

kiểu dáng cơ bản

Video HTML và các điều khiển của nó đều được chứa trong một phần tử

.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
8, được cung cấp chiều rộng và chiều cao tối đa (dựa trên kích thước của video được sử dụng) và được căn giữa trong trang

________số 8

Bản thân bộ chứa điều khiển video cũng cần một số kiểu dáng để nó được thiết lập đúng cách

.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}

Chiều cao của lớp

.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
9 được đặt là (rất chính xác. ) tỷ lệ phần trăm của phần tử
.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
8 kèm theo (điều này đã được thực hiện bằng thử nghiệm dựa trên chiều cao của nút được yêu cầu). Vị trí của nó cũng được đặt cụ thể thành
.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
21, cần thiết cho khả năng phản hồi của nó (sẽ nói thêm về điều đó sau)

Như đã đề cập trước đó, thuộc tính

.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
6 hiện được sử dụng để cho biết liệu các điều khiển video có hiển thị hay không và các điều khiển này cũng cần được tạo kiểu

.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
2

Có một số thuộc tính cũng cần được đặt cho tất cả các thành phần trong điều khiển video

.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
5

Tất cả các phần tử được thả nổi bên trái, vì chúng được căn chỉnh cạnh nhau và mỗi phần tử được đặt để có

.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
23 gần 4% (một lần nữa, giá trị thực được tính dựa trên kích thước bắt buộc của các nút) và
.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
24 . Giá trị cho
.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
25 cũng được đặt, nhưng phần tử đầu tiên (trong trường hợp này là nút phát/tạm dừng) có thuộc tính này bị ghi đè bởi giá trị 0

Vùng chứa

.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
3 cho phần tử
.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
2 cũng yêu cầu một số cài đặt cụ thể;

<div id="video-controls" class="controls" data-state="hidden">
  <button id="playpause" type="button" data-state="play">Play/Pausebutton>
  <button id="stop" type="button" data-state="stop">Stopbutton>
  <div class="progress">
    <progress id="progress" value="0" min="0">
      <span id="progress-bar">span>
    progress>
  div>
  <button id="mute" type="button" data-state="mute">Mute/Unmutebutton>
  <button id="volinc" type="button" data-state="volup">Vol+button>
  <button id="voldec" type="button" data-state="voldown">Vol-button>
  <button id="fs" type="button" data-state="go-fullscreen">Fullscreenbutton>
div>
1

nút

Nhiệm vụ tạo kiểu chính đầu tiên cần giải quyết là làm cho các nút của điều khiển video thực sự trông giống và hoạt động như các nút thực

Mỗi nút có một số kiểu dáng cơ bản

<div id="video-controls" class="controls" data-state="hidden">
  <button id="playpause" type="button" data-state="play">Play/Pausebutton>
  <button id="stop" type="button" data-state="stop">Stopbutton>
  <div class="progress">
    <progress id="progress" value="0" min="0">
      <span id="progress-bar">span>
    progress>
  div>
  <button id="mute" type="button" data-state="mute">Mute/Unmutebutton>
  <button id="volinc" type="button" data-state="volup">Vol+button>
  <button id="voldec" type="button" data-state="voldown">Vol-button>
  <button id="fs" type="button" data-state="go-fullscreen">Fullscreenbutton>
div>
2

Theo mặc định, tất cả các phần tử của

.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
28 đều có đường viền, vì vậy phần tử này sẽ bị xóa. Vì hình nền sẽ được sử dụng để hiển thị các biểu tượng phù hợp nên màu nền của nút được đặt thành trong suốt, không lặp lại và phần tử phải chứa đầy đủ hình ảnh

Các trạng thái

.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
29 và
.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
50 đơn giản sau đó được đặt cho từng nút làm thay đổi độ mờ của nút

<div id="video-controls" class="controls" data-state="hidden">
  <button id="playpause" type="button" data-state="play">Play/Pausebutton>
  <button id="stop" type="button" data-state="stop">Stopbutton>
  <div class="progress">
    <progress id="progress" value="0" min="0">
      <span id="progress-bar">span>
    progress>
  div>
  <button id="mute" type="button" data-state="mute">Mute/Unmutebutton>
  <button id="volinc" type="button" data-state="volup">Vol+button>
  <button id="voldec" type="button" data-state="voldown">Vol-button>
  <button id="fs" type="button" data-state="go-fullscreen">Fullscreenbutton>
div>
6

Để có được hình ảnh nút phù hợp, một bộ biểu tượng bộ điều khiển chung miễn phí đã được tải xuống từ web. Sau đó, mỗi hình ảnh được chuyển đổi thành chuỗi được mã hóa base64 (sử dụng bộ mã hóa hình ảnh base64 trực tuyến), vì hình ảnh khá nhỏ nên chuỗi được mã hóa kết quả khá ngắn

Vì một số nút có chức năng kép, e. g. phát/tạm dừng và tắt tiếng/bật tiếng, các nút này có các trạng thái khác nhau cần được tạo kiểu. Như đã đề cập trước đó, một biến

.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
6 được sử dụng để cho biết các nút đó hiện đang ở trạng thái nào.

Ví dụ: nút phát/tạm dừng có các định nghĩa hình nền sau (toàn bộ chuỗi base64 đã được bỏ qua cho ngắn gọn)

<div id="video-controls" class="controls" data-state="hidden">
  <button id="playpause" type="button" data-state="play">Play/Pausebutton>
  <button id="stop" type="button" data-state="stop">Stopbutton>
  <div class="progress">
    <progress id="progress" value="0" min="0">
      <span id="progress-bar">span>
    progress>
  div>
  <button id="mute" type="button" data-state="mute">Mute/Unmutebutton>
  <button id="volinc" type="button" data-state="volup">Vol+button>
  <button id="voldec" type="button" data-state="voldown">Vol-button>
  <button id="fs" type="button" data-state="go-fullscreen">Fullscreenbutton>
div>
8

Khi thay đổi

.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
6 của nút, hình ảnh thích hợp cũng sẽ được thay đổi. Tất cả các nút khác được xử lý theo cách tương tự

Thanh tiến trình

Phần tử

.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
2 có kiểu cơ bản được thiết lập như sau

.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
1

Giống như các phần tử

.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
28,
.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
2 cũng có một đường viền mặc định, được loại bỏ ở đây. Nó cũng được bo tròn một chút vì lý do thẩm mỹ. Thuộc tính
.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
56 cũng được xác định ở đây vì Internet Explorer sử dụng màu đã xác định này để tạo kiểu cho màu nền của thanh tiến trình khi nó tăng lên

Như đã đề cập trong bài viết trước, có một dự phòng được cung cấp cho các trình duyệt không hỗ trợ phần tử

.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
2;

figure {
  max-width: 64rem;
  width: 100%;
  max-height: 30.875rem;
  height: 100%;
  margin: 1.25rem auto;
  padding: 1.051%;
  background-color: #666;
}
0

Lớp

.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
58 cũng được sử dụng ở đây khi phần tử
.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
2 đang bị "làm giả"; . Phần tử
<div id="video-controls" class="controls" data-state="hidden">
  <button id="playpause" type="button" data-state="play">Play/Pausebutton>
  <button id="stop" type="button" data-state="stop">Stopbutton>
  <div class="progress">
    <progress id="progress" value="0" min="0">
      <span id="progress-bar">span>
    progress>
  div>
  <button id="mute" type="button" data-state="mute">Mute/Unmutebutton>
  <button id="volinc" type="button" data-state="volup">Vol+button>
  <button id="voldec" type="button" data-state="voldown">Vol-button>
  <button id="fs" type="button" data-state="go-fullscreen">Fullscreenbutton>
div>
10 bên trong được sử dụng làm phần tiến trình thực tế của thanh tiến trình giả có chiều rộng ban đầu được đặt thành 0% (được cập nhật qua JavaScript) và nó cũng được đặt màu nền

Có một số thuộc tính dành riêng cho trình duyệt cần được đặt để đảm bảo rằng Firefox và Chrome sử dụng màu bắt buộc cho thanh tiến trình

figure {
  max-width: 64rem;
  width: 100%;
  max-height: 30.875rem;
  height: 100%;
  margin: 1.25rem auto;
  padding: 1.051%;
  background-color: #666;
}
1

Mặc dù các thuộc tính giống nhau được đặt thành cùng một giá trị nhưng các quy tắc này cần được xác định riêng, nếu không Chrome sẽ bỏ qua

JavaScript

Đó thực sự là kiểu dáng ngay lập tức;

Kiểm soát khả năng hiển thị

Thay đổi đầu tiên rất đơn giản.

.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
6 để hiển thị các điều khiển video khi JavaScript có sẵn cho trình duyệt hiện cần được đặt

figure {
  max-width: 64rem;
  width: 100%;
  max-height: 30.875rem;
  height: 100%;
  margin: 1.25rem auto;
  padding: 1.051%;
  background-color: #666;
}
2

Hỗ trợ thanh tiến trình

Cũng cần kiểm tra để thiết lập thanh tiến trình "giả" nếu trình duyệt không hỗ trợ phần tử

.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
2

figure {
  max-width: 64rem;
  width: 100%;
  max-height: 30.875rem;
  height: 100%;
  margin: 1.25rem auto;
  padding: 1.051%;
  background-color: #666;
}
3

chức năng nút

Phần này xem xét JavaScript cần thiết để triển khai chức năng của nút

Phát/Tạm dừng và Tắt tiếng

Giờ đây, các nút thực sự trông giống như các nút và có hình ảnh cho biết chức năng của chúng, cần thực hiện một số thay đổi để các nút "chức năng kép" (chẳng hạn như nút phát/tạm dừng) ở đúng "trạng thái" và hiển thị . Để tạo điều kiện thuận lợi cho việc này, một hàm mới được định nghĩa có tên là

<div id="video-controls" class="controls" data-state="hidden">
  <button id="playpause" type="button" data-state="play">Play/Pausebutton>
  <button id="stop" type="button" data-state="stop">Stopbutton>
  <div class="progress">
    <progress id="progress" value="0" min="0">
      <span id="progress-bar">span>
    progress>
  div>
  <button id="mute" type="button" data-state="mute">Mute/Unmutebutton>
  <button id="volinc" type="button" data-state="volup">Vol+button>
  <button id="voldec" type="button" data-state="voldown">Vol-button>
  <button id="fs" type="button" data-state="go-fullscreen">Fullscreenbutton>
div>
13, hàm này chấp nhận một biến kiểu cho biết chức năng của nút

figure {
  max-width: 64rem;
  width: 100%;
  max-height: 30.875rem;
  height: 100%;
  margin: 1.25rem auto;
  padding: 1.051%;
  background-color: #666;
}
4

Chức năng này sau đó được gọi bởi trình xử lý sự kiện có liên quan

figure {
  max-width: 64rem;
  width: 100%;
  max-height: 30.875rem;
  height: 100%;
  margin: 1.25rem auto;
  padding: 1.051%;
  background-color: #666;
}
5

Bạn có thể nhận thấy rằng có những trình xử lý mới trong đó các sự kiện

<div id="video-controls" class="controls" data-state="hidden">
  <button id="playpause" type="button" data-state="play">Play/Pausebutton>
  <button id="stop" type="button" data-state="stop">Stopbutton>
  <div class="progress">
    <progress id="progress" value="0" min="0">
      <span id="progress-bar">span>
    progress>
  div>
  <button id="mute" type="button" data-state="mute">Mute/Unmutebutton>
  <button id="volinc" type="button" data-state="volup">Vol+button>
  <button id="voldec" type="button" data-state="voldown">Vol-button>
  <button id="fs" type="button" data-state="go-fullscreen">Fullscreenbutton>
div>
14 và
<div id="video-controls" class="controls" data-state="hidden">
  <button id="playpause" type="button" data-state="play">Play/Pausebutton>
  <button id="stop" type="button" data-state="stop">Stopbutton>
  <div class="progress">
    <progress id="progress" value="0" min="0">
      <span id="progress-bar">span>
    progress>
  div>
  <button id="mute" type="button" data-state="mute">Mute/Unmutebutton>
  <button id="volinc" type="button" data-state="volup">Vol+button>
  <button id="voldec" type="button" data-state="voldown">Vol-button>
  <button id="fs" type="button" data-state="go-fullscreen">Fullscreenbutton>
div>
15 được phản hồi trên video. Có một lý do cho việc này. Mặc dù bộ điều khiển video mặc định của trình duyệt đã bị tắt, nhưng nhiều trình duyệt vẫn có thể truy cập chúng bằng cách nhấp chuột phải vào video HTML. Điều này có nghĩa là người dùng có thể phát/tạm dừng video từ các điều khiển này, sau đó sẽ khiến các nút của bộ điều khiển tùy chỉnh không đồng bộ. Nếu người dùng sử dụng các điều khiển mặc định, thì các sự kiện Media API đã xác định — chẳng hạn như
<div id="video-controls" class="controls" data-state="hidden">
  <button id="playpause" type="button" data-state="play">Play/Pausebutton>
  <button id="stop" type="button" data-state="stop">Stopbutton>
  <div class="progress">
    <progress id="progress" value="0" min="0">
      <span id="progress-bar">span>
    progress>
  div>
  <button id="mute" type="button" data-state="mute">Mute/Unmutebutton>
  <button id="volinc" type="button" data-state="volup">Vol+button>
  <button id="voldec" type="button" data-state="voldown">Vol-button>
  <button id="fs" type="button" data-state="go-fullscreen">Fullscreenbutton>
div>
14 và
<div id="video-controls" class="controls" data-state="hidden">
  <button id="playpause" type="button" data-state="play">Play/Pausebutton>
  <button id="stop" type="button" data-state="stop">Stopbutton>
  <div class="progress">
    <progress id="progress" value="0" min="0">
      <span id="progress-bar">span>
    progress>
  div>
  <button id="mute" type="button" data-state="mute">Mute/Unmutebutton>
  <button id="volinc" type="button" data-state="volup">Vol+button>
  <button id="voldec" type="button" data-state="voldown">Vol-button>
  <button id="fs" type="button" data-state="go-fullscreen">Fullscreenbutton>
div>
15 — sẽ được nâng lên để có thể tận dụng lợi thế này nhằm đảm bảo rằng các nút điều khiển tùy chỉnh được giữ đồng bộ. Để đảm bảo điều này, một trình xử lý nhấp chuột mới cần được xác định cho nút phát/tạm dừng để nó cũng tăng các sự kiện
<div id="video-controls" class="controls" data-state="hidden">
  <button id="playpause" type="button" data-state="play">Play/Pausebutton>
  <button id="stop" type="button" data-state="stop">Stopbutton>
  <div class="progress">
    <progress id="progress" value="0" min="0">
      <span id="progress-bar">span>
    progress>
  div>
  <button id="mute" type="button" data-state="mute">Mute/Unmutebutton>
  <button id="volinc" type="button" data-state="volup">Vol+button>
  <button id="voldec" type="button" data-state="voldown">Vol-button>
  <button id="fs" type="button" data-state="go-fullscreen">Fullscreenbutton>
div>
14 và
<div id="video-controls" class="controls" data-state="hidden">
  <button id="playpause" type="button" data-state="play">Play/Pausebutton>
  <button id="stop" type="button" data-state="stop">Stopbutton>
  <div class="progress">
    <progress id="progress" value="0" min="0">
      <span id="progress-bar">span>
    progress>
  div>
  <button id="mute" type="button" data-state="mute">Mute/Unmutebutton>
  <button id="volinc" type="button" data-state="volup">Vol+button>
  <button id="voldec" type="button" data-state="voldown">Vol-button>
  <button id="fs" type="button" data-state="go-fullscreen">Fullscreenbutton>
div>
15

figure {
  max-width: 64rem;
  width: 100%;
  max-height: 30.875rem;
  height: 100%;
  margin: 1.25rem auto;
  padding: 1.051%;
  background-color: #666;
}
6

Âm lượng

Chức năng

<div id="video-controls" class="controls" data-state="hidden">
  <button id="playpause" type="button" data-state="play">Play/Pausebutton>
  <button id="stop" type="button" data-state="stop">Stopbutton>
  <div class="progress">
    <progress id="progress" value="0" min="0">
      <span id="progress-bar">span>
    progress>
  div>
  <button id="mute" type="button" data-state="mute">Mute/Unmutebutton>
  <button id="volinc" type="button" data-state="volup">Vol+button>
  <button id="voldec" type="button" data-state="voldown">Vol-button>
  <button id="fs" type="button" data-state="go-fullscreen">Fullscreenbutton>
div>
20, được gọi khi bấm vào các nút âm lượng của trình phát, cũng thay đổi — giờ đây chức năng này gọi một chức năng mới có tên là
<div id="video-controls" class="controls" data-state="hidden">
  <button id="playpause" type="button" data-state="play">Play/Pausebutton>
  <button id="stop" type="button" data-state="stop">Stopbutton>
  <div class="progress">
    <progress id="progress" value="0" min="0">
      <span id="progress-bar">span>
    progress>
  div>
  <button id="mute" type="button" data-state="mute">Mute/Unmutebutton>
  <button id="volinc" type="button" data-state="volup">Vol+button>
  <button id="voldec" type="button" data-state="voldown">Vol-button>
  <button id="fs" type="button" data-state="go-fullscreen">Fullscreenbutton>
div>
21

figure {
  max-width: 64rem;
  width: 100%;
  max-height: 30.875rem;
  height: 100%;
  margin: 1.25rem auto;
  padding: 1.051%;
  background-color: #666;
}
7

Chức năng

<div id="video-controls" class="controls" data-state="hidden">
  <button id="playpause" type="button" data-state="play">Play/Pausebutton>
  <button id="stop" type="button" data-state="stop">Stopbutton>
  <div class="progress">
    <progress id="progress" value="0" min="0">
      <span id="progress-bar">span>
    progress>
  div>
  <button id="mute" type="button" data-state="mute">Mute/Unmutebutton>
  <button id="volinc" type="button" data-state="volup">Vol+button>
  <button id="voldec" type="button" data-state="voldown">Vol-button>
  <button id="fs" type="button" data-state="go-fullscreen">Fullscreenbutton>
div>
21 mới này thực hiện tương tự như chức năng
<div id="video-controls" class="controls" data-state="hidden">
  <button id="playpause" type="button" data-state="play">Play/Pausebutton>
  <button id="stop" type="button" data-state="stop">Stopbutton>
  <div class="progress">
    <progress id="progress" value="0" min="0">
      <span id="progress-bar">span>
    progress>
  div>
  <button id="mute" type="button" data-state="mute">Mute/Unmutebutton>
  <button id="volinc" type="button" data-state="volup">Vol+button>
  <button id="voldec" type="button" data-state="voldown">Vol-button>
  <button id="fs" type="button" data-state="go-fullscreen">Fullscreenbutton>
div>
20 nhưng nó cũng đặt trạng thái của nút tắt tiếng tùy thuộc vào cài đặt âm lượng hiện tại của video.
<div id="video-controls" class="controls" data-state="hidden">
  <button id="playpause" type="button" data-state="play">Play/Pausebutton>
  <button id="stop" type="button" data-state="stop">Stopbutton>
  <div class="progress">
    <progress id="progress" value="0" min="0">
      <span id="progress-bar">span>
    progress>
  div>
  <button id="mute" type="button" data-state="mute">Mute/Unmutebutton>
  <button id="volinc" type="button" data-state="volup">Vol+button>
  <button id="voldec" type="button" data-state="voldown">Vol-button>
  <button id="fs" type="button" data-state="go-fullscreen">Fullscreenbutton>
div>
21 cũng được gọi khi sự kiện
<div id="video-controls" class="controls" data-state="hidden">
  <button id="playpause" type="button" data-state="play">Play/Pausebutton>
  <button id="stop" type="button" data-state="stop">Stopbutton>
  <div class="progress">
    <progress id="progress" value="0" min="0">
      <span id="progress-bar">span>
    progress>
  div>
  <button id="mute" type="button" data-state="mute">Mute/Unmutebutton>
  <button id="volinc" type="button" data-state="volup">Vol+button>
  <button id="voldec" type="button" data-state="voldown">Vol-button>
  <button id="fs" type="button" data-state="go-fullscreen">Fullscreenbutton>
div>
25 được nâng lên

figure {
  max-width: 64rem;
  width: 100%;
  max-height: 30.875rem;
  height: 100%;
  margin: 1.25rem auto;
  padding: 1.051%;
  background-color: #666;
}
8

Thanh tiến trình

Một thay đổi nhỏ cũng cần được thực hiện đối với trình xử lý nhấp chuột cho phần tử

.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
2. Vì phần tử
.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
8 kèm theo hiện đã đặt
<div id="video-controls" class="controls" data-state="hidden">
  <button id="playpause" type="button" data-state="play">Play/Pausebutton>
  <button id="stop" type="button" data-state="stop">Stopbutton>
  <div class="progress">
    <progress id="progress" value="0" min="0">
      <span id="progress-bar">span>
    progress>
  div>
  <button id="mute" type="button" data-state="mute">Mute/Unmutebutton>
  <button id="volinc" type="button" data-state="volup">Vol+button>
  <button id="voldec" type="button" data-state="voldown">Vol-button>
  <button id="fs" type="button" data-state="go-fullscreen">Fullscreenbutton>
div>
28 trên đó nên các tính toán được thực hiện bởi trình xử lý nhấp chuột này không chính xác. Bây giờ nó cũng cần tính đến vị trí bù của phần tử cha

figure {
  max-width: 64rem;
  width: 100%;
  max-height: 30.875rem;
  height: 100%;
  margin: 1.25rem auto;
  padding: 1.051%;
  background-color: #666;
}
9

Toàn màn hình

Việc triển khai FullScreen không thay đổi

kiểu dáng đáp ứng

Giờ đây, trình phát đã có giao diện cơ bản được chăm sóc, một số thay đổi kiểu dáng khác - liên quan đến truy vấn phương tiện - cần được thực hiện để làm cho trình phát phản hồi nhanh

Trình phát hiện hoạt động khá tốt cho đến khi được hiển thị trên màn hình "trung bình" (e. g. 1024px/64em) hoặc nhỏ hơn. Trong trường hợp này, các lề và phần đệm trên phần tử

.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
8 cần được loại bỏ để tận dụng tất cả không gian có sẵn và các nút hơi quá nhỏ nên điều này cần được thay đổi bằng cách đặt chiều cao mới trên phần tử đó

.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
0

Điều này hoạt động đủ tốt cho đến khi nó được xem trên màn hình nhỏ hơn (680px/42. 5em), do đó, một điểm dừng khác được thực hiện tại đây. Vì chiều cao của phần tử lớp

.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
9 bây giờ sẽ thay đổi, nên chiều cao cố định không còn cần thiết nữa — do đó, nó được đặt thành
<div id="video-controls" class="controls" data-state="hidden">
  <button id="playpause" type="button" data-state="play">Play/Pausebutton>
  <button id="stop" type="button" data-state="stop">Stopbutton>
  <div class="progress">
    <progress id="progress" value="0" min="0">
      <span id="progress-bar">span>
    progress>
  div>
  <button id="mute" type="button" data-state="mute">Mute/Unmutebutton>
  <button id="volinc" type="button" data-state="volup">Vol+button>
  <button id="voldec" type="button" data-state="voldown">Vol-button>
  <button id="fs" type="button" data-state="go-fullscreen">Fullscreenbutton>
div>
62. Các định nghĩa cho các phần tử trong phần tử
.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
9 bây giờ cũng cần được thay đổi

.controls {
  width: 100%;
  height: 8.0971659919028340080971659919028%; /* of figure's height */
  position: relative;
}
1

Hộp chứa

<div id="video-controls" class="controls" data-state="hidden">
  <button id="playpause" type="button" data-state="play">Play/Pausebutton>
  <button id="stop" type="button" data-state="stop">Stopbutton>
  <div class="progress">
    <progress id="progress" value="0" min="0">
      <span id="progress-bar">span>
    progress>
  div>
  <button id="mute" type="button" data-state="mute">Mute/Unmutebutton>
  <button id="volinc" type="button" data-state="volup">Vol+button>
  <button id="voldec" type="button" data-state="voldown">Vol-button>
  <button id="fs" type="button" data-state="go-fullscreen">Fullscreenbutton>
div>
64 hiện được chuyển lên trên cùng của bộ điều khiển thông qua
<div id="video-controls" class="controls" data-state="hidden">
  <button id="playpause" type="button" data-state="play">Play/Pausebutton>
  <button id="stop" type="button" data-state="stop">Stopbutton>
  <div class="progress">
    <progress id="progress" value="0" min="0">
      <span id="progress-bar">span>
    progress>
  div>
  <button id="mute" type="button" data-state="mute">Mute/Unmutebutton>
  <button id="volinc" type="button" data-state="volup">Vol+button>
  <button id="voldec" type="button" data-state="voldown">Vol-button>
  <button id="fs" type="button" data-state="go-fullscreen">Fullscreenbutton>
div>
65, do đó, hộp chứa này và tất cả các nút cần phải rộng hơn. Ngoài ra, các nút cần được đẩy bên dưới vùng chứa tiến trình để chúng có thể nhìn thấy được