摘要
今天先顯示一個「引起共鳴」的進場畫面,按下「一起看看今天怎麼了 →」後再切到既有的拖延紀錄表單。專注在 DOM 顯示/隱藏與焦點管理,確保新手容易上手且與 Day 10/11 的清單功能無衝突。
進入 → 共鳴畫面 → 點按 → 表單
假設你已具備 Day 5/6 的 readRecords()、writeRecords()、Day 7/9 的 renderHistory() 與 Day 10/11 的清單功能外框。以下片段著重新增/修改的進場畫面邏輯。
HTML:
<!-- 引起共鳴畫面 -->
<section id="resonanceScreen" aria-labelledby="res-title" role="dialog" aria-modal="true">
<div>
<h1 id="res-title">你是不是也常常…</h1>
<ul>
<li>看了一堆自我提升影片,結果還是沒開始?</li>
<li>打開 IG/小紅書 想找靈感,結果一個多小時過去?</li>
<li>明明有想做的事,卻又莫名其妙滑到半夜?</li>
</ul>
<p><strong>想想看,假日想做但沒做的事通常是什麼?</strong></p>
<div>
<button id="btnStart" aria-describedby="res-desc">一起看看今天怎麼了 →</button>
</div>
</div>
</section>
<!-- 紀錄表單 -->
<section id="recordFormSection">
<h1>I want to...</h1>
...
</section>
JavaScript:
// ===== 引起共鳴畫面 =====
// 元素
const introEl = document.getElementById('resonanceScreen');
const startBtn = document.getElementById('btnStart');
const formSection = document.getElementById('recordFormSection');
// 進入頁面時只顯示共鳴畫面:把表單區「隱藏」
function showIntro() {
if (!introEl) return;
introEl.hidden = false;
if (formSection) formSection.hidden = true; // ← 隱藏
}
// 按「一起看看今天怎麼了 →」後:隱藏共鳴、顯示表單、聚焦第一欄
function hideIntro() {
if (!introEl) return;
introEl.hidden = true;
if (formSection) formSection.hidden = false; // ← 顯示回來
focusFirstField();
if (typeof renderHistory === 'function') {
try { renderHistory(); } catch (_) {}
}
}
// 嘗試把焦點放到第一個可輸入欄位
function focusFirstField() {
const first = document.querySelector('#taskInput');
if (first && typeof first.focus === 'function') {
try { first.focus(); } catch (_) {}
}
}
// 事件:點按開始 → 切換到表單
startBtn?.addEventListener('click', () => {
hideIntro();
});
// 初始化:一律顯示共鳴畫面
(function initIntro() {
showIntro(); // 預設進來先顯示共鳴、隱藏表單
})();
進場狀態:重新整理 → 只看到共鳴畫面;表單被隱藏。
按鈕切換:點「一起看看今天怎麼了 →」→ 共鳴消失、表單出現。
焦點管理:切換後游標在 #taskInput(或第一個可輸入欄位)。
功能相容:既有的新增/排序/篩選/刪除/Undo 都能正常運作。
結構正確:表單內容確實包在 #recordFormSection 內;script 載入順序在 HTML 之後(或用 defer / DOMContentLoaded)。