我建立了一个HTML5视频幻灯片。我想做的是在用户转到下一张幻灯片时暂停正在播放的所有视频。我将如何在javascript中执行此操作?到目前为止,这是我所做的:
https://codepen.io/patriciaworth/pen/wVgovq
//starting slide
var slideIndex = 1;
showSlides(slideIndex);
// Next/previous controls
function plusSlides(n) {
showSlides(slideIndex += n);
}
//calcuate number of slides and which ones not to show
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
if (n > slides.length) {
slideIndex = 1
}
if (n < 1) {
slideIndex = slides.length
}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
//pause videos here?
}
slides[slideIndex - 1].style.display = "block";
}
* {
margin: 0;
padding: 0;
border: 0;
}
html {
background: #333;
}
$page-font: Arial;
$electric-pink: #e236c8;
h1 {
color: #fff;
float: left;
font-family: Arial;
font-size: 20px;
}
.slideshow {
margin: 0 auto;
width: 600px;
}
.ss-controls {
float: right;
.ss-back,
.ss-forward {
color: #fff;
font-family: $page-font;
font-size: 16px;
margin: 0 3px;
&:hover {
color: $electric-pink;
cursor: pointer;
}
}
}
.mySlides {
margin-top: 20px;
width: 100%;
video {
width: 100%;
margin-bottom: 15px;
}
color: #e6e6e6;
font-family: $page-font;
}
/* Fading animation */
.fade {
-webkit-animation-name: fade;
-webkit-animation-duration: 1.5s;
animation-name: fade;
animation-duration: 1.5s;
}
@-webkit-keyframes fade {
from {
opacity: .4
}
to {
opacity: 1
}
}
@keyframes fade {
from {
opacity: .4
}
to {
opacity: 1
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta content="HTML5 Video Slideshow" name="description">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, min-width=320" />
<title>HTML5 Video Slideshow</title>
<!--My CSS -->
<link href="css/styles.css" rel="stylesheet">
<!-- Fontawesome -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.9.0/css/all.css" rel="stylesheet">
</head>
<body>
<div class="slideshow">
<h1>Video Slideshow</h1>
<span class="ss-controls">
<span class="ss-back">
<a class="prev" onclick="plusSlides(-1)"><i class="fas fa-caret-left fa-2x"></i></a>
</span>
<span class="ss-forward">
<a class="next" onclick="plusSlides(1)"><i class="fas fa-caret-right fa-2x"></i></a>
</span>
</span>
<div class="mySlides fade">
<video controls>
<source src="videos/mp4/Reflona - SilentCrafter.mp4" type="video/mp4">
<source src="videos/mp4/Reflona - SilentCrafter.webm" type="video/webm">
Your browser does not support the video tag.
</video> Reflona - SilentCrafter
</div>
<div class="mySlides fade">
<video controls>
<source src="videos/mp4/Fog - Dizaro.mp4" type="video/mp4">
<source src="videos/mp4/Fog - Dizaro.webm" type="video/webm">
Your browser does not support the video tag.
</video> Dizaro - Fog
</div>
<div class="mySlides fade">
<video controls>
<source src="videos/mp4/Virtual Trip - Niwel.mp4" type="video/mp4">
<source src="videos/mp4/Virtual Trip - Niwel.webm" type="video/webm">
Your browser does not support the video tag.
</video> Virtual Trip - Niwel
</div>
</div>
<!--Slideshow js-->
<script src="js/slideshow.js"></script>
</body>
</html>
请注意,由于我无法上传视频,因此这些视频不可见。
答案 0 :(得分:1)
您需要为视频提供一个ID号,以便以后您可以使用相同的值来暂停或播放。
一种简单的逻辑是检查当前幻灯片的n
,然后暂停(与id
不相等的任何视频(按n
)。
例如:假设您有5张幻灯片...
n
变量的范围为1
至5
。<video>
,id=
,vid1
等中的vid2
命名每个幻灯片自己的vid3
标记... 使用!=
检查不等于状态。
在FOR
循环中,检测哪个幻灯片不是等于当前的“ n
”。如果不等于n
,则暂停。
记住:
!=
的意思是“ IS-NOT-EQUAL
”(例如: if ( abc != xyz ) then do...
||
的意思是“ OR ELSE
”(例如: if ( (abc == true) || (xyz == true) ) then do...
示例代码:
var tmpElement; var n = 0;
n = 3; //# let's assume now showing slide number 3...
tmpElement = document.getElementById("vid"+n); //get video tag with id of "vid3"...
for (i = 0; i < (slides.length); i++)
{
if( i != n) //if "i" is NOT-EQUAL-TO our 3...
{
//# pause videos here?
tmpElement = document.getElementById("vid"+i); tmpElement.pause(); //pause any not current "n" video element
}
}
tmpElement.play(); //play video #3
我希望以上内容足以说明如何修复代码逻辑。
下面也是一个更长的例子。它将3个视频并排放置,将播放一个当前视频,同时暂停其他两个视频。当前视频已循环播放...
<!DOCTYPE html>
<html>
<body>
<div> <a id="myVideoTag"> </a> </div>
<video width="320" height="240" id="vid0" >
<source src="null.mp4" type="video/mp4">
</video>
<video width="320" height="240" id="vid1" >
<source src="null.mp4" type="video/mp4">
</video>
<video width="320" height="240" id="vid2" >
<source src="null.mp4" type="video/mp4">
</video>
<div onclick="do_Menu_Clicks(this);" id="btn_prev" div style="font-size:14px; width:30;
float:left; width: 100px; height:40px; background: rgba(213, 104, 176, 0.5);
position:absolute; top: 500px; left: 200px; z-index: 3" >
<b> # PREV </b>
</div>
<div onclick="do_Menu_Clicks(this);" id="btn_next" div style="font-size:14px; width:30;
float:left; width: 100px; height:40px; background: rgba(64, 169, 116, 0.5);
position:absolute; top: 500px; left: 400px; z-index: 3" >
<b> # NEXT </b>
</div>
<script>
var path1 = "your-path-to/video-1.mp4";
var path2 = "your-path-to/video-2.mp4";
var path3 = "your-path-to/video-3.mp4";
tmpElement = document.getElementById("vid0"); tmpElement.setAttribute("src", path1);
tmpElement = document.getElementById("vid1"); tmpElement.setAttribute("src", path2);
tmpElement = document.getElementById("vid2"); tmpElement.setAttribute("src", path3);
var i;
var n = -1; //start at "-1" since "0" means first slide...
var slides = ["vid0", "vid1", "vid2"]; //# first slide is at zero index... eg: [0,1,2,3]
//starting slide
var slideIndex = n; //1;
showSlides(slideIndex);
// Next/previous controls
//function plusSlides(n) { showSlides(slideIndex += n); }
function do_Menu_Clicks ( input )
{
//# update for next slide
if (input.id == "btn_next")
{ if ( n < (slides.length-1) ) { n++; } }
if (input.id == "btn_prev")
{ if (n > 0) {n--;} }
//# check if BIGGER than ( || means "OR ELSE" ) if SMALLER than..
if ( (n > slides.length-1) || (n < 0) ) { slideIndex = 0; } //reset to zero
showSlides(n);
}
//calcuate number of slides and which ones not to show
function showSlides(n)
{
tmpElement = document.getElementById("vid"+n); //tmpElement.play();
var playVidPromise = tmpElement.play();
if (playVidPromise !== undefined) { playVidPromise.then(_ => {}).catch(error => { }); }
for (i = 0; i < (slides.length); i++)
{
if( i != n)
{
//slides[i].style.display = "none";
//# pause videos here?
tmpElement = document.getElementById("vid"+i); tmpElement.pause(); //pause any not current "n" video element
}
}
}
</script>
</body>
</html>