js使用video循环播放视频列表,请给我写个详细完整的例子
时间: 2024-06-10 17:07:12 浏览: 159
当然,我很乐意为您提供一个JS循环播放视频列表的例子,以下是代码:
HTML:
```html
<div class="video-container"></div>
```
JS:
```javascript
const videoList = [
{
src: "https://example.com/video1.mp4",
title: "Video 1"
},
{
src: "https://example.com/video2.mp4",
title: "Video 2"
},
{
src: "https://example.com/video3.mp4",
title: "Video 3"
}
];
const videoContainer = document.querySelector(".video-container");
let currentVideoIndex = 0;
function playNextVideo() {
if (currentVideoIndex >= videoList.length) {
currentVideoIndex = 0;
}
const currentVideo = videoList[currentVideoIndex];
// 创建新的video元素
const videoElement = document.createElement("video");
videoElement.src = currentVideo.src;
videoElement.controls = true;
// 创建一个标题标签并插入
const titleElement = document.createElement("h2");
titleElement.textContent = currentVideo.title;
videoContainer.appendChild(titleElement);
// 插入video元素
videoContainer.appendChild(videoElement);
// 当视频播完了,播放下一个视频
videoElement.addEventListener("ended", () => {
currentVideoIndex++;
playNextVideo();
});
}
playNextVideo();
```
上面的代码会创建一个视频容器并按照给定的列表循环播放视频。如有任何需求更新,请随时告诉我,我将为您提供支持。
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)