ui: add shared motion layer (page enter, scroll reveal, loading bar)

- ui/motion.js: Motion.enter/scan/loading, IntersectionObserver reveal with
  sweep fallback for jump-scroll, hidden tab and late-rendered cards
- _shared.css: --mu-ease token, .mu-loadbar, #view.mu-view-in, .mu-reveal
- router.js: start/stop loading bar, replay enter animation per view
- reduced-motion disables everything; maps are never transformed
- sw.js: precache motion.js, VERSION 2026.09.27.104
This commit is contained in:
Bkk1234 committed 2026-11-02 23:56:16 +07:00
1 parent ba5f4fd887
commit 5381e0690a
7 files changed
+351 -6

No files matched your search

+19
View File
@@ -130,6 +130,25 @@ python -m http.server 8090 # เปิด http://localhost:8090/ui/index.
- `.gitignore` ว่างเปล่า — ถ้าเพิ่มไฟล์ขยะ (เช่น .DS_Store) ให้เพิ่มใน .gitignore
- งานถัดไปที่คาดว่าเกี่ยวข้อง: นำดีไซน์นี้ไป implement ใน `web-at-transit` (Next.js)
## การเคลื่อนไหว (ui/motion.js) — อนิเมชันร่วมทุกหน้า
**ไฟล์:** `ui/motion.js` (โหลดใน `index.html` ก่อน `router.js`) + CSS ท้าย `_shared.css` (`.mu-loadbar`, `#view.mu-view-in`, `.mu-reveal`)
**สเปก/โทเคน:** `DESIGN.md §8` — อย่าคิดระยะเวลาใหม่เอง
| ทำอะไร | เรียกจากไหน |
|---|---|
| แถบโหลดบางบนสุดระหว่าง fetch partial | `router.js` → `Motion.loading(true/false)` |
| หน้าใหม่ค่อย ๆ โผล่ (fade + 10px) | `router.js` → `Motion.enter(viewEl)` หลัง inject |
| การ์ดโผล่ตอนเลื่อน | อัตโนมัติ: `scan()` ใน `#view` + `MutationObserver` (การ์ดจาก JSON ก็ได้) |
**กติกา**
- อนิเมตเฉพาะ `opacity` + `transform` · ห้าม width/height/top/left
- **ห้าม**ใส่ `.mu-reveal` ให้อิลิเมนต์ที่ครอบแผนที่ (`.at-map`, `.leaflet-container`, `.blog-mapx`) — ใส่ `data-no-reveal` ถ้าต้องการยกเว้นเอง
- ของที่เห็นบนจอตอนโหลดไม่ถูกอนิเมต · หลังจบจะลบ class ทิ้ง (hover lift เดิมไม่เพี้ยน)
- `prefers-reduced-motion: reduce` → ปิดทั้งหมด (ทั้ง JS และ CSS)
- เพิ่มหน้าใหม่: ไม่ต้องแก้อะไร (router + observer จัดการ) — เพียงใช้คลาสการ์ดเดิม (`mu-card`, `mu-stop-card`, `mu-zone`, `mu-summary`, `mu-stat`, …)
- อย่าลืม `./motion.js` ใน `PRECACHE` ของ `sw.js` (มีแล้ว) และ bump `VERSION` เมื่อแก้ SW
## หน้ารายละเอียดป้าย `#/stops/<รหัสป้าย>` — ข้อมูลมาจากไหน
หน้า `ui/partials/stopDetail.html` เป็น "โครงเปล่า" ล้วน ๆ (มีแต่ `<style>` + element ที่มี id)
+22
View File
@@ -222,6 +222,28 @@ subset thai/latin/latin-ext · ~218 KB) + ไฟล์ `.woff2` 18 ไฟล์
---
## 8. การเคลื่อนไหว (Motion) — `ui/motion.js` + ท้าย `_shared.css`
**หลักคิด:** อนิเมชันต้องบอก "ของใหม่เข้ามา" ไม่ใช่ของเล่น — ทุกอย่างจบใน ≤ 0.6 วิ และต้องปิดได้
| บทบาท | ค่า | ที่มา |
|---|---|---|
| เส้นโค้ง | `--mu-ease` = `cubic-bezier(.22,.61,.36,1)` (deceleration) | MD motion |
| เข้าหน้า | 420ms · จาง + ขึ้น 10px (`#view.mu-view-in`) | — |
| การ์ดโผล่ตอนเลื่อน | 520ms · จาง + ขึ้น 18px (`.mu-reveal` → `.is-in`) | — |
| เหลื่อมต่อชิ้น | 45ms/ชิ้น (สูงสุด 4 ชิ้น = 180ms) | MD stagger 30–50ms |
| แถบโหลด | 3px บนสุด · โผล่/จาง 180–260ms (ออกเร็วกว่าเข้า) | MD exit-faster |
**กฎ**
1. แตะเฉพาะ `opacity` + `transform` — **ห้ามอนิเมต width/height/top/left** (ทำให้เกิด layout shift)
2. **ห้ามใส่ `.mu-reveal` ให้ของที่ครอบแผนที่** (`SKIP_INSIDE` ใน motion.js — Leaflet คำนวณพิกัดจากกล่องแม่)
3. ของที่เห็นบนจออยู่แล้วตอนโหลด **ไม่ต้องอนิเมต** (กันกะพริบ) — JS เลือกให้เอง
4. เคลียร์ class/transform ทิ้งหลังจบ → hover lift เดิมของแต่ละหน้าไม่ถูกทับ
5. `prefers-reduced-motion: reduce` = ปิดทั้งชุด (ไม่แตะ DOM, ไม่มีแถบโหลด)
6. เพิ่มหน้าใหม่ไม่ต้องแก้อะไร — router เรียก `Motion.enter()` ให้ และ MutationObserver สแกนการ์ดที่ JS สร้างทีหลัง
---
## 9. รูปภาพ (Asset cache)
รูปทุกตัวเติม `?v=<เวอร์ชันแอป>` อัตโนมัติผ่าน `ui/imageurl.js` — เปลี่ยนรูปทับชื่อเดิมแล้ว
+70
View File
@@ -518,3 +518,73 @@ body:has(.mu-site-footer) .mu-footer { display: none; }
}
.mu-install-btn:active { transform: translateY(1px); }
.mu-install-btn svg { width: 20px; height: 20px; }
/* =========================================================
Motion — อนิเมชันร่วมทุกหน้า (ไฟล์ JS: ui/motion.js)
---------------------------------------------------------
โทเคนเดียวทั้งเว็บ: ระยะ/ความเร็ว/เส้นโค้ง อยู่ที่นี่ที่เดียว
· --mu-ease = deceleration (เข้าช้า ออกเร็ว ตาม MD motion)
· 420ms = เข้าหน้า · 520ms = reveal · 45ms = ระยะเหลื่อมต่อชิ้น
ปิดทั้งชุดเมื่อผู้ใช้ตั้ง prefers-reduced-motion: reduce
========================================================= */
:root {
--mu-ease: cubic-bezier(.22, .61, .36, 1);
}
/* ---------- แถบโหลดบาง ๆ บนสุด (ระหว่าง fetch partial) ---------- */
.mu-loadbar {
position: fixed;
top: 0;
left: 0;
right: 0;
height: 3px;
z-index: 1200; /* เหนือ topbar (1100) แต่ใต้ vreset (3000) */
pointer-events: none;
opacity: 0;
transition: opacity .25s ease; /* ออกเร็วกว่าเข้า (60–70%) */
}
.mu-loadbar.is-on { opacity: 1; }
.mu-loadbar-fill {
height: 100%;
width: 100%;
transform: scaleX(0);
transform-origin: 0 50%;
border-radius: 0 2px 2px 0;
background: linear-gradient(90deg, #7a4cc0, #b694f6);
box-shadow: 0 0 8px rgba(122, 76, 192, .45);
}
@media (prefers-reduced-motion: no-preference) {
/* ---------- เข้าหน้าใหม่: จาง + เลื่อนขึ้นเล็กน้อย ---------- */
#view.mu-view-in {
animation: muViewIn .42s var(--mu-ease) both;
}
@keyframes muViewIn {
from { opacity: 0; transform: translate3d(0, 10px, 0); }
to { opacity: 1; transform: none; }
}
/* ---------- เลื่อนแล้วค่อยโผล่ ---------- */
/* JS ใส่ .mu-reveal เฉพาะของที่ยังไม่เห็นบนจอ → ไม่มีอะไรกะพริบตอนโหลด
ใช้เฉพาะ opacity + transform = ไม่ดันเลย์เอาต์ (CLS 0) */
.mu-reveal {
opacity: 0;
transform: translate3d(0, 18px, 0);
transition:
opacity .52s var(--mu-ease),
transform .52s var(--mu-ease);
transition-delay: var(--mu-d, 0ms);
}
.mu-reveal.is-in {
opacity: 1;
transform: none;
}
}
/* ลดการเคลื่อนไหว: ไม่มีอนิเมชัน แต่เนื้อหาต้องอ่านได้ทันที */
@media (prefers-reduced-motion: reduce) {
#view.mu-view-in { animation: none; }
.mu-reveal { opacity: 1; transform: none; transition: none; }
.mu-loadbar { display: none; }
}
+6 -5
View File
@@ -12,8 +12,8 @@
<link href="./_shared.css" rel="stylesheet">
<link href="./vendor/leaflet/leaflet.css" rel="stylesheet">
<link href="./map.css" rel="stylesheet">
<meta name="app-version" content="2026.09.27.103">
<link rel="icon" type="image/png" href="../.image/icons/minibus-512x512.png?v=2026.09.27.103">
<meta name="app-version" content="2026.09.27.104">
<link rel="icon" type="image/png" href="../.image/icons/minibus-512x512.png?v=2026.09.27.104">
<style>
.mu-card.empty { width: 100%; padding: 16px; text-align: center; color: #6f7b95; background: #fff7fb; border: 1px dashed #f4c7df; }
a.mu-card { text-decoration: none; color: inherit; display: block; }
@@ -339,7 +339,7 @@
<meta name="theme-color" content="#7a4cc0">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="default">
<link rel="apple-touch-icon" href="../.image/icons/minibus-192x192.png?v=2026.09.27.103">
<link rel="apple-touch-icon" href="../.image/icons/minibus-192x192.png?v=2026.09.27.104">
<script src="./imageurl.js" defer></script>
<script src="./pwa.js" defer></script>
<script src="./i18n.js" defer></script>
@@ -349,6 +349,7 @@
<script src="./map.js" defer></script>
<script src="./partials/schedules.js" defer></script>
<script src="./partials/otherStop.js" defer></script>
<script src="./motion.js" defer></script>
<script src="./router.js" defer></script>
<link rel="stylesheet" href="./responsive.css">
</head>
@@ -444,7 +445,7 @@
<header class="mu-topbar">
<div class="mu-container mu-topbar-inner">
<a class="mu-brand" href="#/" aria-label="Atroute — หน้าแรก" data-i18n-attr="aria-label:aria.brandHome">
<img class="mu-logo-img" src="../.image/icons/minibus-512x512.png?v=2026.09.27.103" alt="Atroute logo" width="44" height="44" decoding="async">
<img class="mu-logo-img" src="../.image/icons/minibus-512x512.png?v=2026.09.27.104" alt="Atroute logo" width="44" height="44" decoding="async">
<span>
<span class="mu-brand-title">Atroute</span>
<span class="mu-brand-subtitle" data-i18n="brand.subtitle">สถานีขนส่งผู้โดยสารจังหวัดอ่างทอง</span>
@@ -487,7 +488,7 @@
<div class="mu-footer-grid">
<div class="mu-footer-brand">
<div class="mu-footer-brand-row">
<img src="../.image/icons/minibus-512x512.png?v=2026.09.27.103" alt="Atroute">
<img src="../.image/icons/minibus-512x512.png?v=2026.09.27.104" alt="Atroute">
<div>
<strong>Atroute</strong>
<span data-i18n="brand.subtitle">สถานีขนส่งผู้โดยสารจังหวัดอ่างทอง</span>
+226
View File
@@ -0,0 +1,226 @@
/* ============================================================
Atroute — motion layer
------------------------------------------------------------
อนิเมชันเบา ๆ ที่ใช้ร่วมทุกหน้า (โหลดจาก shell ก่อน router.js)
1) แถบโหลดบาง ๆ บนสุด Motion.loading(true|false)
2) หน้าใหม่ค่อย ๆ โผล่ Motion.enter(#view) ← router เรียกหลัง inject
3) เลื่อนแล้วค่อยโผล่ สแกนอัตโนมัติใน #view + MutationObserver
(การ์ดที่ JS สร้างทีหลังก็ได้อนิเมชัน)
หลักการ
- แตะเฉพาะ opacity + transform → ไม่เกิด layout shift (CLS) และไม่กระตุก
- ทำเฉพาะอิลิเมนต์ที่ยังไม่เห็นบนจอ → ของที่เห็นอยู่แล้วไม่กะพริบ
- เคลียร์คลาส/transform ทิ้งหลังจบ → hover/transform เดิมของแต่ละหน้าไม่ถูกทับ
- prefers-reduced-motion: reduce → ปิดทั้งหมด (ไม่แตะ DOM เลย)
============================================================ */
(function () {
'use strict';
var mqReduce = window.matchMedia ? window.matchMedia('(prefers-reduced-motion: reduce)') : null;
function reduced() { return !!(mqReduce && mqReduce.matches); }
/* ----------------------------------------------------------
1) แถบโหลดด้านบน (บอกว่ากำลังโหลดหน้า — ไม่บล็อกการคลิก)
---------------------------------------------------------- */
var bar = null, barFill = null, growTimer = null, outTimer = null;
function ensureBar() {
if (bar && document.body.contains(bar)) return;
bar = document.createElement('div');
bar.className = 'mu-loadbar';
bar.setAttribute('aria-hidden', 'true');
barFill = document.createElement('div');
barFill.className = 'mu-loadbar-fill';
bar.appendChild(barFill);
document.body.appendChild(bar);
}
function setProgress(p) {
if (!barFill) return;
barFill.style.transition = 'transform .4s cubic-bezier(.22,.61,.36,1)';
barFill.style.transform = 'scaleX(' + p + ')';
barFill.style.transformOrigin = '0 50%';
barFill.style.width = '100%';
}
function loading(on) {
if (reduced() || !document.body) return;
ensureBar();
clearTimeout(growTimer);
clearTimeout(outTimer);
if (on) {
bar.classList.add('is-on');
barFill.style.transition = 'none';
barFill.style.transform = 'scaleX(0)';
void barFill.offsetWidth; // reflow → ให้เริ่มจาก 0 จริง
setProgress(0.18);
growTimer = setTimeout(function () { setProgress(0.5); }, 200);
growTimer = setTimeout(function () { setProgress(0.78); }, 700);
return;
}
setProgress(1);
outTimer = setTimeout(function () {
bar.classList.remove('is-on');
outTimer = setTimeout(function () {
if (!barFill) return;
barFill.style.transition = 'none';
barFill.style.transform = 'scaleX(0)';
}, 260);
}, 180);
}
/* ----------------------------------------------------------
2) หน้าใหม่ค่อย ๆ โผล่ (เรียกจาก router ทุกครั้งที่ inject)
---------------------------------------------------------- */
function enter(el) {
if (!el || reduced()) { firstPass = false; return; }
el.classList.remove('mu-view-in');
void el.offsetWidth; // รีสตาร์ทอนิเมชันได้ทุกครั้ง
el.classList.add('mu-view-in');
firstPass = true;
scan(el);
/* partial บางหน้าเรนเดอร์การ์ดหลัง fetch JSON — รอบสองรับของที่มาช้า */
setTimeout(function () { firstPass = false; scan(el); }, 400);
}
/* ----------------------------------------------------------
3) เลื่อนแล้วค่อยโผล่
---------------------------------------------------------- */
var SELECTOR = [
'.mu-zone', '.mu-card', '.mu-stop-card', '.mu-blog-card', '.mu-blog-row',
'.mu-credit-section', '.mu-summary', '.mu-stat', '.mu-zone-head',
'.mu-sponsor-head', '.mu-schedule', '.mu-kpi', '.mu-post-figure',
'.mu-nearby', '.mu-route-tools'
].join(',');
/* ห้ามใส่ transform ให้ของที่ครอบแผนที่ (Leaflet คำนวณพิกัดจากกล่องแม่) */
var SKIP_INSIDE = '.leaflet-container, .at-map, .blog-mapx, [data-no-reveal]';
var io = ('IntersectionObserver' in window)
? new IntersectionObserver(onIntersect, { rootMargin: '0px 0px -8% 0px', threshold: 0.05 })
: null;
var firstPass = false;
var pending = []; // อิลิเมนต์ที่ยังรอโผล่ (ใช้ตอนกระโดดข้ามหน้า)
function reveal(el) {
if (!el || el.classList.contains('is-in')) return;
var at = pending.indexOf(el);
if (at !== -1) pending.splice(at, 1);
io.unobserve(el);
el.classList.add('is-in');
var d = parseFloat(el.style.getPropertyValue('--mu-d')) || 0;
setTimeout(function () {
el.classList.remove('mu-reveal', 'is-in');
el.style.removeProperty('--mu-d');
}, 700 + d);
}
function onIntersect(entries) {
for (var i = 0; i < entries.length; i++) {
if (entries[i].isIntersecting) reveal(entries[i].target);
}
sweep(); // กันของที่ถูก "ข้าม" ตอนเลื่อนจัมป์
}
/* เปิดของที่ผ่านขึ้นไปเหนือจอแล้วโดยไม่ต้องรอIntersect
(กด End / กดลิงก์ข้ามส่วน / เบราว์เซอร์คืนตำแหน่งเดิม → IO ไม่เคยเห็นของกลางทาง) */
function sweep() {
if (!pending.length) return;
var vh = window.innerHeight || document.documentElement.clientHeight;
for (var i = pending.length - 1; i >= 0; i--) {
var el = pending[i];
if (!el.isConnected) { pending.splice(i, 1); io.unobserve(el); continue; }
if (el.getBoundingClientRect().top < vh) reveal(el);
}
}
var sweepQueued = 0;
function queueSweep() {
if (sweepQueued) return;
sweepQueued = 1;
setTimeout(function () { sweepQueued = 0; sweep(); }, 90);
}
function scan(root) {
if (!io || reduced()) return;
root = root || document.getElementById('view');
if (!root) return;
var nodes = root.querySelectorAll(SELECTOR);
if (!nodes.length) return;
/* ตัดของจากหน้าจอเก่าที่ถูกแทนที่แล้วออกจากคิว */
pending = pending.filter(function (el) { return el.isConnected; });
var vh = window.innerHeight || document.documentElement.clientHeight;
var idx = 0;
for (var i = 0; i < nodes.length; i++) {
var el = nodes[i];
if (el.classList.contains('mu-reveal') || el.classList.contains('is-in')) continue;
if (el.closest(SKIP_INSIDE)) continue;
/* ซ้อนกันอยู่ → ปล่อยให้ตัวแม่เป็นคนอนิเมต (ลด observer + ไม่กระตุกซ้อน) */
if (el.parentElement && el.parentElement.closest(SELECTOR) && root.contains(el.parentElement.closest(SELECTOR))) continue;
var rect = el.getBoundingClientRect();
if (rect.width === 0 && rect.height === 0) continue; // ซ่อนอยู่
if (firstPass && rect.top < vh) continue; // เห็นอยู่แล้ว → ไม่ต้องอนิเมต
el.style.setProperty('--mu-d', (Math.min(idx, 4) * 45) + 'ms');
el.classList.add('mu-reveal');
pending.push(el);
idx++;
io.observe(el);
}
/* ของใหม่ดันความสูงหน้า → กวาดซ้ำอีกรอบหลังเลย์เอาต์นิ่ง */
if (idx) setTimeout(sweep, 300);
}
/* เลื่อน/ย่อหน้า → เปิดของที่เลยขึ้นไปแล้ว (กันค้าง opacity 0) */
window.addEventListener('scroll', queueSweep, { passive: true });
window.addEventListener('resize', queueSweep);
/* กลับมาที่แท็บ (จากพื้นหลัง) → ตรวจอีกรอบ เผื่อของค้างจากตอนที่ยังไม่วาดจอ */
document.addEventListener('visibilitychange', function () {
if (document.visibilityState === 'visible') queueSweep();
});
/* ปริ้นต์/บันทึก PDF ต้องเห็นครบทุกการ์ด */
window.addEventListener('beforeprint', function () {
var rest = pending.slice();
for (var i = 0; i < rest.length; i++) reveal(rest[i]);
});
/* การ์ดที่ JS สร้างทีหลัง (fetch JSON / กรอง / เปลี่ยนภาษา) → สแกนซ้ำให้เอง
ใช้ setTimeout ไม่ใช่ rAF เพราะแท็บที่ถูกซ่อนจะไม่ยิง rAF */
var scanTimer = 0;
function scheduleScan() {
if (scanTimer) return;
scanTimer = setTimeout(function () {
scanTimer = 0;
scan();
}, 80);
}
function watch() {
var host = document.getElementById('view');
if (!host || !window.MutationObserver) return;
new MutationObserver(function (list) {
for (var i = 0; i < list.length; i++) {
if (list[i].addedNodes && list[i].addedNodes.length) { scheduleScan(); return; }
}
}).observe(host, { childList: true, subtree: true });
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', watch);
} else {
watch();
}
window.Motion = { enter: enter, scan: scan, loading: loading, reduced: reduced };
})();
+6
View File
@@ -123,6 +123,8 @@
window.__route = route; // ให้ script ใน partial อ่าน params ได้
if (window.Motion) Motion.loading(true); // แถบโหลดบาง ๆ ระหว่าง fetch
fetch(view.partial, { cache: 'no-store' })
.then(function (res) {
if (!res.ok) throw new Error('HTTP ' + res.status);
@@ -140,10 +142,14 @@
document.title = (window.I18N && view.tkey) ? I18N.t(view.tkey) : ('Atroute — ' + view.title);
window.scrollTo(0, 0);
/* อนิเมชันเข้าหน้า + สแกนการ์ดที่จะค่อย ๆ โผล่ตอนเลื่อน (ui/motion.js) */
if (window.Motion) { Motion.loading(false); Motion.enter(viewEl); }
/* footer "สายที่เปิดดูบ่อย" — เรนเดอร์ทุกครั้ง (ตามภาษา + อันดับล่าสุด) */
mountPopular();
})
.catch(function () {
if (window.Motion) Motion.loading(false);
viewEl.innerHTML =
'<div class="mu-container" style="padding:64px 28px;text-align:center;color:#6f7b95;">' +
(window.I18N ? I18N.t('router.loadFail') : 'โหลดเนื้อหาไม่สำเร็จ') + '</div>';
+2 -1
View File
@@ -13,7 +13,7 @@
- ขั้นตอนอัปเดต: ดู AGENTS.md หัวข้อ PWA
============================================================ */
const VERSION = "2026.09.27.103"; // ← bump ทุกครั้งที่แก้ SW เอง
const VERSION = "2026.09.27.104"; // ← bump ทุกครั้งที่แก้ SW เอง
const CACHE = `at-transit-${VERSION}`;
const PRECACHE = [
@@ -33,6 +33,7 @@ const PRECACHE = [
"./popular.js",
"./visitors.js",
"./router.js",
"./motion.js",
"./partials/home.html",
"./partials/home.init.js",
"./partials/busStop.html",