对齐内容主线页骨架
This commit is contained in:
@@ -25,8 +25,13 @@
|
||||
var loading = false;
|
||||
var loadMoreBtn = container.querySelector(".aksrt-load-more-btn, .ajax-next");
|
||||
var indicator = container.querySelector(".aksrt-loading-indicator");
|
||||
// 目标容器:文章列表取 .post-grid,评论取 .comment-list
|
||||
var target = container.previousElementSibling;
|
||||
if (target && target.classList && target.classList.contains("content-stream__footer")) {
|
||||
target = target.previousElementSibling;
|
||||
}
|
||||
if (!target && container.parentElement) {
|
||||
target = container.parentElement.querySelector(".post-grid, .comment-list, .content-stream__list");
|
||||
}
|
||||
|
||||
function showSkeleton() {
|
||||
if (!target) return;
|
||||
@@ -96,10 +101,8 @@
|
||||
nextPage++;
|
||||
container.dataset.nextPage = String(nextPage);
|
||||
|
||||
// 重新初始化懒加载(新加载的图片)
|
||||
if (typeof initLazyLoad === "function") initLazyLoad();
|
||||
|
||||
// 如果没有更多页
|
||||
if (nextPage > maxPages || !res.data.has_more) {
|
||||
if (loadMoreBtn) {
|
||||
loadMoreBtn.textContent = nomoreText;
|
||||
@@ -124,7 +127,6 @@
|
||||
});
|
||||
}
|
||||
|
||||
// 加载更多按钮点击
|
||||
if (loadMoreBtn) {
|
||||
loadMoreBtn.addEventListener("click", function(e) {
|
||||
e.preventDefault();
|
||||
@@ -154,55 +156,48 @@
|
||||
});
|
||||
}
|
||||
|
||||
// 无限滚动:IntersectionObserver
|
||||
if (mode === "infinite" && "IntersectionObserver" in window) {
|
||||
// Comment containers respect commentIasAuto; if disabled, fall back to click-to-load
|
||||
if (isComment && window.aksrtData && aksrtData.commentIasAuto === false) {
|
||||
// Skip IntersectionObserver — load_more button already present
|
||||
} else {
|
||||
var sentinel = document.createElement("div");
|
||||
sentinel.className = "aksrt-scroll-sentinel";
|
||||
container.appendChild(sentinel);
|
||||
var lazyloadAction = container.dataset.lazyloadAction || "";
|
||||
var iasMax = isComment
|
||||
? ((window.aksrtData && typeof aksrtData.commentIasMax !== "undefined") ? parseInt(aksrtData.commentIasMax, 10) : 0)
|
||||
: ((window.aksrtData && typeof aksrtData.iasMax !== "undefined") ? parseInt(aksrtData.iasMax, 10) : 0);
|
||||
var initialPage = nextPage - 1; // page at init time
|
||||
var observer = new IntersectionObserver(
|
||||
function (entries) {
|
||||
if (entries[0].isIntersecting && !loading) {
|
||||
// iasMax = 0 means unlimited, otherwise limit additional pages
|
||||
if (iasMax > 0 && (nextPage - initialPage) >= iasMax) {
|
||||
// Reached ias max, show nomore and disconnect
|
||||
observer.disconnect();
|
||||
if (loadMoreBtn) {
|
||||
loadMoreBtn.textContent = nomoreText;
|
||||
loadMoreBtn.classList.add("ajax-nomore");
|
||||
loadMoreBtn.style.pointerEvents = "none";
|
||||
var sentinel = document.createElement("div");
|
||||
sentinel.className = "aksrt-scroll-sentinel";
|
||||
container.appendChild(sentinel);
|
||||
var lazyloadAction = container.dataset.lazyloadAction || "";
|
||||
var iasMax = isComment
|
||||
? ((window.aksrtData && typeof aksrtData.commentIasMax !== "undefined") ? parseInt(aksrtData.commentIasMax, 10) : 0)
|
||||
: ((window.aksrtData && typeof aksrtData.iasMax !== "undefined") ? parseInt(aksrtData.iasMax, 10) : 0);
|
||||
var initialPage = nextPage - 1;
|
||||
var observer = new IntersectionObserver(
|
||||
function (entries) {
|
||||
if (entries[0].isIntersecting && !loading) {
|
||||
if (iasMax > 0 && (nextPage - initialPage) >= iasMax) {
|
||||
observer.disconnect();
|
||||
if (loadMoreBtn) {
|
||||
loadMoreBtn.textContent = nomoreText;
|
||||
loadMoreBtn.classList.add("ajax-nomore");
|
||||
loadMoreBtn.style.pointerEvents = "none";
|
||||
}
|
||||
return;
|
||||
}
|
||||
return;
|
||||
loadNextPage();
|
||||
}
|
||||
loadNextPage();
|
||||
}
|
||||
},
|
||||
{ rootMargin: "400px" }
|
||||
);
|
||||
if (lazyloadAction === "ias") {
|
||||
container.classList.add("ias-pending");
|
||||
}
|
||||
observer.observe(sentinel);
|
||||
},
|
||||
{ rootMargin: "400px" }
|
||||
);
|
||||
if (lazyloadAction === "ias") {
|
||||
container.classList.add("ias-pending");
|
||||
}
|
||||
observer.observe(sentinel);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/* ====== 图片懒加载 ====== */
|
||||
function initLazyLoad(root) {
|
||||
var scope = root || document;
|
||||
var lazyImages = scope.querySelectorAll("img.aksrt-lazy");
|
||||
if (!lazyImages.length) return;
|
||||
|
||||
// 不支持 IntersectionObserver 时直接加载所有图片
|
||||
if (!("IntersectionObserver" in window)) {
|
||||
lazyImages.forEach(function (img) {
|
||||
var src = img.dataset.src;
|
||||
@@ -230,13 +225,11 @@
|
||||
return;
|
||||
}
|
||||
|
||||
// 创建新图片预加载
|
||||
var tempImg = new Image();
|
||||
tempImg.onload = function () {
|
||||
img.src = src;
|
||||
img.classList.remove("aksrt-lazy");
|
||||
img.classList.add("aksrt-loaded");
|
||||
// 移除骨架
|
||||
var skeleton = img.previousElementSibling;
|
||||
if (skeleton && skeleton.classList.contains("aksrt-skeleton")) {
|
||||
skeleton.remove();
|
||||
@@ -245,7 +238,6 @@
|
||||
tempImg.onerror = function () {
|
||||
img.classList.remove("aksrt-lazy");
|
||||
img.classList.add("aksrt-error");
|
||||
// 移除骨架
|
||||
var skeleton = img.previousElementSibling;
|
||||
if (skeleton && skeleton.classList.contains("aksrt-skeleton")) {
|
||||
skeleton.remove();
|
||||
|
||||
Reference in New Issue
Block a user