捲動區域
內容超出哪一側的邊緣,該側就會淡出。
本頁內容
捲動區域是頁面內可獨立捲動的範圍,例如面板中的長清單、寬表格或記錄。它沿用平台的原生捲動行為,搭配纖細低調的捲動條。內容超出哪一側,該側邊緣就會淡出。捲到內容盡頭時,該側邊緣會恢復清晰。閱讀期間內容會在結尾持續增加的區域,例如聊天或建置記錄,也可以跟隨新內容。
捲動區域
<div class="sheet max-w(360) mx-auto"> <div class="scroll-area h(200) p-md text-sm fg-text-body leading-md" tabindex="0" role="region" aria-label="Release notes"> <p class="m(0)">2.1 adds overlays that need no script.</p> <p class="m(0) mt-sm">Dialogs open with invoker commands and trap focus on their own.</p> <p class="m(0) mt-sm"> Popovers, menus and tooltips are placed with anchor positioning and flip at the viewport edge. </p> <p class="m(0) mt-sm"> The select draws its option list in the same material where the browser allows it. </p> <p class="m(0) mt-sm">Segmented controls and tabs glide one indicator between options.</p> <p class="m(0) mt-sm"> Corners turn with continuous curvature where corner-shape is supported. </p> <p class="m(0) mt-sm">Every one of these falls back to a working component.</p> </div></div><div className="sheet max-w(360) mx-auto"> <div className="scroll-area h(200) p-md text-sm fg-text-body leading-md" tabIndex={0} role="region" aria-label="Release notes"> <p className="m(0)">2.1 adds overlays that need no script.</p> <p className="m(0) mt-sm">Dialogs open with invoker commands and trap focus on their own.</p> <p className="m(0) mt-sm"> Popovers, menus and tooltips are placed with anchor positioning and flip at the viewport edge. </p> <p className="m(0) mt-sm"> The select draws its option list in the same material where the browser allows it. </p> <p className="m(0) mt-sm">Segmented controls and tabs glide one indicator between options.</p> <p className="m(0) mt-sm"> Corners turn with continuous curvature where corner-shape is supported. </p> <p className="m(0) mt-sm">Every one of these falls back to a working component.</p> </div></div><div class="sheet max-w(360) mx-auto"> <div class="scroll-area h(200) p-md text-sm fg-text-body leading-md" tabindex="0" role="region" aria-label="Release notes"> <p class="m(0)">2.1 adds overlays that need no script.</p> <p class="m(0) mt-sm">Dialogs open with invoker commands and trap focus on their own.</p> <p class="m(0) mt-sm"> Popovers, menus and tooltips are placed with anchor positioning and flip at the viewport edge. </p> <p class="m(0) mt-sm"> The select draws its option list in the same material where the browser allows it. </p> <p class="m(0) mt-sm">Segmented controls and tabs glide one indicator between options.</p> <p class="m(0) mt-sm"> Corners turn with continuous curvature where corner-shape is supported. </p> <p class="m(0) mt-sm">Every one of these falls back to a working component.</p> </div></div><template> <div class="sheet max-w(360) mx-auto"> <div class="scroll-area h(200) p-md text-sm fg-text-body leading-md" tabindex="0" role="region" aria-label="Release notes"> <p class="m(0)">2.1 adds overlays that need no script.</p> <p class="m(0) mt-sm">Dialogs open with invoker commands and trap focus on their own.</p> <p class="m(0) mt-sm"> Popovers, menus and tooltips are placed with anchor positioning and flip at the viewport edge. </p> <p class="m(0) mt-sm"> The select draws its option list in the same material where the browser allows it. </p> <p class="m(0) mt-sm">Segmented controls and tabs glide one indicator between options.</p> <p class="m(0) mt-sm"> Corners turn with continuous curvature where corner-shape is supported. </p> <p class="m(0) mt-sm">Every one of these falls back to a working component.</p> </div> </div></template>#淡出效果如何運作
淡出效果由遮罩繪製,兩個漸層停止點分別由自訂屬性 --scroll-area-start 和 --scroll-area-end 控制。這兩個屬性各由一段動畫驅動,兩段動畫都使用區域自己的捲動時間軸。第一段在最初 1.5rem 的捲動距離內逐漸顯示起始側的淡出效果,第二段則在最後 1.5rem 逐漸移除結束側的淡出效果。整個過程無須使用腳本或偵測用元素。內容沒有溢出時,時間軸不會啟用,兩側邊緣都保持清晰。
瀏覽器若不支援捲動驅動動畫,區域仍可照常捲動,邊緣會保持清晰。
#捲到邊界後
沿區塊軸捲到區域盡頭後,頁面會接著捲動。即使滑鼠滾輪操作或滑動手勢從區域內開始,只要該方向已無內容可捲動,就會像在頁面其他內容上操作一樣捲動頁面。只有行內軸的捲動會限制在區域內,因此側向滑到一列的盡頭時,手勢就此停止,不會觸發瀏覽器的返回手勢。
#沿行內軸捲動
scroll-area-inline 可側向捲動,並在行內起始側與結束側套用淡出效果,適合用於一排卡片或比所在欄位更寬的表格。
沿行內軸捲動
<div class="scroll-area scroll-area-inline" tabindex="0" role="region" aria-label="Regions"> <div class="flex gap-sm w-fit py(4) px(2)"> <span class="chip">Tokyo</span> <span class="chip">Singapore</span> <span class="chip">Sydney</span> <span class="chip">Mumbai</span> <span class="chip">Frankfurt</span> <span class="chip">London</span> <span class="chip">Paris</span> <span class="chip">Stockholm</span> <span class="chip">Virginia</span> <span class="chip">Oregon</span> <span class="chip">São Paulo</span> <span class="chip">Cape Town</span> </div></div><div className="scroll-area scroll-area-inline" tabIndex={0} role="region" aria-label="Regions"> <div className="flex gap-sm w-fit py(4) px(2)"> <span className="chip">Tokyo</span> <span className="chip">Singapore</span> <span className="chip">Sydney</span> <span className="chip">Mumbai</span> <span className="chip">Frankfurt</span> <span className="chip">London</span> <span className="chip">Paris</span> <span className="chip">Stockholm</span> <span className="chip">Virginia</span> <span className="chip">Oregon</span> <span className="chip">São Paulo</span> <span className="chip">Cape Town</span> </div></div><div class="scroll-area scroll-area-inline" tabindex="0" role="region" aria-label="Regions"> <div class="flex gap-sm w-fit py(4) px(2)"> <span class="chip">Tokyo</span> <span class="chip">Singapore</span> <span class="chip">Sydney</span> <span class="chip">Mumbai</span> <span class="chip">Frankfurt</span> <span class="chip">London</span> <span class="chip">Paris</span> <span class="chip">Stockholm</span> <span class="chip">Virginia</span> <span class="chip">Oregon</span> <span class="chip">São Paulo</span> <span class="chip">Cape Town</span> </div></div><template> <div class="scroll-area scroll-area-inline" tabindex="0" role="region" aria-label="Regions"> <div class="flex gap-sm w-fit py(4) px(2)"> <span class="chip">Tokyo</span> <span class="chip">Singapore</span> <span class="chip">Sydney</span> <span class="chip">Mumbai</span> <span class="chip">Frankfurt</span> <span class="chip">London</span> <span class="chip">Paris</span> <span class="chip">Stockholm</span> <span class="chip">Virginia</span> <span class="chip">Oregon</span> <span class="chip">São Paulo</span> <span class="chip">Cape Town</span> </div> </div></template>#跟隨新內容
聊天、建置記錄或串流產生的回答,會在閱讀期間於結尾持續增加。為區域加上 role="log" 並掛載 follow 行為:內容送達時,它會讓結尾保持在視野內;讀者一捲離結尾就停止跟隨,回到結尾時再恢復跟隨。在內容之後放一個帶有 hidden 的 scroll-area-latest 按鈕:它只在區域未跟隨時出現,按下後會捲到結尾並恢復跟隨。
跟隨建置記錄
<div class="sheet max-w(440) mx-auto"> <div class="scroll-area h(240) p-md font-mono text-xs fg-text-body leading-md" tabindex="0" role="log" aria-label="Build log"> <div id="build-lines"> <p class="m(0)">Cloning master-co/ui at 3041b1c</p> <p class="m(0)">Installing 596 packages</p> <p class="m(0)">Compiling 112 stylesheets</p> </div> <button type="button" class="btn btn-sm btn-pill scroll-area-latest" hidden> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M13.6362 7.5189A0.4947 0.4947 0 0 0 13.5 7.5L2.4755 7.5006A0.5 0.5 0 0 0 2.4266 8.4946L2.5 8.5H13.5A0.4947 0.4947 0 0 0 13.6362 8.4811A0.7009 0.7009 0 0 0 13.6362 7.5189Z" fill="currentColor" stroke="none" fill-rule="evenodd" transform="rotate(90 8 8)" /><path d="M8 4.5L13.2348 7.8313A0.2 0.2 0 0 1 13.2348 8.1687L8 11.5" transform="rotate(90 8 8)" /> </svg>Jump to latest </button> </div></div><script>const steps = ['Checking contracts', 'Rendering /components', 'Rendering /blocks', 'Rendering /pages', 'Writing llms.txt', 'Writing the registry', 'Checking 2,914 links', 'Compressing assets', 'Uploading 71 pages']const lines = document.getElementById('build-lines')let count = 0const timer = setInterval(() => { const line = document.createElement('p') line.className = 'm(0)' line.textContent = steps[count % steps.length] + (count >= steps.length ? ' (pass ' + (Math.floor(count / steps.length) + 1) + ')' : '') lines.append(line) if (++count >= 120) clearInterval(timer)}, 800)</script><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>判斷方式:
- 跟隨。 從掛載行為的那一刻起,區域就會跟隨:它會捲到結尾,而每當內容增加,無論是新增一列、串流的文字落在最後一則訊息中,或圖片載入完成,都會再次捲到結尾。
- 停止。 往起點方向捲動就會停止跟隨,無論是由滑鼠滾輪、手指、捲動條、↑、Page Up 或 Home 造成。往起點方向轉動滾輪時,會在區域移動之前立即停止,因此在那一瞬間送達的文字不會把讀者拉回去。
- 恢復跟隨。 用 End、捲動條或按鈕再次到達結尾時,就會恢復跟隨。距離結尾只差幾個像素也算是結尾。
- 事件。 每當讀者讓區域開始或停止跟隨,區域都會派送
change。行為的value表示是否正在跟隨;將它設為true可讓讀者回到結尾,就像應用程式在讀者送出訊息時所做的那樣。設定它不會派送事件。
import { follow } from '@master/ui/behaviors/follow.js'const log = document.querySelector('.scroll-area[role="log"]')const following = follow(log)form.addEventListener('submit', () => { following.value = true })沒有行為時,區域就是一般的捲動區域:它會停在讀者離開的位置,按鈕也會保持隱藏。按鈕以 sticky 定位固定在區域底部,位於內容和淡出邊緣上方,並疊在最後一列上而不是排在它後面,因此顯示按鈕永遠不會讓內容變高。
#保留讀者的位置
在讀者正在看的內容上方插入內容,例如在頂端載入較早的訊息,會把正在閱讀的內容往下推。瀏覽器本身會用捲動錨定避免這種情況:區域會隨著上方的內容移動,讓正在閱讀的訊息留在原處。樣式表在區域上保留 overflow-anchor: auto,並在其按鈕上設定 none,按鈕永遠不會是瀏覽器固定不動的元素。瀏覽器不支援捲動錨定時,follow 行為會自行處理,移動的距離與上方內容移動的距離相同。頂端若有一行內容,新的列會插入在它下方,例如提示正在載入更多內容的說明,也請設定 overflow-anchor: none,讓瀏覽器固定某一列而不是這行說明。
較早的訊息與串流回答
<div class="sheet max-w(440) mx-auto"> <div class="scroll-area h(320) p-md" tabindex="0" role="log" aria-label="Conversation with Ada"> <p class="m(0) pb-sm text-center text-xs fg-text-muted" style="overflow-anchor: none" id="earlier"> Earlier messages load as you scroll up </p> <ol class="m(0) p(0)" id="turns"> <li class="message message-sent"> <div class="message-bubble">Can the docs build run on every push?</div> </li> <li class="message"> <div class="message-bubble">Yes. It takes about two minutes.</div> </li> </ol> <button type="button" class="btn btn-sm btn-pill scroll-area-latest" hidden> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M13.6362 7.5189A0.4947 0.4947 0 0 0 13.5 7.5L2.4755 7.5006A0.5 0.5 0 0 0 2.4266 8.4946L2.5 8.5H13.5A0.4947 0.4947 0 0 0 13.6362 8.4811A0.7009 0.7009 0 0 0 13.6362 7.5189Z" fill="currentColor" stroke="none" fill-rule="evenodd" transform="rotate(90 8 8)" /><path d="M8 4.5L13.2348 7.8313A0.2 0.2 0 0 1 13.2348 8.1687L8 11.5" transform="rotate(90 8 8)" /> </svg>Jump to latest </button> </div></div><script>const turns = document.getElementById('turns')const turn = (text, sent) => { const item = document.createElement('li') item.className = sent ? 'message message-sent' : 'message' const bubble = document.createElement('div') bubble.className = 'message-bubble' bubble.textContent = text item.append(bubble) return item}const older = [['Morning! Is the release still today?', false], ['It is, once the docs build passes.', true], ['Which build?', false], ['The one that checks every page and link.', true], ['Do we wait for it?', false], ['No, it reports here when it is done.', true]]const log = turns.closest('.scroll-area')log.addEventListener('scroll', () => { if (log.scrollTop > 48 || !older.length) return for (const [text, sent] of older.splice(-2).reverse()) turns.prepend(turn(text, sent)) if (!older.length) document.getElementById('earlier').textContent = 'The start of the conversation'})const exchanges = [ ['What does it check?', 'It renders every page in five languages, checks each preview against the contracts, follows every link and compares the registry with the package, then writes the search index.'], ['And if a link is broken?', 'The build stops and names the page and the link, so nothing half checked reaches the site. Fix it and push again; the next run starts from the beginning.'], ['Thanks!', 'Any time. I will post here when this one is done.']]let at = 0const next = () => { if (at >= exchanges.length) return const [question, answer] = exchanges[at++] turns.append(turn(question, true)) const reply = turn('', false) const bubble = reply.firstChild bubble.setAttribute('aria-busy', 'true') turns.append(reply) const words = answer.split(' ') const stream = setInterval(() => { bubble.textContent += (bubble.textContent ? ' ' : '') + words.shift() if (words.length) return clearInterval(stream) bubble.removeAttribute('aria-busy') setTimeout(next, 2500) }, 140)}setTimeout(next, 1200)</script><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>#無障礙
- 使用者必須能透過鍵盤將焦點移到可捲動區域。若區域內沒有可取得焦點的內容,請加上
tabindex="0"、適當的role(例如region)和標籤。 - 遮罩也會讓區域自己的捲動條在相同的邊緣淡出。強制色彩模式下會移除遮罩。
- 為區域設定高度或最大高度,否則它會隨內容增高,不會產生捲動。
role="log"是禮貌型即時區域:新增到其中的內容會被播報,但不會移動焦點。回答正在串流進某則訊息時,請為該訊息加上aria-busy="true",並在回答完成後移除,讓它完整地播報一次,而不是逐字播報。scroll-area-latest按鈕顯示時,是位於 Tab 順序中的真正按鈕。按下後它會隱藏,焦點會移到區域,之後即可用按鍵捲動區域。- 跟隨永遠不會移動焦點,而且讀者一捲離就會停止,因此持續送達的內容永遠不會讓正在閱讀的人失去所在的位置。
#鍵盤操作
按鍵都是瀏覽器原生的;行為只監聽按鍵造成的捲動。
| 按鍵 | 動作 |
|---|---|
| ArrowUp, ArrowDown, PageUp, PageDown, Home, End | 以瀏覽器的預設方式捲動取得焦點的區域 |
| ArrowUp, PageUp, Home | 搭配跟隨行為時:往開頭捲動會停止跟隨,並顯示 scroll-area-latest 按鈕 |
| End | 搭配跟隨行為時:回到末端後,區域會重新開始跟隨 |
| Enter, Space | 在 scroll-area-latest 上:捲動到末端並重新開始跟隨,行為與按鈕相同 |
#React、Svelte 和 Vue
log 元件會以支援跟隨功能的捲動區域包住子元素,並提供按鈕,掛載行為,並接收和傳出 following。使用 npx master-ui add log 將元件複製到專案中;元件的實作方式與用法請見 React、Svelte 和 Vue。
#類別
| 類別 | 說明 |
|---|---|
scroll-area | 沿區塊軸捲動的區域,使用細捲軸。若某側還有未顯示的內容,該側邊緣會淡出。 |
scroll-area-inline | 沿行內軸捲動,並在起始側與結束側的邊緣套用淡出效果。搭配 scroll-area 使用 |
scroll-area-latest | 套用於區域內容之後的 btn:用來回到正在跟隨的區域末端。以黏貼方式固定在內容底部、位於淡出效果上方;區域跟隨中時,行為會為它加上 hidden |
#自訂屬性
| 屬性 | 預設值 | 說明 |
|---|---|---|
--scroll-area-fade | 1.5rem | 各邊緣的淡出長度 |