feat(motion): scroll-reveal and cinematic entrance animations

- motion.css / motion.js: reveal-on-scroll (IntersectionObserver), hero word-by-word
  entrance, drifting warm orbs, sheen on primary CTA, section underline sweep,
  scroll progress bar, count-up numbers
- content never hidden unless <html>.fx-motion: JS off or reduced-motion shows everything
- hover lift moved to the translate property so reveal keyframes cannot override it
- re-call initReveal after grid re-render and admin re-mount; SW precache + VERSION bump
- docs: DESIGN.md §9 motion contract, AGENTS.md pitfalls
This commit is contained in:
Bkk1234 committed 2026-11-19 01:10:27 +07:00
1 parent c00bc62272
commit 2950295690
10 files changed
+503 -12

No files matched your search

+4
View File
@@ -3,6 +3,7 @@
import { hydrateIcons, toast } from "./ui.js";
import { loadProjects, normalizeData } from "./data.js";
import { defineRoutes, markActiveNav, onRouteChange, startRouter } from "./router.js";
import { countUpInside, initMotion, initReveal } from "./motion.js";
import { homeView } from "./views/home.js";
import { adminView } from "./views/admin.js";
import "./pwa.js";
@@ -51,6 +52,8 @@ function renderRoute({ path, view }) {
document.title = TITLES[path] || TITLES["/"];
window.scrollTo({ top: 0, behavior: "auto" });
main.focus({ preventScroll: true });
initReveal(main);
countUpInside(main);
}
function updateChrome() {
@@ -83,6 +86,7 @@ function skeleton() {
async function boot() {
hydrateIcons();
initMotion();
skeleton();
const result = await loadProjects();
state.data = result.data;
+133
View File
@@ -0,0 +1,133 @@
/* motion.js — reveal-on-scroll, แถบความคืบหน้า, นับเลขขึ้น
หลักการ: ถ้า <html> ไม่มี .fx-motion (JS ปิด / reduced-motion) ให้ทุกอย่างเห็นทันที
ห้ามซ่อนเนื้อหาใน CSS หลัก — ซ่อนเฉพาะใต้ .fx-motion */
const REDUCED = () => window.matchMedia("(prefers-reduced-motion: reduce)").matches;
export function motionEnabled() {
return document.documentElement.classList.contains("fx-motion");
}
/** เปิด/ปิดคลาส .fx-motion ตามการตั้งค่าผู้ใช้ (เรียกซ้ำได้เมื่อผู้ใช้เปลี่ยน setting) */
export function syncMotionPreference() {
document.documentElement.classList.toggle("fx-motion", !REDUCED());
}
/* ------------------------------------------------------------- แถบเลื่อน */
export function initScrollProgress() {
const bar = document.querySelector(".scroll-progress__bar");
if (!bar) return;
let ticking = false;
const update = () => {
const doc = document.documentElement;
const scrollable = doc.scrollHeight - window.innerHeight;
const ratio = scrollable > 0 ? Math.min(1, Math.max(0, window.scrollY / scrollable)) : 0;
bar.style.transform = `scaleX(${ratio.toFixed(4)})`;
ticking = false;
};
window.addEventListener(
"scroll",
() => {
if (ticking) return;
ticking = true;
requestAnimationFrame(update);
},
{ passive: true },
);
window.addEventListener("resize", update, { passive: true });
update();
}
/* --------------------------------------------------------------- reveal */
export function initReveal(root = document) {
const targets = [...root.querySelectorAll("[data-reveal]")];
if (!targets.length) return;
// ไม่มี motion → เปิดให้เห็นทุกอันแล้วจบ
if (!motionEnabled() || REDUCED()) {
targets.forEach((el) => el.setAttribute("data-revealed", ""));
return;
}
const reveal = (el) => {
if (el.hasAttribute("data-revealed")) return;
// หน่วงแบบต่อเนื่องเฉพาะการ์ดในกริด (คิดตามคอลัมน์ ไม่ใช่ index สะสม)
el.setAttribute("data-revealed", "");
if (el.matches(".project-card")) countUpInside(el);
};
if (!("IntersectionObserver" in window)) {
targets.forEach(reveal);
return;
}
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
reveal(entry.target);
observer.unobserve(entry.target);
return;
}
// กระโดดข้ามบล็อกไปแล้ว (anchor jump / คืนตำแหน่ง scroll) — ต้องเปิดให้เห็น ไม่ปล่อยค้างซ่อน
const rootTop = entry.rootBounds ? entry.rootBounds.top : 0;
if (entry.boundingClientRect.bottom <= rootTop) {
reveal(entry.target);
observer.unobserve(entry.target);
}
});
},
{ rootMargin: "2000px 0px -6% 0px", threshold: 0.01 },
);
targets.forEach((el) => observer.observe(el));
}
/* --------------------------------------------------------------- count up */
export function countUpInside(scope = document) {
if (!motionEnabled() || REDUCED()) return;
scope.querySelectorAll("[data-countup]").forEach((el) => {
if (el.dataset.countupDone === "1") return;
el.dataset.countupDone = "1";
const target = Number(el.getAttribute("data-countup")) || 0;
if (target <= 0) return;
const duration = 720;
const start = performance.now();
const tick = (now) => {
const progress = Math.min(1, (now - start) / duration);
const eased = 1 - Math.pow(1 - progress, 3);
el.textContent = String(Math.round(target * eased));
if (progress < 1) requestAnimationFrame(tick);
else el.textContent = String(target);
};
el.textContent = "0";
requestAnimationFrame(tick);
});
}
/** เรียกครั้งเดียวหลังโหลดหน้า */
export function initMotion() {
syncMotionPreference();
initScrollProgress();
if (typeof window.matchMedia === "function") {
const mq = window.matchMedia("(prefers-reduced-motion: reduce)");
mq.addEventListener?.("change", () => {
syncMotionPreference();
if (!REDUCED()) initReveal(document);
});
}
}
/** แบ่งข้อความไทยเป็นช่วงคำ (ตัดด้วยช่องว่างเท่านั้น — ปลอดภัยกับสระ/วรรณยุกต์) */
export function splitWordsHtml(text, escape, startDelay = 140, step = 85) {
return String(text)
.trim()
.split(/\s+/)
.map(
(word, index) =>
`<span class="fx-word" style="--fx-delay:${startDelay + index * step}ms"><span>${escape(
word,
)}</span></span>`,
)
.join("");
}
+6 -3
View File
@@ -1,6 +1,7 @@
/* views/admin.js — หลังบ้าน: แก้ชื่อ/คำอธิบาย/ลิงก์ แล้วเผยแพร่หรือดาวน์โหลด JSON */
import { escapeHtml, icon, isEmail, normalizeUrl, toast, debounce } from "../ui.js";
import { initReveal } from "../motion.js";
import {
clearDraft,
createProject,
@@ -90,7 +91,7 @@ export function adminView(ctx) {
<span data-status-extra></span>
</div>
<section class="panel" aria-labelledby="site-panel-title">
<section class="panel" data-reveal="fade" aria-labelledby="site-panel-title">
<div class="panel__head">
<h2 class="panel__title" id="site-panel-title">ข้อความบนหน้าเว็บ</h2>
</div>
@@ -120,7 +121,7 @@ export function adminView(ctx) {
</div>
</section>
<section class="panel" aria-labelledby="projects-panel-title">
<section class="panel" data-reveal="fade" aria-labelledby="projects-panel-title">
<div class="panel__head">
<h2 class="panel__title" id="projects-panel-title">
รายชื่อโปรเจค (<span data-project-count>${draft.projects.length}</span>)
@@ -134,7 +135,7 @@ export function adminView(ctx) {
</div>
</section>
<section class="panel" aria-labelledby="publish-panel-title">
<section class="panel" data-reveal="fade" aria-labelledby="publish-panel-title">
<div class="panel__head">
<h2 class="panel__title" id="publish-panel-title">ช่องทางบันทึก</h2>
</div>
@@ -184,6 +185,8 @@ export function adminView(ctx) {
}
refreshDirty();
checkApi();
// เรียกซ้ำได้เสมอ เพราะ mount() ถูกเรียกใหม่หลังกด “ทิ้งการแก้ไข”
initReveal(root);
}
function bindSiteFields() {
+14 -6
View File
@@ -1,6 +1,7 @@
/* views/home.js — หน้าแรก: ข้อความต้อนรับ + ค้นหา + การ์ดโปรเจค */
import { escapeHtml, hostOf, icon, normalizeUrl } from "../ui.js";
import { initReveal, splitWordsHtml } from "../motion.js";
export function homeView(ctx) {
let root = null;
@@ -21,7 +22,7 @@ export function homeView(ctx) {
});
}
function cardHtml(project) {
function cardHtml(project, index = 0) {
const url = normalizeUrl(project.project_url);
const host = hostOf(project.project_url);
const name = escapeHtml(project.project_name || "ไม่มีชื่อโปรเจค");
@@ -30,7 +31,7 @@ export function homeView(ctx) {
? `<a href="${escapeHtml(url)}" target="_blank" rel="noopener noreferrer">${name}</a>`
: `<span>${name}</span>`;
return `
<article class="project-card">
<article class="project-card" data-reveal="pop" style="--fx-delay:${(index % 3) * 95}ms">
<div class="project-card__top">
<span class="project-card__icon" aria-hidden="true">${icon(project.icon)}</span>
<div class="project-card__heading">
@@ -70,7 +71,9 @@ export function homeView(ctx) {
return;
}
gridEl.className = "project-grid";
gridEl.innerHTML = items.map(cardHtml).join("");
gridEl.innerHTML = items.map((project, index) => cardHtml(project, index)).join("");
// การ์ดชุดใหม่หลังกรองต้องถูก observe/เปิดให้เห็นด้วย ไม่งั้นจะค้างที่ opacity 0
initReveal(gridEl);
}
return {
@@ -83,12 +86,17 @@ export function homeView(ctx) {
root.innerHTML = `
<div class="wrap">
<section class="hero" aria-labelledby="hero-title">
<div class="hero__ambience" aria-hidden="true">
<span class="hero__orb hero__orb--a"></span>
<span class="hero__orb hero__orb--b"></span>
</div>
<div class="hero__inner">
<p class="hero__eyebrow">${icon("spark")}<span>${escapeHtml(
site.site_title,
)}</span></p>
<h1 class="hero__title" id="hero-title">${escapeHtml(
<h1 class="hero__title" id="hero-title">${splitWordsHtml(
site.welcome_message,
escapeHtml,
)}</h1>
<p class="hero__lead">รวมเว็บที่ทำเองไว้ในที่เดียว เลือกดูได้ตามใจ ไม่มีโฆษณา ไม่มีล็อกอิน</p>
<form class="searchbar" role="search" autocomplete="off">
@@ -101,7 +109,7 @@ export function homeView(ctx) {
</button>
</form>
<div class="hero__meta">
<span>${icon("folder")}${total} โปรเจค</span>
<span>${icon("folder")}<span data-countup="${total}">${total}</span> โปรเจค</span>
<span>${icon("shield")}${escapeHtml(site.ssl_note)}</span>
${
withEmail
@@ -124,7 +132,7 @@ export function homeView(ctx) {
</div>
</section>
<div class="section-head">
<div class="section-head" data-reveal="fade">
<div>
<h2 class="section-head__title" id="projects-title">รายชื่อโปรเจค</h2>
<p class="section-head__sub">กดที่การ์ดเพื่อเปิดเว็บนั้นในแท็บใหม่</p>