实现视频播放,视频切换,点赞功能
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>快音视频</title>
<strong>快音视频</strong>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
background: #000;
color: #fff;
height: 100vh;
}
.video-container {
display: flex;
flex-direction: column;
align-items: center;
position: relative;
width: 80vw; /* 宽度占据视口宽度的80% */
height: calc(80vw * 9 / 16); /* 高度设置为宽度的9/16,保持16:9的比例 */
}
.video-item {
position: relative;
text-align: center;
width: 100%;
height: 100%;
display: none; /* Hide all videos initially */
}
.video {
width: 100%;
height: 100%;
object-fit: cover; /* 保持视频比例并覆盖整个容器 */
border-radius: 8px;
}
.video-title {
font-size: 1.5rem;
font-weight: bold;
position: absolute;
top: 5%;
left: 50%;
transform: translateX(-50%);
background: rgba(0, 0, 0, 0.7);
padding: 5px 10px;
border-radius: 5px;
}
.like-btn {
background: none;
border: none;
font-size: 2rem;
color: #ccc; /* 初始状态为灰色 */
position: absolute;
bottom: 10%;
cursor: pointer;
transition: color 0.3s;
}
.like-btn.liked {
color: #ff0000; /* 点赞状态为红色 */
}
.like-count {
color: #fff;
font-size: 1.5rem;
position: absolute;
bottom: 10%;
left: 3rem;
}
.controls {
display: flex;
justify-content: center;
margin-top: 1rem;
}
.control-btn {
background: #ff0000;
border: none;
color: #fff;
font-size: 1.5rem;
padding: 10px 20px;
margin: 0 10px;
cursor: pointer;
border-radius: 5px;
transition: background 0.3s;
}
.control-btn:hover {
background: #cc0000;
}
</style>
</head>
<body>
<div class="video-container">
<!-- 视频列表 -->
<div class="video-item" id="video1">
<div class="video-title">@幻想家姜一时</div>
<video src="G:\douyin\WeChat_20240915104509.mp4" class="video" controls></video>
<button class="like-btn" data-liked="false">❤️</button>
<span class="like-count">0</span>
</div>
<div class="video-item" id="video2">
<div class="video-title">@Liffyt</div>
<video src="G:\douyin\WeChat_20240915112150.mp4" class="video" controls></video>
<button class="like-btn" data-liked="false">❤️</button>
<span class="like-count">0</span>
</div>
<div class="controls">
<button class="control-btn" id="prevVideo">上一视频</button>
<button class="control-btn" id="nextVideo">下一视频</button>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const videoItems = document.querySelectorAll('.video-item');
const prevButton = document.getElementById('prevVideo');
const nextButton = document.getElementById('nextVideo');
let currentIndex = 0;
const updateVideo = (index) => {
videoItems.forEach((item, idx) => {
const video = item.querySelector('.video');
if (idx === index) {
item.style.display = 'block';
video.play();
} else {
item.style.display = 'none';
video.pause();
video.currentTime = 0; // 重置视频时间
}
});
};
const handleLikeButtonClick = (event) => {
const button = event.currentTarget;
const liked = button.getAttribute('data-liked') === 'true';
const likeCountElem = button.nextElementSibling;
let likeCount = parseInt(likeCountElem.textContent, 10);
if (liked) {
button.setAttribute('data-liked', 'false');
button.classList.remove('liked');
likeCount--;
} else {
button.setAttribute('data-liked', 'true');
button.classList.add('liked');
likeCount++;
}
likeCountElem.textContent = likeCount;
};
prevButton.addEventListener('click', () => {
currentIndex = (currentIndex - 1 + videoItems.length) % videoItems.length;
updateVideo(currentIndex);
});
nextButton.addEventListener('click', () => {
currentIndex = (currentIndex + 1) % videoItems.length;
updateVideo(currentIndex);
});
videoItems.forEach((item, index) => {
const video = item.querySelector('.video');
const likeButton = item.querySelector('.like-btn');
video.addEventListener('ended', () => {
currentIndex = (currentIndex + 1) % videoItems.length;
updateVideo(currentIndex);
});
likeButton.addEventListener('click', handleLikeButtonClick);
});
updateVideo(currentIndex);
});
</script>
</body>
</html>