html点击div播放视频,CSS2 video点击中间播放按钮播放视频代码
时间: 2023-08-10 07:05:18 浏览: 126
HTML代码:
```html
<div class="video-container">
<video src="video.mp4"></video>
<div class="play-btn"></div>
</div>
```
CSS2代码:
```css
.video-container {
position: relative;
width: 100%;
height: 0;
padding-bottom: 56.25%; /* 16:9 aspect ratio */
}
.video-container video {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: cover;
}
.play-btn {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 60px;
height: 60px;
border-radius: 50%;
background-color: rgba(0, 0, 0, 0.5);
cursor: pointer;
z-index: 1;
}
.play-btn:before {
content: "";
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 0;
height: 0;
border-top: 15px solid transparent;
border-bottom: 15px solid transparent;
border-left: 25px solid white;
}
.play-btn:hover {
background-color: rgba(0, 0, 0, 0.7);
}
.play-btn:hover:before {
border-left: 25px solid black;
}
.video-container video:focus {
outline: none;
}
.play-btn:focus {
outline: none;
}
.video-container video:not(:focus):not(:hover) + .play-btn {
display: block;
}
.video-container video:not(:focus):not(:hover) {
pointer-events: none;
}
.play-btn:focus + video,
.play-btn:hover + video {
cursor: pointer;
pointer-events: auto;
}
.play-btn:focus + video:focus,
.play-btn:hover + video:focus {
outline: none;
}
.play-btn:focus:before,
.play-btn:hover:before {
border-left: 25px solid black;
}
```
这段代码实现了点击 `div` 播放视频,以及点击中间的播放按钮播放视频的功能。视频播放器使用的是 HTML5 自带的 `video` 标签。CSS2 实现了视频容器的自适应大小,以及播放按钮的样式和交互效果。请注意,CSS3 有更多的功能和特性,可以让这段代码更加简洁和优美。
阅读全文