gpt4 book ai didi

javascript - 单独播放/暂停多个 HTML5 视频

转载 作者:行者123 更新时间:2023-12-01 01:17:37 25 4
gpt4 key购买 nike

任何人都可以帮我重写以下代码,使其在不使用 ID 的情况下工作,以便我可以将其用于单个页面上的多个视频吗?预先感谢您!!

HTML MARKUP

<div id="video-container">
<video id="video" width="640" height="365" src="https://d3vv6lp55qjaqc.cloudfront.net/items/3V3g280Q1z441P071g3E/tnt-lidcap-web.mp4" type="video/mp4" autoplay loop muted></video>
<div id="video-controls">
<button id="play-pause" type="button" aria-label="toggle pause play">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 120 120" width="120" height="120">
<circle cx="60" cy="60" r="60" fill="#000" fill-opacity=".5" />
<path stroke="#fff" stroke-linecap="round" stroke-width="10" d="M74.933 33v54M45.067 33v54" id="pauseIcon" class="video-pause" style="visibility: visible" />
<path fill="#fff" stroke="#fff" stroke-linejoin="round" stroke-width="8" d="M54 33L54 87 81 60z" id="playIcon" class="video-play" style="visibility: hidden" />
</svg>
</button>
</div>
</div>
JAVASCRIPT (WORKS FOR SINGLE VIDEO)
window.onload = function() {

var video = document.getElementById("video");
var playButton = document.getElementById("play-pause");
var pauseIcon = document.getElementById("pauseIcon");
var playIcon = document.getElementById("playIcon");

playButton.addEventListener("click", function() {
if (video.paused == true) {
video.play();
pauseIcon.style.visibility ="visible";
playIcon.style.visibility ="hidden";
} else {
video.pause();
pauseIcon.style.visibility ="hidden";
playIcon.style.visibility ="visible";
}
});
}
Here is a link to my current working CodePen as well:
https://codepen.io/jhughes1006/pen/yZPVGo

最佳答案

您可以用 class 替换每个 id 并使用 .querySelectorAll() 执行相同的任务。

onload = e => {
const containers = document.querySelectorAll('.video-container'):
containers.forEach(container => {
const video = container.querySelector('video');
const playButton = container.querySelector('button');
playButton.addEventListener('click', e => {
// do stuff
})
})
}

关于javascript - 单独播放/暂停多个 HTML5 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54596652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com