gpttemp
This commit is contained in:
72
code/public/static/js/lazy.js
Normal file
72
code/public/static/js/lazy.js
Normal file
@@ -0,0 +1,72 @@
|
||||
(function () {
|
||||
if (window.__LAZY_LOADER_READY__) return;
|
||||
|
||||
const hasIO = "IntersectionObserver" in window;
|
||||
|
||||
function applyBg(el) {
|
||||
const src = el.getAttribute("data-src");
|
||||
if (src) el.style.backgroundImage = `url(${src})`;
|
||||
el.classList.remove("lazyload-bg");
|
||||
el.removeAttribute("data-src");
|
||||
}
|
||||
|
||||
function applyImg(img) {
|
||||
const src = img.getAttribute("data-src");
|
||||
const srcset = img.getAttribute("data-srcset");
|
||||
const sizes = img.getAttribute("data-sizes");
|
||||
|
||||
if (sizes) img.setAttribute("sizes", sizes);
|
||||
if (srcset) img.setAttribute("srcset", srcset);
|
||||
if (src) img.setAttribute("src", src);
|
||||
|
||||
img.classList.remove("lazyload-img");
|
||||
img.removeAttribute("data-src");
|
||||
img.removeAttribute("data-srcset");
|
||||
img.removeAttribute("data-sizes");
|
||||
}
|
||||
|
||||
function observe(elements, callback, options) {
|
||||
if (!elements || elements.length === 0) return;
|
||||
|
||||
// 兜底:不支持 IO 直接加载
|
||||
if (!hasIO) {
|
||||
elements.forEach(callback);
|
||||
return;
|
||||
}
|
||||
|
||||
const obs = new IntersectionObserver((entries, observer) => {
|
||||
entries.forEach(entry => {
|
||||
if (!entry.isIntersecting) return;
|
||||
callback(entry.target);
|
||||
observer.unobserve(entry.target);
|
||||
});
|
||||
}, options);
|
||||
|
||||
elements.forEach(el => obs.observe(el));
|
||||
}
|
||||
|
||||
function scan(root) {
|
||||
const ctx = root || document;
|
||||
|
||||
observe(
|
||||
ctx.querySelectorAll(".lazyload-bg"),
|
||||
applyBg,
|
||||
{ rootMargin: "400px 0px", threshold: 0.01 }
|
||||
);
|
||||
|
||||
observe(
|
||||
ctx.querySelectorAll(".lazyload-img"),
|
||||
applyImg,
|
||||
{ rootMargin: "200px 0px", threshold: 0.01 }
|
||||
);
|
||||
}
|
||||
|
||||
document.addEventListener("DOMContentLoaded", function () {
|
||||
scan(document);
|
||||
});
|
||||
|
||||
// 给“加载更多/分页追加 DOM”使用
|
||||
window.lazyScan = scan;
|
||||
|
||||
window.__LAZY_LOADER_READY__ = true;
|
||||
})();
|
||||
154
code/public/static/js/player/dplayer.init.js
Normal file
154
code/public/static/js/player/dplayer.init.js
Normal file
@@ -0,0 +1,154 @@
|
||||
;(function () {
|
||||
/**
|
||||
* =====================================================
|
||||
* DPlayer Initializer (Scoped by dom_prefix)
|
||||
* -----------------------------------------------------
|
||||
* - 不使用全局变量
|
||||
* - 支持一个页面多个播放器
|
||||
* - 只初始化 data-engine="dplayer" 的模块
|
||||
* =====================================================
|
||||
*/
|
||||
|
||||
function initDPlayer(strPlayUrl) {
|
||||
// if (!root) return;
|
||||
// var root = document.querySelector('[data-engine="dplayer"]');
|
||||
// console.log(root)
|
||||
// if (!root) return;
|
||||
// var domPrefix = document.body.dataset.class;
|
||||
|
||||
// // 防御
|
||||
// if (!domPrefix) {
|
||||
// console.error('[Player] dom_prefix missing on body');
|
||||
// return;
|
||||
// }
|
||||
// var core = root.querySelector('.' + domPrefix + '-player-core');
|
||||
|
||||
// if (!core) return;
|
||||
// console.log(core)
|
||||
|
||||
console.log('strPlayUrl')
|
||||
let DomDPlayer = document.getElementById('dplayer')
|
||||
// ---- 创建 DPlayer 实例 ----
|
||||
try {
|
||||
videoPlayer = new DPlayer({
|
||||
container: DomDPlayer,
|
||||
autoplay: true,
|
||||
screenshot: false,
|
||||
//logo: dplayerLogo,
|
||||
video: {
|
||||
url: strPlayUrl,
|
||||
type: 'hls',
|
||||
},
|
||||
pluginOptions: {
|
||||
hls: {
|
||||
maxBufferLength: 600,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
videoPlayer.play()
|
||||
|
||||
// ---- 错误处理(兜底)----
|
||||
videoPlayer.on('error', function () {
|
||||
showError(root, '播放失败,请尝试切换线路');
|
||||
});
|
||||
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
showError(root, '播放器初始化失败');
|
||||
}
|
||||
}
|
||||
|
||||
function showError(root, msg) {
|
||||
var err = root.querySelector('.' + root.dataset.prefix + '-player-status');
|
||||
if (!err) {
|
||||
err = document.createElement('div');
|
||||
err.className = root.dataset.prefix + '-player-status is-error';
|
||||
root.appendChild(err);
|
||||
}
|
||||
err.innerText = msg;
|
||||
}
|
||||
|
||||
function boot() {
|
||||
var players = document.querySelectorAll('[data-engine="dplayer"]');
|
||||
if (!players.length) return;
|
||||
|
||||
players.forEach(function (root) {
|
||||
// dom_prefix 注入(防串)
|
||||
if (!root.dataset.prefix) {
|
||||
var cls = root.className || '';
|
||||
var m = cls.match(/([a-z0-9]{4,8})-player/);
|
||||
if (m) {
|
||||
root.dataset.prefix = m[1];
|
||||
}
|
||||
}
|
||||
console.log(root)
|
||||
initDPlayer(root);
|
||||
});
|
||||
}
|
||||
|
||||
// 获取第一个线路 key(带容错)
|
||||
function getDefaultLine() {
|
||||
if (typeof arrPlayUrl === "undefined" || !arrPlayUrl || typeof arrPlayUrl !== "object" ) {
|
||||
console.warn("arrPlayUrl 无效:");
|
||||
return null;
|
||||
}
|
||||
|
||||
const keys = Object.keys(arrPlayUrl);
|
||||
if (keys.length === 0) {
|
||||
console.warn("没有任何播放线路");
|
||||
return null;
|
||||
}
|
||||
|
||||
return keys[0]; // 后端排序,第一位为默认
|
||||
}
|
||||
|
||||
// 获取某个线路的第一个 url
|
||||
function getFirstPlayUrl(playLine) {
|
||||
if (!arrPlayUrl || !arrPlayUrl[playLine] || !arrPlayUrl[playLine][0]) {
|
||||
console.warn("播放线路不存在:", playLine, arrPlayUrl);
|
||||
return null;
|
||||
}
|
||||
return arrPlayUrl[playLine][0].url || null;
|
||||
}
|
||||
|
||||
// DOM Ready
|
||||
document.addEventListener("DOMContentLoaded", function () {
|
||||
// boot();
|
||||
console.log(strPlayType)
|
||||
console.log(boolIsPlayPage)
|
||||
if (typeof strPlayType !== "undefined" && boolIsPlayPage) {
|
||||
|
||||
// 用户指定线路
|
||||
if (strPlayType !== "default") {
|
||||
const activeUrl = arrPlayUrl[strPlayType][intPlayUrlIndex - 1];
|
||||
strPlayUrl = activeUrl.url;
|
||||
// checkLine(strPlayType);
|
||||
|
||||
// 默认播放第一个线路的第一个 URL
|
||||
} else {
|
||||
const defaultLine = getDefaultLine();
|
||||
strPlayUrl = getFirstPlayUrl(defaultLine);
|
||||
// checkLine(defaultLine);
|
||||
}
|
||||
console.log('strPlayUrlstrPlayUrl')
|
||||
initDPlayer(strPlayUrl);
|
||||
|
||||
} else if (strPlayType == "default" && !boolIsPlayPage) {
|
||||
|
||||
const defaultLine = getDefaultLine();
|
||||
strPlayUrl = getFirstPlayUrl(defaultLine);
|
||||
// checkLine(defaultLine);
|
||||
|
||||
initDPlayer(strPlayUrl);
|
||||
}
|
||||
})
|
||||
// if (document.readyState === 'loading') {
|
||||
// document.addEventListener('DOMContentLoaded', boot);
|
||||
// } else {
|
||||
// boot();
|
||||
// }
|
||||
})();
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user