feat(ui): 重做页面转场为推入式,旧页面下沉、新页面从屏幕外滑入

原先的「滑入」只是 translateX(40px),几十像素的位移,在 1280px 宽的
屏上几乎看不出来,不是真正的页面转场。

现改为两段协同的推入效果:

1. 新页面(.page-slide)从屏幕外整体滑入:translateX(100%) → 0,
   同时 scale 0.94 → 1、opacity 0 → 1。用百分比而非固定像素,
   任何视口宽度下都从边缘之外开始。
2. 旧内容(#app)留在原位下沉:scale 1 → 0.94、右移 4%,
   并叠加一层暗色遮罩(#app::after,opacity 0 → 1)。

关键改动:#app 不再被 display:none 直接隐藏——那样根本没有动画可做。
现由 body.page-stack-open 与 .page-stack-sink 控制下沉,转场结束后
才清理状态。相关逻辑抽成 sinkBackground / riseBackground / showSlide /
hideSlide 四个函数,供 switchPage、showPostPage、showUserPage、
showCustomPage 共用。返回按钮走 navigateBackToFeed,与浏览器后退键
方向一致(新页面向左滑出、首页上浮)。

过程中修掉两个真实问题:

- 样式表靠后位置本有一条 `#app { animation: fadeIn ... }`(交互动效层)。
  ID 选择器优先级高于类选择器,导致最初的 .page-stack-sink 完全不生效,
  实测首页 transform 始终为 none。改为 #app.page-stack-sink 组合选择器
  后正常。reduced-motion 分支同样需要组合选择器才能压过它。
- app.js 中 showCustomPage 仍保留旧的 `#app.style.display = 'none'`,
  且给容器写了内联 display,会覆盖转场所需的 display 切换。已一并移除。

实测(Puppeteer 逐帧采样,视口 1280×900):
- 新页面 X 坐标:1280 → 860 → 541 → 421 → 326 → 60 → 6 → 0,
  起始 1280 恰好等于视口宽度,确认从屏幕外开始
- 首页:scale 1 → 0.940,右移 0 → 48px,遮罩不透明度 0 → 1.00
- 中间帧截图可见新页面自右侧滑入、旧页面在背景中缩小变暗

完整测试套件 25/25 通过。
This commit is contained in:
Furrium Network committed 2026-09-22 21:22:07 +08:00
1 parent 0ea921186a
commit 7176bea12c
4 files changed
+297 -92

No files matched your search

+136 -33
View File
@@ -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) { function switchPage(page, param) {
const url = new URL(window.location); const url = new URL(window.location);
@@ -250,19 +349,34 @@ function switchPage(page, param) {
} }
window.history.pushState({ page: page }, '', url); window.history.pushState({ page: page }, '', url);
document.getElementById('app').style.display = 'none'; // 转场开始:先记录当前显示的覆盖页,稍后让它滑出
document.querySelectorAll('.page-slide').forEach(el => { const currentSlide = document.querySelector('.page-slide.active');
el.classList.remove('active', 'slide-out', 'slide-back'); 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'); const customContainer = document.getElementById('customPageContainer');
if (customContainer) { if (customContainer && customContainer.classList.contains('active')) {
customContainer.classList.remove('active'); hideSlide(customContainer);
customContainer.style.display = 'none';
} }
if (page === 'feed') { if (page === 'feed') {
// 回到首页:首页上浮回原位,不需要显示任何覆盖页
document.getElementById('app').style.display = 'flex'; document.getElementById('app').style.display = 'flex';
riseBackground();
currentPage = 'feed'; currentPage = 'feed';
renderFeed(); renderFeed();
renderStats(); renderStats();
@@ -270,23 +384,9 @@ function switchPage(page, param) {
return; return;
} }
const pageMap = { const el = targetEl;
messages: 'pageMessages',
settings: 'pageSettings',
admin: 'pageAdmin',
new: 'pageNew'
};
const el = document.getElementById(pageMap[page]);
if (el) { if (el) {
// 后退导航时用反向滑入:返回上一页时内容应从左侧进来, showSlide(el, { back: goingBack });
// 而主动点击进入是从右侧进来。
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 = '';
currentPage = page; currentPage = page;
if (page === 'admin') { if (page === 'admin') {
@@ -610,23 +710,25 @@ function renderCustomPagesNav() {
// ============================================================ // ============================================================
function showCustomPage(pageName) { function showCustomPage(pageName) {
document.getElementById('app').style.display = 'none'; const goingBack = typeof window.isNavigatingBack === 'function' && window.isNavigatingBack();
document.querySelectorAll('.page-slide').forEach(el => {
el.classList.remove('active', 'slide-out', 'slide-back');
});
let container = document.getElementById('customPageContainer'); let container = document.getElementById('customPageContainer');
if (!container) { if (!container) {
container = document.createElement('div'); container = document.createElement('div');
container.id = 'customPageContainer'; container.id = 'customPageContainer';
container.className = 'page-slide'; 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); document.body.appendChild(container);
} }
container.innerHTML = '<div style="text-align:center;color:#94a3b8;padding:40px 0;">加载中...</div>'; // 先让其他覆盖页滑出
container.classList.add('active'); document.querySelectorAll('.page-slide.active').forEach(other => {
container.style.display = 'block'; if (other !== container) hideSlide(other, { back: goingBack });
});
container.innerHTML = Skeleton.postDetail();
showSlide(container, { back: goingBack });
currentPage = 'custom'; currentPage = 'custom';
API.getCustomPage(pageName).then(page => { API.getCustomPage(pageName).then(page => {
@@ -1350,12 +1452,13 @@ async function init() {
document.querySelectorAll('.back-btn').forEach(btn => { document.querySelectorAll('.back-btn').forEach(btn => {
btn.addEventListener('click', function() { btn.addEventListener('click', function() {
switchPage('feed'); // 返回按钮等同于后退导航:让当前页向左滑出、首页从下沉状态上浮
navigateBackToFeed();
}); });
}); });
document.getElementById('forumName').addEventListener('click', function() { document.getElementById('forumName').addEventListener('click', function() {
switchPage('feed'); navigateBackToFeed();
}); });
// ===== 忘记密码 ===== // ===== 忘记密码 =====
+9 -13
View File
@@ -14,20 +14,16 @@ function switchToPost(postId) {
} }
function showPostPage(postId) { function showPostPage(postId) {
document.getElementById('app').style.display = 'none'; const goingBack = typeof window.isNavigatingBack === 'function' && window.isNavigatingBack();
document.querySelectorAll('.page-slide').forEach(el => {
el.classList.remove('active', 'slide-out', 'slide-back');
});
const el = document.getElementById('pagePost'); const el = document.getElementById('pagePost');
// 后退导航时用反向滑入,与主动进入区分
if (typeof window.isNavigatingBack === 'function' && window.isNavigatingBack()) { // 先让其他覆盖页滑出(从帖子直接跳到用户主页之类的情况)
el.classList.add('slide-back'); document.querySelectorAll('.page-slide.active').forEach(other => {
} if (other !== el) hideSlide(other, { back: goingBack });
el.classList.add('active'); });
// 强制重排以重放动画(同一元素连续切换时需要)
el.style.animation = 'none'; // 首页留在背景中下沉,新页面从屏幕外滑入
void el.offsetHeight; showSlide(el, { back: goingBack });
el.style.animation = '';
currentPage = 'post'; currentPage = 'post';
renderPostDetail(postId); renderPostDetail(postId);
} }
+9 -12
View File
@@ -78,19 +78,16 @@ async function renderUserProfile(username) {
} }
function showUserPage(username) { function showUserPage(username) {
document.getElementById('app').style.display = 'none'; const goingBack = typeof window.isNavigatingBack === 'function' && window.isNavigatingBack();
document.querySelectorAll('.page-slide').forEach(el => {
el.classList.remove('active', 'slide-out', 'slide-back');
});
const el = document.getElementById('pageUser'); const el = document.getElementById('pageUser');
// 后退导航时用反向滑入,与主动进入区分
if (typeof window.isNavigatingBack === 'function' && window.isNavigatingBack()) { // 先让其他覆盖页滑出(例如从帖子详情跳到用户主页)
el.classList.add('slide-back'); document.querySelectorAll('.page-slide.active').forEach(other => {
} if (other !== el) hideSlide(other, { back: goingBack });
el.classList.add('active'); });
el.style.animation = 'none';
void el.offsetHeight; // 首页留在背景中下沉,新页面从屏幕外滑入
el.style.animation = ''; showSlide(el, { back: goingBack });
currentPage = 'user'; currentPage = 'user';
renderUserProfile(username); renderUserProfile(username);
} }
+143 -34
View File
@@ -855,7 +855,77 @@ svg {
resize: vertical; 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 { .page-slide {
display: none; display: none;
position: fixed; position: fixed;
@@ -867,48 +937,87 @@ svg {
padding: 84px 32px 40px; padding: 84px 32px 40px;
overflow-y: auto; overflow-y: auto;
transition: background 0.3s ease; transition: background 0.3s ease;
/* 转场期间避免横向滚动条 */
will-change: transform, opacity;
} }
.page-slide.active { .page-slide.active {
display: block; 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 { .page-slide.active.slide-back {
animation: slideInBack 0.35s cubic-bezier(0.22, 1, 0.36, 1); animation: pageSlideInBack 0.42s 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;
}
} }
/* 主动离开当前页时播放退出动画,由 JS 加上 .slide-out */
.page-slide.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 { .page-slide.slide-out.slide-out-back {
transform: translateX(0); animation: pageSlideOutBack 0.3s cubic-bezier(0.4, 0, 1, 1) forwards;
opacity: 1; }
/* ===== 旧内容下沉 =====
#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 { /* 同样要用 ID 组合选择器才能压过 #app 的 fadeIn */
transform: translateX(-40px); #app.page-stack-sink,
opacity: 0.5; #app.page-stack-rise {
animation: none !important;
transform: none !important;
}
#app::after {
transition: none;
} }
} }