使用html写一个视频网站
时间: 2024-05-26 21:09:58 浏览: 17
可以使用HTML5的<video>标签来嵌入视频,同时再结合CSS和JavaScript实现样式和交互。
示例代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>视频网站</title>
<style>
/* 样式 */
body {
background-color: #f5f5f5;
font-family: Arial, sans-serif;
}
h1 {
text-align: center;
margin-top: 50px;
}
.video-container {
margin: 50px auto;
width: 640px;
height: 360px;
position: relative;
}
video {
width: 100%;
height: 100%;
object-fit: cover;
position: absolute;
top: 0;
left: 0;
}
#play-button {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 50px;
color: #fff;
background-color: rgba(0, 0, 0, 0.5);
border-radius: 50%;
padding: 20px;
cursor: pointer;
}
/* JS */
#play-button:hover {
background-color: rgba(0, 0, 0, 0.8);
}
#play-button:focus {
outline: none;
}
.video-container.playing #play-button {
display: none;
}
/* 播放按钮 */
.play {
height: 0;
width: 0;
border-top: 20px solid transparent;
border-bottom: 20px solid transparent;
border-left: 30px solid #fff;
position: relative;
left: 10px;
}
.pause {
height: 40px;
width: 10px;
background-color: #fff;
position: relative;
left: 10px;
}
.pause::before, .pause::after {
content: "";
display: block;
height: 100%;
width: 10px;
background-color: #fff;
position: absolute;
top: 0;
left: -20px;
}
.pause::after {
left: 20px;
}
</style>
</head>
<body>
<h1>视频网站</h1>
<div class="video-container">
<video id="my-video">
<source src="example.mp4" type="video/mp4">
</video>
<button id="play-button">
<span class="play"></span>
</button>
</div>
<script>
const myVideo = document.getElementById("my-video");
const playButton = document.getElementById("play-button");
const videoContainer = document.querySelector(".video-container");
playButton.addEventListener("click", function() {
myVideo.play();
videoContainer.classList.add("playing");
this.innerHTML = '<span class="pause"></span>';
});
myVideo.addEventListener("ended", function() {
videoContainer.classList.remove("playing");
playButton.innerHTML = '<span class="play"></span>';
});
</script>
</body>
</html>