javascript - 在 img 点击播放视频
问题描述
我试图在图像点击上播放我的本地视频,但不知何故,视频无法播放。它显示视频“框”,但视频不会播放。单击图像后如何播放视频的任何想法?
非常感谢! HTML 部分:假设每个 img div 中都有视频 - 对于这个例子,我只包含了 1 个视频
<div class="container-gallery" id="portfolio">
<h1>Portfolio gallery</h1>
<div class="wrapper-gallery" id="index-gallery">
<div class="gallery-img img1">
<div><a href="#">First image</a></div>
<video class="hidden" width="1000" controls>
<source src="../video/#6.mp4" type="video/mp4">
</video>
</div>
<div class="gallery-img img2">
<div><a href="#">Second image</a></div>
</div>
<div class="gallery-img img3">
<div><a href="#">Third image</a></div>
</div>
<div class="gallery-img img4">
<div><a href="#">Fourth image</a></div>
</div>
<div class="gallery-img img5">
<div><a href="#">Fifth image</a></div>
</div>
<div class="gallery-img img6">
<div><a href="#">Sixth image</a></div>
</div>
<div class="gallery-img img7">
<div><a href="#">Seventh image</a></div>
</div>
</div>
</div>
CSS部分:
.img-window {
width: 90vw;
height: 92vh;
background-color: rgba(0,0,0,.8);
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
cursor: pointer;
display: flex;
justify-content: center;
align-items: center;
z-index: 100;
}
.img-window img {
max-height: 80vh;
max-width: 80vw;
z-index: 1000;
}
.hidden {
display: none;
}
.show {
display: block;
}
Javascript 部分
let galleryImages = document.querySelectorAll('.gallery-img');
let video = document.querySelector('video');
let getLatestOpenedImg;
if(galleryImages) {
galleryImages.forEach(function(image, index) {
image.onclick = function(e) {
e.preventDefault();
getLatestOpenedImg = index + 1;
let container = document.body;
let newVidWindow = document.createElement("div");
container.appendChild(newVidWindow);
newVidWindow.setAttribute("class", "img-window");
newVidWindow.setAttribute("onclick", "closeImg()");
newVidWindow.appendChild(video);
video.classList.remove('hidden');
video.classList.add('show');
video.play();
}
});
}
function closeImg() {
document.querySelector(".img-window").remove();
}
解决方案
这是您的代码未更改,它似乎工作正常。
会不会是浏览器问题阻止了嵌入式视频的播放?
let galleryImages = document.querySelectorAll('.gallery-img');
let video = document.querySelector('video');
let getLatestOpenedImg;
if(galleryImages) {
galleryImages.forEach(function(image, index) {
image.onclick = function(e) {
e.preventDefault();
getLatestOpenedImg = index + 1;
let container = document.body;
let newVidWindow = document.createElement("div");
container.appendChild(newVidWindow);
newVidWindow.setAttribute("class", "img-window");
newVidWindow.setAttribute("onclick", "closeImg()");
newVidWindow.appendChild(video);
video.classList.remove('hidden');
// not needed
//video.classList.add('show');
video.play();
}
});
}
function closeImg() {
document.querySelector(".img-window").remove();
}
.img-window {
width: 90vw;
height: 92vh;
background-color: rgba(0,0,0,.8);
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
cursor: pointer;
display: flex;
justify-content: center;
align-items: center;
z-index: 100;
}
.img-window img {
max-height: 80vh;
max-width: 80vw;
z-index: 1000;
}
.hidden {
display: none;
}
/* not needed
.show {
display: block;
}
*/
<div class="container-gallery" id="portfolio">
<h1>Portfolio gallery</h1>
<div class="wrapper-gallery" id="index-gallery">
<div class="gallery-img img1">
<div><a href="#">First image</a></div>
<video class="hidden" width="1000" controls>
<source
src="https://interactive-examples.mdn.mozilla.net/media/examples/flower.mp4"
type="video/mp4">
</video>
</div>
<div class="gallery-img img2">
<div><a href="#">Second image</a></div>
</div>
<div class="gallery-img img3">
<div><a href="#">Third image</a></div>
</div>
<div class="gallery-img img4">
<div><a href="#">Fourth image</a></div>
</div>
<div class="gallery-img img5">
<div><a href="#">Fifth image</a></div>
</div>
<div class="gallery-img img6">
<div><a href="#">Sixth image</a></div>
</div>
<div class="gallery-img img7">
<div><a href="#">Seventh image</a></div>
</div>
</div>
</div>
推荐阅读
- azure-functions - CallRecords 的 Azure 更改通知:生命周期通知对我不起作用
- ruby-on-rails - Rails MiniTest CSV.generate
- ios - 使用通知问题转移价值 | 斯威夫特 5
- python - 从 Jupyter Notebook 对 Cisco AnyConnect 进行身份验证,以便通过 VPN 访问私有 URL
- android - 没有 JRE 的 Apksigner?
- audio - OpenAL 未检测到任何播放或捕获设备
- ansible - 即使项目失败,如何获得循环的结果?
- java - 我将如何将 Sentinel 迭代实施到两种方法程序?
- python - 使用 Python Twisted,编写一个能够响应传入 APL 查询的 DNS 服务器
- python - 为什么 PyCharm 认为这个变量可能在赋值之前被引用了 while True 循环?