From 26312f799004409657a03809cd245dd8273bfef4 Mon Sep 17 00:00:00 2001 From: Furrium Network Date: Sun, 9 Aug 2026 19:47:55 +0800 Subject: [PATCH] Update feed.js --- js/feed.js | 43 ++++++++++++++++--------------------------- 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/js/feed.js b/js/feed.js index a1ec8e2..e1e711d 100644 --- a/js/feed.js +++ b/js/feed.js @@ -3,27 +3,26 @@ // ============================================================ let currentSort = 'latest'; - let totalPages = 1; const PAGE_SIZE = 20; function renderFeed() { const container = document.getElementById('postList'); container.innerHTML = '
加载中...
'; - API.getPosts(currentSort, currentPage, PAGE_SIZE).then(result => { + API.getPosts(currentSort, currentPageNum, PAGE_SIZE).then(result => { const posts = result.data || []; const pagination = result.pagination || { total: 0, totalPages: 1, page: 1 }; totalPages = pagination.totalPages || 1; - currentPage = pagination.page || 1; + currentPageNum = pagination.page || 1; - if (posts.length === 0 && currentPage === 1) { + if (posts.length === 0 && currentPageNum === 1) { container.innerHTML = '
✨ 还没有帖子,快来发布第一条吧!
'; return; } - if (posts.length === 0 && currentPage > 1) { - currentPage = 1; + if (posts.length === 0 && currentPageNum > 1) { + currentPageNum = 1; renderFeed(); return; } @@ -73,32 +72,26 @@ function renderFeed() { }); container.innerHTML = html; - // ============================================================ - // 完整分页控件 - // ============================================================ + // 分页控件 if (totalPages > 1) { let paginationHtml = `
- `; - // 显示页码按钮 - let startPage = Math.max(1, currentPage - 4); - let endPage = Math.min(totalPages, currentPage + 4); + let startPage = Math.max(1, currentPageNum - 4); + let endPage = Math.min(totalPages, currentPageNum + 4); - // 如果当前页靠近开头,显示更多后面的页 - if (currentPage <= 4) { + if (currentPageNum <= 4) { endPage = Math.min(totalPages, 9); } - // 如果当前页靠近结尾,显示更多前面的页 - if (currentPage > totalPages - 4) { + if (currentPageNum > totalPages - 4) { startPage = Math.max(1, totalPages - 8); } - // 第一页 if (startPage > 1) { paginationHtml += ``; if (startPage > 2) { @@ -107,7 +100,7 @@ function renderFeed() { } for (let i = startPage; i <= endPage; i++) { - const isActive = i === currentPage; + const isActive = i === currentPageNum; paginationHtml += ` @@ -135,12 +128,11 @@ function renderFeed() { `; container.innerHTML += paginationHtml; - // 绑定页码点击事件 container.querySelectorAll('.page-btn:not([disabled])').forEach(btn => { btn.addEventListener('click', function() { const page = parseInt(this.dataset.page); if (page >= 1 && page <= totalPages) { - currentPage = page; + currentPageNum = page; const url = new URL(window.location); url.searchParams.set('postpage', page); window.history.pushState({}, '', url); @@ -150,7 +142,6 @@ function renderFeed() { }); } - // 事件绑定 container.querySelectorAll('.post-card').forEach(card => { card.addEventListener('click', function(e) { if (e.target.closest('button')) return; @@ -214,12 +205,10 @@ document.querySelectorAll('.tab').forEach(tab => { document.querySelectorAll('.tab').forEach(t => t.classList.remove('active')); this.classList.add('active'); currentSort = this.dataset.sort; - currentPage = 1; + currentPageNum = 1; const url = new URL(window.location); url.searchParams.delete('postpage'); window.history.pushState({}, '', url); - if (currentPage === 'feed') { - renderFeed(); - } + renderFeed(); }); });