先完成页面效果(HTML和CSS代码),再逐个给每个按钮添加事件,控制视频播放/暂停、进度和是否全屏等功能。
时间: 2023-06-19 08:09:13 浏览: 88
使用html+css+javascript实现一个漂亮、炫酷的表白页面,给出源码
好的,下面是一个示例代码,其中包括一个视频和三个控制按钮:播放/暂停、全屏切换和音量控制。
HTML代码:
```html
<div class="video-container">
<video id="my-video" src="video.mp4"></video>
<div class="controls">
<button id="play-pause-btn">Play/Pause</button>
<button id="fullscreen-btn">Fullscreen</button>
<input id="volume-slider" type="range" min="0" max="1" step="0.1" value="1">
</div>
</div>
```
CSS样式:
```css
.video-container {
position: relative;
width: 640px;
height: 360px;
}
video {
width: 100%;
height: 100%;
object-fit: cover;
}
.controls {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 50px;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
align-items: center;
justify-content: center;
}
button {
margin: 0 10px;
background-color: transparent;
border: none;
color: #fff;
font-size: 16px;
cursor: pointer;
outline: none;
}
input[type="range"] {
width: 100px;
height: 10px;
margin: 0 10px;
-webkit-appearance: none;
background-color: #ddd;
border-radius: 5px;
outline: none;
}
input[type="range"]::-webkit-slider-thumb {
-webkit-appearance: none;
width: 20px;
height: 20px;
background-color: #fff;
border-radius: 50%;
cursor: pointer;
}
```
JavaScript代码:
```javascript
const video = document.getElementById("my-video");
const playPauseBtn = document.getElementById("play-pause-btn");
const fullscreenBtn = document.getElementById("fullscreen-btn");
const volumeSlider = document.getElementById("volume-slider");
// 播放/暂停
playPauseBtn.addEventListener("click", function() {
if (video.paused) {
video.play();
playPauseBtn.textContent = "Pause";
} else {
video.pause();
playPauseBtn.textContent = "Play";
}
});
// 全屏切换
fullscreenBtn.addEventListener("click", function() {
if (video.requestFullscreen) {
video.requestFullscreen();
} else if (video.mozRequestFullScreen) {
video.mozRequestFullScreen();
} else if (video.webkitRequestFullscreen) {
video.webkitRequestFullscreen();
}
});
// 音量控制
volumeSlider.addEventListener("input", function() {
video.volume = volumeSlider.value;
});
```
这个示例代码演示了如何使用JavaScript控制HTML5视频的播放、暂停、全屏和音量。你可以根据自己的需要进一步扩展它。
阅读全文