🐕️又是学习代码的一天呢(纯学习)
我首先声明,俺不是原创。这个是我从皮皮社那里最先看到的,在夏末那里扒来的,我只是单纯的想学习代码。
代码如下:
<div class="post_content padding blur animated fadeIn">
<!-- 视频播放容器 16:10 比例 -->
<div style="max-width: 460px; width: 92%; margin: 24px auto; position: relative; height: 0; padding-bottom: 62.5%; border-radius: 8px; overflow: hidden; box-shadow: 0 4px 16px rgba(0,0,0,.15); background: #1a1a1a;">
<video id="beautyVideo" controls poster="https://v2.xxapi.cn/api/wapmeinvpic" preload="metadata" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; object-fit: cover; outline: none; border: none;">
<source id="videoSource" src="https://wzapi.com/api/sjxjjsp?t=1777449316751" type="video/mp4">
您的浏览器不支持视频播放,请更新浏览器
</video>
</div>
<!-- 按钮容器 -->
<div style="max-width: 460px; width: 92%; margin: 0 auto 30px; display: flex; gap: 16px; justify-content: center; align-items: center;">
<button id="autoPlayBtn" style="padding: 11px 28px; border: 0; border-radius: 999px; background: #ff4d4f; color: #fff; font-size: 14px; cursor: pointer; transition: background 0.3s ease; box-shadow: 0 2px 8px rgba(255,77,79,.25);">关闭连续播放</button>
<button id="nextVideoBtn" style="padding: 11px 28px; border: 0; border-radius: 999px; background: #444950; color: #fff; font-size: 14px; cursor: pointer; transition: background 0.3s ease; box-shadow: 0 2px 8px rgba(0,0,0,.2);">重试中(1)...</button>
</div>
<script>
(function() {
// 核心配置
const CONFIG = {
VIDEO_API: 'https://wzapi.com/api/sjxjjsp',
LOAD_TIMEOUT: 5000,
RETRY_DELAY: 1000,
MAX_RETRY: 3
};
const elements = {
video: document.getElementById('beautyVideo'),
source: document.getElementById('videoSource'),
autoBtn: document.getElementById('autoPlayBtn'),
nextBtn: document.getElementById('nextVideoBtn')
};
const state = {
isAutoPlay: true, // ★默认开启连续播放
isLoading: false,
loadTimeoutTimer: null,
retryCount: 0,
isManualNext: false
};
if (!elements.video || !elements.source || !elements.autoBtn || !elements.nextBtn) {
console.error('元素加载失败');
return;
}
function getVideoUrl() {
const timestamp = new Date().getTime();
return `${CONFIG.VIDEO_API}?t=${timestamp}`;
}
function clearLoadTimeout() {
if (state.loadTimeoutTimer) {
clearTimeout(state.loadTimeoutTimer);
state.loadTimeoutTimer = null;
}
}
function setButtonLoading(loading) {
state.isLoading = loading;
elements.nextBtn.textContent = loading ? '加载中...' : '播放下一个视频';
}
function autoPlayVideo() {
elements.video.play().catch(err => console.log('自动播放被阻止'));
}
function loadNextVideo(isManual = false) {
if (state.isLoading) return;
clearLoadTimeout();
state.isManualNext = isManual;
setButtonLoading(true);
elements.source.src = getVideoUrl();
state.loadTimeoutTimer = setTimeout(() => {
handleLoadError('timeout');
}, CONFIG.LOAD_TIMEOUT);
elements.video.load();
}
function handleLoadSuccess() {
clearLoadTimeout();
setButtonLoading(false);
state.retryCount = 0;
if (state.isManualNext || state.isAutoPlay) {
autoPlayVideo();
}
state.isManualNext = false;
}
function handleLoadError(reason) {
clearLoadTimeout();
state.retryCount++;
if (state.retryCount <= CONFIG.MAX_RETRY) {
console.warn(`加载失败(${reason}),第${state.retryCount}次重试...`);
elements.nextBtn.textContent = `重试中(${state.retryCount})...`;
setTimeout(() => loadNextVideo(state.isManualNext), CONFIG.RETRY_DELAY);
} else {
setButtonLoading(false);
elements.nextBtn.textContent = '加载失败,点击重试';
state.retryCount = 0;
}
}
// 视频事件
elements.video.addEventListener('loadeddata', handleLoadSuccess);
elements.video.addEventListener('error', () => handleLoadError('video_error'));
elements.video.addEventListener('ended', () => {
if (state.isAutoPlay) loadNextVideo(false);
});
// 自动播放按钮(切换连续播放开关)
elements.autoBtn.onclick = function() {
state.isAutoPlay = !state.isAutoPlay;
this.textContent = state.isAutoPlay ? '关闭连续播放' : '开启连续播放';
this.style.background = state.isAutoPlay ? '#ff4d4f' : '#1677ff';
if (state.isAutoPlay && elements.video.ended) {
loadNextVideo(false);
}
};
// 下一个按钮
elements.nextBtn.onclick = function() {
if (!state.isLoading) {
state.retryCount = 0;
loadNextVideo(true);
}
};
// 悬浮改为背景高亮
[elements.autoBtn, elements.nextBtn].forEach(btn => {
btn.onmouseenter = () => {
if(btn.id === 'autoPlayBtn'){
btn.style.background = '#e53e3f';
}else{
btn.style.background = '#33383f';
}
};
btn.onmouseleave = () => {
if(btn.id === 'autoPlayBtn'){
btn.style.background = state.isAutoPlay ? '#ff4d4f' : '#1677ff';
}else{
btn.style.background = '#444950';
}
};
});
// 初始加载
loadNextVideo(false);
})();
</script>
</div>

哇咔咔,反正视频也是别人的。哈哈哈
活抓皮哥一个
对,但凡有问题,都是夏末的事。
我也想学习一下代码。
哇哈哈,我只是单纯的爱学习而已。