Đặc tả HTML5 đã giới thiệu phần tử
2, có thể được sử dụng để nhúng nội dung video vào các trang web và được hỗ trợ tốt trên các trình duyệt hiện đại. Trong bài đăng này, chúng ta sẽ tìm hiểu cách nhúng trình phát video HTML5 vào tài liệu HTML và xác định các điều khiển tùy chỉnh cho nó độc lập với mặc định của trình duyệt5
6
Custom HTML5 Video Player7
11
12
14 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
18 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
21
Your browser doesn't support HTML5 video.
24
Đây là một liên kết đến bản demo CodeSandbox
Thiết lập dự án
Chạy lệnh sau để tạo ba tệp.
3,5
6
Custom HTML5 Video Player7
11
12
14 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
18 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
21
Your browser doesn't support HTML5 video.
24
4,5
6
Custom HTML5 Video Player7
11
12
14 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
18 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
21
Your browser doesn't support HTML5 video.
24
5 trong một thư mục có tên là5
6
Custom HTML5 Video Player7
11
12
14 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
18 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
21
Your browser doesn't support HTML5 video.
24
65
6
Custom HTML5 Video Player7
11
12
14 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
18 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
21
Your browser doesn't support HTML5 video.
24
sao chép1mkdir custom-video-player
3touch index.html style.css script.js
Để nhúng trình phát video vào ứng dụng của chúng tôi, hãy thêm phần sau vào tệp
3 của chúng tôi5
6
Custom HTML5 Video Player7
11
12
14 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
18 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
21
Your browser doesn't support HTML5 video.
24
sao chép5
6
Custom HTML5 Video Player7
11
12
14 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
18 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
21
Your browser doesn't support HTML5 video.
24
Trong đoạn mã trên, chúng tôi đã hiển thị phần tử
8. Bạn có thể thêm video từ máy tính cục bộ của mình vào thuộc tính5
6
Custom HTML5 Video Player7
11
12
14 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
18 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
21
Your browser doesn't support HTML5 video.
24
9 hoặc sử dụng liên kết Cloudinary mặc định được sử dụng trong mã. Đặc tả HTML5 hiện hỗ trợ ba định dạng video, nhưng chúng tôi đã sử dụng nhiều thẻ5
6
Custom HTML5 Video Player7
11
12
14 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
18 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
21
Your browser doesn't support HTML5 video.
24
2 để cung cấp hai định dạng của cùng một video trong MP4 và WebM. Ngoài ra, chúng tôi đã chỉ định nội dung được xác định trước sẽ được hiển thị cho tác nhân người dùng không hỗ trợ phần tử5
6
Custom HTML5 Video Player7
11
12
14 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
18 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
21
Your browser doesn't support HTML5 video.
24
13 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650352188/production.mp4"
7 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650352188/production.mp4"
10
Your browser doesn't support HTML5 video.
Thẻ
1 cũng chấp nhận một số thuộc tính gốc tùy chọn, chẳng hạn như3 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650352188/production.mp4"
7 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650352188/production.mp4"
10
Your browser doesn't support HTML5 video.
3, hiển thị giao diện điều khiển trình phát tiêu chuẩn khi được chỉ định hoặc đặt thành true. Nhấp để tìm hiểu thêm về các thuộc tính khác3 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650352188/production.mp4"
7 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650352188/production.mp4"
10
Your browser doesn't support HTML5 video.
Trước khi tiếp tục, hãy thêm một số kiểu để ứng dụng của chúng ta có giao diện đẹp. Điền tệp
4 của bạn với các kiểu từ liên kết CodeSandbox này. Nếu bạn xem trước trang trong trình duyệt của mình, bạn sẽ thấy trình phát video được nhúng như bên dưới5
6
Custom HTML5 Video Player7
11
12
14 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
18 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
21
Your browser doesn't support HTML5 video.
24
Tùy chỉnh Trình phát video
Trước khi tùy chỉnh trình phát video, chúng ta cần xóa thuộc tính
3, thuộc tính hiển thị các điều khiển video như3 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650352188/production.mp4"
7 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650352188/production.mp4"
10
Your browser doesn't support HTML5 video.
6,3 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650352188/production.mp4"
7 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650352188/production.mp4"
10
Your browser doesn't support HTML5 video.
7, v.v. , sau đó xây dựng các điều khiển tùy chỉnh của chúng tôi. Cập nhật phần tử3 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650352188/production.mp4"
7 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650352188/production.mp4"
10
Your browser doesn't support HTML5 video.
2 trong tệp5
6
Custom HTML5 Video Player7
11
12
14 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
18 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
21
Your browser doesn't support HTML5 video.
24
3 của bạn như hình bên dưới5
6
Custom HTML5 Video Player7
11
12
14 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
18 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
21
Your browser doesn't support HTML5 video.
24
sao chép3 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650352188/production.mp4"
7 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650352188/production.mp4"
10
Your browser doesn't support HTML5 video.
Nếu bạn kiểm tra trình duyệt của mình, bạn sẽ thấy trình phát không tương tác
Chuyển đổi Trạng thái Tạm dừng/Phát
Để thêm chức năng tạm dừng và phát cho trình phát của chúng tôi, hãy cập nhật tệp
3 của chúng tôi với nội dung sau5
6
Custom HTML5 Video Player7
11
12
14 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
18 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
21
Your browser doesn't support HTML5 video.
24
sao chép1
2 //...
4 ►
Thêm phần sau vào tệp
5 của bạn với phần sau để thêm một số tương tác5
6
Custom HTML5 Video Player7
11
12
14 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
18 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
21
Your browser doesn't support HTML5 video.
24
sao chép1const video = document.querySelector[".video"];
2const toggleButton = document.querySelector[".toggleButton"];
4 if [video.paused || video.ended] {
10function updateToggleButton[] {
11 toggleButton.innerHTML = video.paused ? "►" : "❚❚";
13toggleButton.addEventListener["click", togglePlay];
14video.addEventListener["click", togglePlay];
15video.addEventListener["play", updateToggleButton];
16video.addEventListener["pause", updateToggleButton];
Chúng tôi đã bắt đầu bằng cách chọn phần tử
1, cho phép truy cập vào các thuộc tính và phương thức của video. Sau đó, chúng tôi đã chọn phần tử3 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650352188/production.mp4"
7 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650352188/production.mp4"
10
Your browser doesn't support HTML5 video.
3 và thêm một trình xử lý sự kiện vào phần tử đó để kích hoạt hàm1
2 //...
4 ►
4 khi nhấp vào. Hàm1
2 //...
4 ►
4 thực thi một điều kiện chuyển đổi phát hoặc tạm dừng video dựa trên trạng thái hiện tại của video1
2 //...
4 ►
Tiếp theo, chúng tôi đã thêm trình xử lý sự kiện
6 và1
2 //...
4 ►
7 vào phần tử1
2 //...
4 ►
1 gọi hàm3 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650352188/production.mp4"
7 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650352188/production.mp4"
10
Your browser doesn't support HTML5 video.
9, hàm này cập nhật dựa trên trạng thái của video1
2 //...
4 ►
Sau đó, bạn sẽ có thể tạm dừng và phát video bằng cách nhấp vào nút hoặc chính video đó
Triển khai thanh tiến trình
Để thêm thanh tiến trình cập nhật video khi phát, hãy cập nhật tệp
3 của bạn với thông tin sau5
6
Custom HTML5 Video Player7
11
12
14 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
18 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
21
Your browser doesn't support HTML5 video.
24
sao chép2
6
8 ►
Để làm cho thanh tiến trình có tính tương tác, hãy cập nhật tệp
5 của bạn để trông như thế này5
6
Custom HTML5 Video Player7
11
12
14 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
18 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
21
Your browser doesn't support HTML5 video.
24
sao chép1const video = document.querySelector[".video"];
2const toggleButton = document.querySelector[".toggleButton"];
3const progress = document.querySelector[".progress"];
4const progressBar = document.querySelector[".progress__filled"];
7 if [video.paused || video.ended] {
14function updateToggleButton[] {
15 toggleButton.innerHTML = video.paused ? "►" : "❚ ❚";
18function handleProgress[] {
19 const progressPercentage = [video.currentTime / video.duration] * 100;
20 progressBar.style.flexBasis = `${progressPercentage}%`;
24 const scrubTime = [e.offsetX / progress.offsetWidth] * video.duration;
25 video.currentTime = scrubTime;
28toggleButton.addEventListener["click", togglePlay];
29video.addEventListener["click", togglePlay];
30video.addEventListener["play", updateToggleButton];
31video.addEventListener["pause", updateToggleButton];
33video.addEventListener["timeupdate", handleProgress];
34progress.addEventListener["click", scrub];
36progress.addEventListener["mousedown", [] => [mousedown = true]];
37progress.addEventListener["mousemove", [e] => mousedown && scrub[e]];
38progress.addEventListener["mouseup", [] => [mousedown = false]];
Chúng tôi đã chọn phần tử DOM của thanh tiến trình và thêm một trình lắng nghe sự kiện vào đối tượng
1 thực thi hàm3 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650352188/production.mp4"
7 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650352188/production.mp4"
10
Your browser doesn't support HTML5 video.
3 trên1const video = document.querySelector[".video"];
2const toggleButton = document.querySelector[".toggleButton"];
4 if [video.paused || video.ended] {
10function updateToggleButton[] {
11 toggleButton.innerHTML = video.paused ? "►" : "❚❚";
13toggleButton.addEventListener["click", togglePlay];
14video.addEventListener["click", togglePlay];
15video.addEventListener["play", updateToggleButton];
16video.addEventListener["pause", updateToggleButton];
4. Hàm1const video = document.querySelector[".video"];
2const toggleButton = document.querySelector[".toggleButton"];
4 if [video.paused || video.ended] {
10function updateToggleButton[] {
11 toggleButton.innerHTML = video.paused ? "►" : "❚❚";
13toggleButton.addEventListener["click", togglePlay];
14video.addEventListener["click", togglePlay];
15video.addEventListener["play", updateToggleButton];
16video.addEventListener["pause", updateToggleButton];
3 tính toán tỷ lệ phần trăm thời gian hiện tại của video so với tổng thời lượng của video, sau đó cập nhật kiểu khi video tiếp tục1const video = document.querySelector[".video"];
2const toggleButton = document.querySelector[".toggleButton"];
4 if [video.paused || video.ended] {
10function updateToggleButton[] {
11 toggleButton.innerHTML = video.paused ? "►" : "❚❚";
13toggleButton.addEventListener["click", togglePlay];
14video.addEventListener["click", togglePlay];
15video.addEventListener["play", updateToggleButton];
16video.addEventListener["pause", updateToggleButton];
Chúng tôi cũng đã thêm chức năng
6 để lọc video đến một điểm cụ thể liên quan đến thanh tiến trình1const video = document.querySelector[".video"];
2const toggleButton = document.querySelector[".toggleButton"];
4 if [video.paused || video.ended] {
10function updateToggleButton[] {
11 toggleButton.innerHTML = video.paused ? "►" : "❚❚";
13toggleButton.addEventListener["click", togglePlay];
14video.addEventListener["click", togglePlay];
15video.addEventListener["play", updateToggleButton];
16video.addEventListener["pause", updateToggleButton];
Lưu các thay đổi và xem trước ứng dụng trong trình duyệt của bạn để xem trạng thái cập nhật của trình phát video
Thêm thanh trượt âm lượng và tốc độ phát lại
Để thêm thanh trượt tùy chỉnh sẽ được sử dụng để quản lý trạng thái âm lượng và tốc độ phát lại, hãy cập nhật tệp
3 của bạn với thông tin sau5
6
Custom HTML5 Video Player7
11
12
14 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
18 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
21
Your browser doesn't support HTML5 video.
24
sao chép1
2 //...
6
8 ►
14 class="controls__slider"
23 class="controls__slider"
Chúng tôi đã thêm hai thẻ
8 với giá trị1const video = document.querySelector[".video"];
2const toggleButton = document.querySelector[".toggleButton"];
4 if [video.paused || video.ended] {
10function updateToggleButton[] {
11 toggleButton.innerHTML = video.paused ? "►" : "❚❚";
13toggleButton.addEventListener["click", togglePlay];
14video.addEventListener["click", togglePlay];
15video.addEventListener["play", updateToggleButton];
16video.addEventListener["pause", updateToggleButton];
9 của1const video = document.querySelector[".video"];
2const toggleButton = document.querySelector[".toggleButton"];
4 if [video.paused || video.ended] {
10function updateToggleButton[] {
11 toggleButton.innerHTML = video.paused ? "►" : "❚❚";
13toggleButton.addEventListener["click", togglePlay];
14video.addEventListener["click", togglePlay];
15video.addEventListener["play", updateToggleButton];
16video.addEventListener["pause", updateToggleButton];
0 với giá trị được xác định trước là2
6
8 ►
1 và2
6
8 ►
2 cho âm lượng và tốc độ phát lại, tương ứng2
6
8 ►
Bây giờ hãy cập nhật tệp
5 của bạn bằng mã sau để làm cho các thanh trượt hoạt động5
6
Custom HTML5 Video Player7
11
12
14 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
18 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
21
Your browser doesn't support HTML5 video.
24
sao chép1const video = document.querySelector[".video"];
2const toggleButton = document.querySelector[".toggleButton"];
3const progress = document.querySelector[".progress"];
4const progressBar = document.querySelector[".progress__filled"];
6const sliders = document.querySelectorAll[".contols__slider"];
10function updateToggleButton[] {
13function handleProgress[] {
19function handleSliderUpdate[] {
20 video[this.name] = this.value;
23sliders.forEach[[slider] => {
24 slider.addEventListener["change", handleSliderUpdate];
Chúng tôi đã chọn các phần tử
4 và đính kèm một trình xử lý sự kiện cho mỗi phần tử đó để kích hoạt hàm2
6
8 ►
5. Hàm này sử dụng các thuộc tính2
6
8 ►
6 và2
6
8 ►
7 của thanh trượt để xác định trạng thái của thanh trượt2
6
8 ►
Nếu bạn xem trước trang trong trình duyệt của mình, bạn sẽ thấy các thanh trượt
Thêm điều khiển bỏ qua
Thêm phần sau vào tệp
3 của bạn5
6
Custom HTML5 Video Player7
11
12
14 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
18 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
21
Your browser doesn't support HTML5 video.
24
sao chép1
//...
4
6 ►
7
10 « 10s
11 10s »
Chúng tôi đã thêm hai nút có thuộc tính dữ liệu sẽ được truy cập bằng JavaScript. Để làm cho các nút hoạt động, hãy cập nhật tệp
5 của bạn với thông tin sau5
6
Custom HTML5 Video Player7
11
12
14 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
18 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
21
Your browser doesn't support HTML5 video.
24
0sao chép5
6
Custom HTML5 Video Player7
11
12
14 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
18 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
21
Your browser doesn't support HTML5 video.
24
Chúng tôi đã chọn các nút của mình bằng phương pháp
0 và thêm một trình nghe vào mỗi nút gọi hàm1const video = document.querySelector[".video"];
2const toggleButton = document.querySelector[".toggleButton"];
3const progress = document.querySelector[".progress"];
4const progressBar = document.querySelector[".progress__filled"];
7 if [video.paused || video.ended] {
14function updateToggleButton[] {
15 toggleButton.innerHTML = video.paused ? "►" : "❚ ❚";
18function handleProgress[] {
19 const progressPercentage = [video.currentTime / video.duration] * 100;
20 progressBar.style.flexBasis = `${progressPercentage}%`;
24 const scrubTime = [e.offsetX / progress.offsetWidth] * video.duration;
25 video.currentTime = scrubTime;
28toggleButton.addEventListener["click", togglePlay];
29video.addEventListener["click", togglePlay];
30video.addEventListener["play", updateToggleButton];
31video.addEventListener["pause", updateToggleButton];
33video.addEventListener["timeupdate", handleProgress];
34progress.addEventListener["click", scrub];
36progress.addEventListener["mousedown", [] => [mousedown = true]];
37progress.addEventListener["mousemove", [e] => mousedown && scrub[e]];
38progress.addEventListener["mouseup", [] => [mousedown = false]];
1 khi nhấp. Hàm này sau đó sẽ cập nhật thời gian hiện tại của video dựa trên giá trị được chỉ định trong thuộc tính1const video = document.querySelector[".video"];
2const toggleButton = document.querySelector[".toggleButton"];
3const progress = document.querySelector[".progress"];
4const progressBar = document.querySelector[".progress__filled"];
7 if [video.paused || video.ended] {
14function updateToggleButton[] {
15 toggleButton.innerHTML = video.paused ? "►" : "❚ ❚";
18function handleProgress[] {
19 const progressPercentage = [video.currentTime / video.duration] * 100;
20 progressBar.style.flexBasis = `${progressPercentage}%`;
24 const scrubTime = [e.offsetX / progress.offsetWidth] * video.duration;
25 video.currentTime = scrubTime;
28toggleButton.addEventListener["click", togglePlay];
29video.addEventListener["click", togglePlay];
30video.addEventListener["play", updateToggleButton];
31video.addEventListener["pause", updateToggleButton];
33video.addEventListener["timeupdate", handleProgress];
34progress.addEventListener["click", scrub];
36progress.addEventListener["mousedown", [] => [mousedown = true]];
37progress.addEventListener["mousemove", [e] => mousedown && scrub[e]];
38progress.addEventListener["mouseup", [] => [mousedown = false]];
21const video = document.querySelector[".video"];
2const toggleButton = document.querySelector[".toggleButton"];
3const progress = document.querySelector[".progress"];
4const progressBar = document.querySelector[".progress__filled"];
7 if [video.paused || video.ended] {
14function updateToggleButton[] {
15 toggleButton.innerHTML = video.paused ? "►" : "❚ ❚";
18function handleProgress[] {
19 const progressPercentage = [video.currentTime / video.duration] * 100;
20 progressBar.style.flexBasis = `${progressPercentage}%`;
24 const scrubTime = [e.offsetX / progress.offsetWidth] * video.duration;
25 video.currentTime = scrubTime;
28toggleButton.addEventListener["click", togglePlay];
29video.addEventListener["click", togglePlay];
30video.addEventListener["play", updateToggleButton];
31video.addEventListener["pause", updateToggleButton];
33video.addEventListener["timeupdate", handleProgress];
34progress.addEventListener["click", scrub];
36progress.addEventListener["mousedown", [] => [mousedown = true]];
37progress.addEventListener["mousemove", [e] => mousedown && scrub[e]];
38progress.addEventListener["mouseup", [] => [mousedown = false]];
Thêm phím tắt
Chúng ta có thể tận dụng sự kiện bàn phím mặc định
3 để tùy chỉnh thêm trình phát video. Để thêm phím tắt vào ứng dụng, hãy thêm phím tắt này vào cuối tệp1const video = document.querySelector[".video"];
2const toggleButton = document.querySelector[".toggleButton"];
3const progress = document.querySelector[".progress"];
4const progressBar = document.querySelector[".progress__filled"];
7 if [video.paused || video.ended] {
14function updateToggleButton[] {
15 toggleButton.innerHTML = video.paused ? "►" : "❚ ❚";
18function handleProgress[] {
19 const progressPercentage = [video.currentTime / video.duration] * 100;
20 progressBar.style.flexBasis = `${progressPercentage}%`;
24 const scrubTime = [e.offsetX / progress.offsetWidth] * video.duration;
25 video.currentTime = scrubTime;
28toggleButton.addEventListener["click", togglePlay];
29video.addEventListener["click", togglePlay];
30video.addEventListener["play", updateToggleButton];
31video.addEventListener["pause", updateToggleButton];
33video.addEventListener["timeupdate", handleProgress];
34progress.addEventListener["click", scrub];
36progress.addEventListener["mousedown", [] => [mousedown = true]];
37progress.addEventListener["mousemove", [e] => mousedown && scrub[e]];
38progress.addEventListener["mouseup", [] => [mousedown = false]];
5 của bạn5
6
Custom HTML5 Video Player7
11
12
14 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
18 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
21
Your browser doesn't support HTML5 video.
24
1sao chép5
6
Custom HTML5 Video Player7
11
12
14 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
18 src="//res.cloudinary.com/ifeomaimoh/video/upload/v1650567487/speech_ofvfzq.mp4"
21
Your browser doesn't support HTML5 video.
24
Để đơn giản hóa mọi thứ, chúng tôi chỉ thêm một phím tắt bật/tắt phát khi nhấn phím cách trên bàn phím. Bạn có thể ghi đối tượng sự kiện vào bảng điều khiển để xem các thuộc tính khác
Tìm dự án ở đây trên GitHub
Phần kết luận
Cho đến nay, chúng ta đã xem xét phần tử video HTML5 mặc định và cách tạo các điều khiển tùy chỉnh cho phần tử đó, nhưng bạn không cần phải dừng ở đây. Thông tin thêm có sẵn trong tài liệu chính thức