diff --git a/js/app.js b/js/app.js index f81ef5c..3bee4a8 100644 --- a/js/app.js +++ b/js/app.js @@ -206,6 +206,105 @@ async function loadForumName() { } } +// ============================================================ +// 页面转场 +// ============================================================ +// 打开一个覆盖页时: +// 1. 首页(#app)留在原位,缩小并后退,同时叠加暗色遮罩 → 「下沉」 +// 2. 新页面从屏幕之外滑入,并由 0.94 放大到 1 +// 关闭时反向播放:新页面滑出屏幕,首页上浮回原位。 +// +// 关键点:首页不再被 display:none 直接隐藏,否则整段动画无从谈起。 +// 只有在转场完全结束后才把它藏起来,避免它继续响应点击或影响滚动。 + +const TRANSITION_MS = 420; + +function sinkBackground() { + const app = document.getElementById('app'); + if (!app) return; + app.classList.remove('page-stack-rise'); + app.classList.add('page-stack-sink'); + document.body.classList.add('page-stack-open'); +} + +function riseBackground() { + const app = document.getElementById('app'); + document.body.classList.remove('page-stack-open'); + if (!app) return; + app.classList.remove('page-stack-sink'); + app.classList.add('page-stack-rise'); + // 动画结束后移除类,避免遗留的 transform 影响 sticky 定位等 + window.setTimeout(() => app.classList.remove('page-stack-rise'), TRANSITION_MS); +} + +// 展示某个覆盖页:先让背景下沉,再挂上 active 触发滑入 +function showSlide(el, { back = false } = {}) { + if (!el) return; + // 清掉上一次的退出状态 + el.classList.remove('slide-out', 'slide-out-back', 'slide-back'); + if (back) el.classList.add('slide-back'); + el.classList.add('active'); + // 强制重排以重放动画(同一元素连续切换时需要) + el.style.animation = 'none'; + void el.offsetHeight; + el.style.animation = ''; + + // 首页仍在文档流中,需要让出滚动位置 + sinkBackground(); +} + +// 关闭覆盖页:播退出动画,结束后清理状态并让首页上浮 +function hideSlide(el, { back = false, onDone } = {}) { + if (!el) { + riseBackground(); + if (onDone) onDone(); + return; + } + el.classList.remove('active', 'slide-back'); + el.classList.add('slide-out'); + if (back) el.classList.add('slide-out-back'); + + riseBackground(); + + window.setTimeout(() => { + el.classList.remove('slide-out', 'slide-out-back'); + if (onDone) onDone(); + }, 300); +} + +// 返回首页:当前覆盖页向左滑出,首页从下沉状态上浮回原位。 +// 与浏览器后退键走同一条路径,保证两种方式的方向感一致。 +function navigateBackToFeed() { + const currentSlide = document.querySelector('.page-slide.active'); + + // 同步 URL:去掉页面参数,回到纯首页地址 + const url = new URL(window.location); + url.searchParams.delete('page'); + url.searchParams.delete('post'); + url.searchParams.delete('user'); + url.searchParams.delete('custom'); + window.history.pushState({ page: 'feed' }, '', url); + + if (currentSlide) { + hideSlide(currentSlide, { back: true, onDone: () => { + document.getElementById('app').style.display = 'flex'; + } }); + } else { + document.getElementById('app').style.display = 'flex'; + riseBackground(); + } + + const customContainer = document.getElementById('customPageContainer'); + if (customContainer && customContainer.classList.contains('active')) { + hideSlide(customContainer, { back: true }); + } + + currentPage = 'feed'; + renderFeed(); + renderStats(); + renderLinks(); +} + function switchPage(page, param) { const url = new URL(window.location); @@ -250,19 +349,34 @@ function switchPage(page, param) { } window.history.pushState({ page: page }, '', url); - document.getElementById('app').style.display = 'none'; - document.querySelectorAll('.page-slide').forEach(el => { - el.classList.remove('active', 'slide-out', 'slide-back'); - }); + // 转场开始:先记录当前显示的覆盖页,稍后让它滑出 + const currentSlide = document.querySelector('.page-slide.active'); + const goingBack = typeof window.isNavigatingBack === 'function' && window.isNavigatingBack(); + + // 目标页就是当前页时无需转场 + const pageMap = { + messages: 'pageMessages', + settings: 'pageSettings', + admin: 'pageAdmin', + new: 'pageNew' + }; + const targetEl = page === 'feed' ? null : document.getElementById(pageMap[page]); + if (currentSlide && targetEl && currentSlide === targetEl) return; + + // 旧覆盖页滑出 + if (currentSlide && currentSlide !== targetEl) { + hideSlide(currentSlide, { back: goingBack }); + } const customContainer = document.getElementById('customPageContainer'); - if (customContainer) { - customContainer.classList.remove('active'); - customContainer.style.display = 'none'; + if (customContainer && customContainer.classList.contains('active')) { + hideSlide(customContainer); } if (page === 'feed') { + // 回到首页:首页上浮回原位,不需要显示任何覆盖页 document.getElementById('app').style.display = 'flex'; + riseBackground(); currentPage = 'feed'; renderFeed(); renderStats(); @@ -270,23 +384,9 @@ function switchPage(page, param) { return; } - const pageMap = { - messages: 'pageMessages', - settings: 'pageSettings', - admin: 'pageAdmin', - new: 'pageNew' - }; - const el = document.getElementById(pageMap[page]); + const el = targetEl; if (el) { - // 后退导航时用反向滑入:返回上一页时内容应从左侧进来, - // 而主动点击进入是从右侧进来。 - if (typeof window.isNavigatingBack === 'function' && window.isNavigatingBack()) { - el.classList.add('slide-back'); - } - el.classList.add('active'); - el.style.animation = 'none'; - void el.offsetHeight; - el.style.animation = ''; + showSlide(el, { back: goingBack }); currentPage = page; if (page === 'admin') { @@ -610,23 +710,25 @@ function renderCustomPagesNav() { // ============================================================ function showCustomPage(pageName) { - document.getElementById('app').style.display = 'none'; - document.querySelectorAll('.page-slide').forEach(el => { - el.classList.remove('active', 'slide-out', 'slide-back'); - }); + const goingBack = typeof window.isNavigatingBack === 'function' && window.isNavigatingBack(); let container = document.getElementById('customPageContainer'); if (!container) { container = document.createElement('div'); container.id = 'customPageContainer'; container.className = 'page-slide'; - container.style.cssText = 'display:none;position:fixed;inset:0;background:var(--bg);z-index:50;padding:84px 32px 40px;overflow-y:auto;transition:background 0.2s;'; + // 不再写内联 display:交给 .page-slide / .active 的样式控制, + // 否则会覆盖转场动画所需的 display 切换。 document.body.appendChild(container); } - container.innerHTML = '
加载中...
'; - container.classList.add('active'); - container.style.display = 'block'; + // 先让其他覆盖页滑出 + document.querySelectorAll('.page-slide.active').forEach(other => { + if (other !== container) hideSlide(other, { back: goingBack }); + }); + + container.innerHTML = Skeleton.postDetail(); + showSlide(container, { back: goingBack }); currentPage = 'custom'; API.getCustomPage(pageName).then(page => { @@ -1350,12 +1452,13 @@ async function init() { document.querySelectorAll('.back-btn').forEach(btn => { btn.addEventListener('click', function() { - switchPage('feed'); + // 返回按钮等同于后退导航:让当前页向左滑出、首页从下沉状态上浮 + navigateBackToFeed(); }); }); document.getElementById('forumName').addEventListener('click', function() { - switchPage('feed'); + navigateBackToFeed(); }); // ===== 忘记密码 ===== diff --git a/js/post.js b/js/post.js index 23ad67c..577404a 100644 --- a/js/post.js +++ b/js/post.js @@ -14,20 +14,16 @@ function switchToPost(postId) { } function showPostPage(postId) { - document.getElementById('app').style.display = 'none'; - document.querySelectorAll('.page-slide').forEach(el => { - el.classList.remove('active', 'slide-out', 'slide-back'); - }); + const goingBack = typeof window.isNavigatingBack === 'function' && window.isNavigatingBack(); const el = document.getElementById('pagePost'); - // 后退导航时用反向滑入,与主动进入区分 - if (typeof window.isNavigatingBack === 'function' && window.isNavigatingBack()) { - el.classList.add('slide-back'); - } - el.classList.add('active'); - // 强制重排以重放动画(同一元素连续切换时需要) - el.style.animation = 'none'; - void el.offsetHeight; - el.style.animation = ''; + + // 先让其他覆盖页滑出(从帖子直接跳到用户主页之类的情况) + document.querySelectorAll('.page-slide.active').forEach(other => { + if (other !== el) hideSlide(other, { back: goingBack }); + }); + + // 首页留在背景中下沉,新页面从屏幕外滑入 + showSlide(el, { back: goingBack }); currentPage = 'post'; renderPostDetail(postId); } diff --git a/js/user.js b/js/user.js index bfef30e..dfcff84 100644 --- a/js/user.js +++ b/js/user.js @@ -78,19 +78,16 @@ async function renderUserProfile(username) { } function showUserPage(username) { - document.getElementById('app').style.display = 'none'; - document.querySelectorAll('.page-slide').forEach(el => { - el.classList.remove('active', 'slide-out', 'slide-back'); - }); + const goingBack = typeof window.isNavigatingBack === 'function' && window.isNavigatingBack(); const el = document.getElementById('pageUser'); - // 后退导航时用反向滑入,与主动进入区分 - if (typeof window.isNavigatingBack === 'function' && window.isNavigatingBack()) { - el.classList.add('slide-back'); - } - el.classList.add('active'); - el.style.animation = 'none'; - void el.offsetHeight; - el.style.animation = ''; + + // 先让其他覆盖页滑出(例如从帖子详情跳到用户主页) + document.querySelectorAll('.page-slide.active').forEach(other => { + if (other !== el) hideSlide(other, { back: goingBack }); + }); + + // 首页留在背景中下沉,新页面从屏幕外滑入 + showSlide(el, { back: goingBack }); currentPage = 'user'; renderUserProfile(username); } diff --git a/style.css b/style.css index f970c49..2e99e80 100644 --- a/style.css +++ b/style.css @@ -855,7 +855,77 @@ svg { resize: vertical; } -/* ===== 独立页面(滑入) ===== */ +/* ===== 独立页面(推入式转场) ===== + ------------------------------------------------------------ + 转场由两部分组成,共同营造「新页面把旧页面推到背景」的层次感: + + 1. 新页面(.page-slide)从屏幕右侧之外整体滑入,同时由 0.94 放大 + 到 1,配合透明度渐显。用 100vw 而非几十像素,才是真正的「从 + 屏幕外进来」而不是轻微抖动。 + 2. 旧内容(#app)留在原位但向后下沉:缩小到 0.94、右移少量、加一层 + 暗色遮罩(用 filter 提亮/压暗由 .page-stack-dim 承担)。 + + #app 不再被 display:none 直接藏掉,否则没有动画可言。是否下沉由 + JS 通过 body.page-stack-open 控制。 */ + +@keyframes pageSlideIn { + from { + transform: translateX(100%) scale(0.94); + opacity: 0; + } + to { + transform: translateX(0) scale(1); + opacity: 1; + } +} + +/* 后退:从左侧之外滑入 */ +@keyframes pageSlideInBack { + from { + transform: translateX(-100%) scale(0.94); + opacity: 0; + } + to { + transform: translateX(0) scale(1); + opacity: 1; + } +} + +/* 退出:原路滑回屏幕外,略快于进入,避免拖沓 */ +@keyframes pageSlideOut { + from { + transform: translateX(0) scale(1); + opacity: 1; + } + to { + transform: translateX(100%) scale(0.94); + opacity: 0; + } +} + +@keyframes pageSlideOutBack { + from { + transform: translateX(0) scale(1); + opacity: 1; + } + to { + transform: translateX(-100%) scale(0.94); + opacity: 0; + } +} + +/* 旧内容下沉:缩小 + 轻微右移 */ +@keyframes stackSink { + from { transform: translateX(0) scale(1); } + to { transform: translateX(4%) scale(0.94); } +} + +@keyframes stackRise { + from { transform: translateX(4%) scale(0.94); } + to { transform: translateX(0) scale(1); } +} + +/* ===== 页面覆盖层 ===== */ .page-slide { display: none; position: fixed; @@ -867,48 +937,87 @@ svg { padding: 84px 32px 40px; overflow-y: auto; transition: background 0.3s ease; + /* 转场期间避免横向滚动条 */ + will-change: transform, opacity; } + .page-slide.active { display: block; - animation: slideIn 0.35s cubic-bezier(0.22, 1, 0.36, 1); + animation: pageSlideIn 0.42s cubic-bezier(0.22, 1, 0.36, 1); } -/* 后退(浏览器返回键或返回按钮)时用反向动画。 - 原先只有 slideIn 一个方向,前进与后退都从右侧滑入,方向感是错的: - 返回上一页时内容应该从左侧进来。由 JS 在 popstate 上打这个标记。 */ + +/* 后退(浏览器返回键或返回按钮)时方向相反 */ .page-slide.active.slide-back { - animation: slideInBack 0.35s cubic-bezier(0.22, 1, 0.36, 1); -} -@keyframes slideIn { - from { - transform: translateX(40px); - opacity: 0.6; - } - to { - transform: translateX(0); - opacity: 1; - } -} -@keyframes slideInBack { - from { - transform: translateX(-40px); - opacity: 0.6; - } - to { - transform: translateX(0); - opacity: 1; - } + animation: pageSlideInBack 0.42s cubic-bezier(0.22, 1, 0.36, 1); } + +/* 主动离开当前页时播放退出动画,由 JS 加上 .slide-out */ .page-slide.slide-out { - animation: slideOut 0.25s cubic-bezier(0.55, 0, 1, 0.45) forwards; + display: block; + animation: pageSlideOut 0.3s cubic-bezier(0.4, 0, 1, 1) forwards; + pointer-events: none; } -@keyframes slideOut { - from { - transform: translateX(0); - opacity: 1; + +.page-slide.slide-out.slide-out-back { + animation: pageSlideOutBack 0.3s cubic-bezier(0.4, 0, 1, 1) forwards; +} + +/* ===== 旧内容下沉 ===== + #app 被推到背景:缩小并右移。 + 注意必须写成 #app.page-stack-sink 而非单独的 .page-stack-sink: + 样式表靠后位置有一条 `#app { animation: fadeIn ... }`(交互动效层), + ID 选择器的优先级高于类选择器,只写类名会被它覆盖导致动画不生效。 */ + +#app.page-stack-sink { + transform-origin: center center; + animation: stackSink 0.42s cubic-bezier(0.22, 1, 0.36, 1) forwards; +} + +#app.page-stack-rise { + transform-origin: center center; + animation: stackRise 0.42s cubic-bezier(0.22, 1, 0.36, 1) forwards; +} + +/* 下沉同时叠加暗色遮罩,强化「退到后面」的感觉。 + 遮罩放在 #app 的伪元素上,避免额外 DOM。 */ +#app::after { + content: ''; + position: fixed; + inset: 0; + background: rgba(15, 18, 28, 0.42); + opacity: 0; + pointer-events: none; + transition: opacity 0.42s cubic-bezier(0.22, 1, 0.36, 1); + z-index: 45; +} + +body.page-stack-open #app::after { + opacity: 1; +} + +/* 下沉期间禁用旧内容的交互,防止误点 */ +#app.page-stack-sink, +body.page-stack-open #app { + pointer-events: none; +} + +/* 减少动画时退化为简单的淡入,不做位移与缩放 */ +@media (prefers-reduced-motion: reduce) { + .page-slide.active, + .page-slide.active.slide-back, + .page-slide.slide-out, + .page-slide.slide-out.slide-out-back { + animation: fadeIn 0.12s ease-out !important; + transform: none !important; } - to { - transform: translateX(-40px); - opacity: 0.5; + /* 同样要用 ID 组合选择器才能压过 #app 的 fadeIn */ + #app.page-stack-sink, + #app.page-stack-rise { + animation: none !important; + transform: none !important; + } + #app::after { + transition: none; } }