结构
展开区域
先显示长文本的开头几行,按需展开全文。
本页内容
展开区域先显示长文本的前几行,用户可按需查看余下内容,适用于描述、评论或更新日志条目。收起时,文本底部逐渐淡出。文本展开至完整高度时,下方的切换控件会翻转。
展开区域
<details class="expand max-w(420) mx-auto text-sm leading-md fg-text-body"> <summary class="expand-toggle"> <span class="expand-more">Show more</span><span class="expand-less">Show less</span><svg class="icon expand-chevron" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" transform="rotate(90 8 8)" /> </svg> </summary> <p class="m(0)"> Moonstone is a feldspar with a soft glow that seems to move under its surface as the stone turns. The effect comes from light scattering between thin layers inside it. Cut as a smooth dome, it shows a band of light that glides across the top. The theme takes its name from that glow: surfaces are pale and even, edges are found by a line of light before a line of ink, and nothing ends in a hard corner. Obsidian is its counterpart, a volcanic glass, dark and smooth, with edges that catch the light. </p></details>import ChevronDownIcon from '@master/icons-react/native/16/soft/chevron-down'<details className="expand max-w(420) mx-auto text-sm leading-md fg-text-body"> <summary className="expand-toggle"> <span className="expand-more">Show more</span><span className="expand-less">Show less</span><ChevronDownIcon className="icon expand-chevron" /> </summary> <p className="m(0)"> Moonstone is a feldspar with a soft glow that seems to move under its surface as the stone turns. The effect comes from light scattering between thin layers inside it. Cut as a smooth dome, it shows a band of light that glides across the top. The theme takes its name from that glow: surfaces are pale and even, edges are found by a line of light before a line of ink, and nothing ends in a hard corner. Obsidian is its counterpart, a volcanic glass, dark and smooth, with edges that catch the light. </p></details><script> import ChevronDownIcon from '@master/icons-svelte/native/16/soft/chevron-down'</script><details class="expand max-w(420) mx-auto text-sm leading-md fg-text-body"> <summary class="expand-toggle"> <span class="expand-more">Show more</span><span class="expand-less">Show less</span><ChevronDownIcon class="icon expand-chevron" /> </summary> <p class="m(0)"> Moonstone is a feldspar with a soft glow that seems to move under its surface as the stone turns. The effect comes from light scattering between thin layers inside it. Cut as a smooth dome, it shows a band of light that glides across the top. The theme takes its name from that glow: surfaces are pale and even, edges are found by a line of light before a line of ink, and nothing ends in a hard corner. Obsidian is its counterpart, a volcanic glass, dark and smooth, with edges that catch the light. </p></details><script setup>import ChevronDownIcon from '@master/icons-vue/native/16/soft/chevron-down'</script><template> <details class="expand max-w(420) mx-auto text-sm leading-md fg-text-body"> <summary class="expand-toggle"> <span class="expand-more">Show more</span><span class="expand-less">Show less</span><ChevronDownIcon class="icon expand-chevron" /> </summary> <p class="m(0)"> Moonstone is a feldspar with a soft glow that seems to move under its surface as the stone turns. The effect comes from light scattering between thin layers inside it. Cut as a smooth dome, it shows a band of light that glides across the top. The theme takes its name from that glow: surfaces are pale and even, edges are found by a line of light before a line of ink, and nothing ends in a hard corner. Obsidian is its counterpart, a volcanic glass, dark and smooth, with edges that catch the light. </p> </details></template>组件使用原生 <details>,切换控件是它的 <summary>,其中包含文字和一个 expand-chevron 图标,因此原生支持键盘操作、页内查找和辅助技术。浏览器提供 ::details-content 时,收起状态会显示预览。其他浏览器沿用普通 details 的行为,收起时隐藏正文,仍可通过切换控件展开。
按 <details> 的要求,summary 在标记中排在最前面,视觉上显示在正文下方。
#预览行数
--expand-lines 以正文自身的行高为单位设置预览高度,默认显示四行。
预览行数
<details class="expand max-w(420) mx-auto text-sm leading-md fg-text-body" style="--expand-lines: 2"> <summary class="expand-toggle"> <span class="expand-more">Read the full note</span><span class="expand-less">Show less</span><svg class="icon expand-chevron" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" transform="rotate(90 8 8)" /> </svg> </summary> <p class="m(0)"> Moonstone is a feldspar with a soft glow that seems to move under its surface as the stone turns. The effect comes from light scattering between thin layers inside it. Cut as a smooth dome, it shows a band of light that glides across the top. The theme takes its name from that glow: surfaces are pale and even, edges are found by a line of light before a line of ink, and nothing ends in a hard corner. Obsidian is its counterpart, a volcanic glass, dark and smooth, with edges that catch the light. </p></details>import ChevronDownIcon from '@master/icons-react/native/16/soft/chevron-down'<details className="expand max-w(420) mx-auto text-sm leading-md fg-text-body" style={{ '--expand-lines': '2' } as React.CSSProperties}> <summary className="expand-toggle"> <span className="expand-more">Read the full note</span><span className="expand-less">Show less</span><ChevronDownIcon className="icon expand-chevron" /> </summary> <p className="m(0)"> Moonstone is a feldspar with a soft glow that seems to move under its surface as the stone turns. The effect comes from light scattering between thin layers inside it. Cut as a smooth dome, it shows a band of light that glides across the top. The theme takes its name from that glow: surfaces are pale and even, edges are found by a line of light before a line of ink, and nothing ends in a hard corner. Obsidian is its counterpart, a volcanic glass, dark and smooth, with edges that catch the light. </p></details><script> import ChevronDownIcon from '@master/icons-svelte/native/16/soft/chevron-down'</script><details class="expand max-w(420) mx-auto text-sm leading-md fg-text-body" style="--expand-lines: 2"> <summary class="expand-toggle"> <span class="expand-more">Read the full note</span><span class="expand-less">Show less</span><ChevronDownIcon class="icon expand-chevron" /> </summary> <p class="m(0)"> Moonstone is a feldspar with a soft glow that seems to move under its surface as the stone turns. The effect comes from light scattering between thin layers inside it. Cut as a smooth dome, it shows a band of light that glides across the top. The theme takes its name from that glow: surfaces are pale and even, edges are found by a line of light before a line of ink, and nothing ends in a hard corner. Obsidian is its counterpart, a volcanic glass, dark and smooth, with edges that catch the light. </p></details><script setup>import ChevronDownIcon from '@master/icons-vue/native/16/soft/chevron-down'</script><template> <details class="expand max-w(420) mx-auto text-sm leading-md fg-text-body" style="--expand-lines: 2"> <summary class="expand-toggle"> <span class="expand-more">Read the full note</span><span class="expand-less">Show less</span><ChevronDownIcon class="icon expand-chevron" /> </summary> <p class="m(0)"> Moonstone is a feldspar with a soft glow that seems to move under its surface as the stone turns. The effect comes from light scattering between thin layers inside it. Cut as a smooth dome, it shows a band of light that glides across the top. The theme takes its name from that glow: surfaces are pale and even, edges are found by a line of light before a line of ink, and nothing ends in a hard corner. Obsidian is its counterpart, a volcanic glass, dark and smooth, with edges that catch the light. </p> </details></template>#切换控件的文案
将两种状态的文案分别放在 expand-more 和 expand-less 中,样式表会显示与当前状态对应的文案。条件允许时,用“阅读完整评论”这类文案明确说明展开后能看到什么,单写“更多”则过于笼统。如果同一句文案适用于两种状态,直接把它作为纯文本写在 summary 中。
#使用场景
展开区域适合呈现大多数读者只会快速浏览的长文本。问答列表适合用折叠面板,每一项收起时只显示标题。把链接和控件放在收起时仍可见的部分,因为收起时,预览范围之外被裁切的内容仍可通过键盘访问。
#无障碍
- 辅助技术将 summary 识别为按钮,并通过它获知正文是否已展开。页内查找命中其中的内容时,会展开 details。
- 底部的淡出效果由覆盖在文本上的遮罩实现,因此最后一条可见文本行会变淡,前几行则保持完整的对比度。
- 启用减少动态效果时,文本会立即展开。
#类名
| 类名 | 说明 |
|---|---|
expand | details 元素,折叠时显示底部渐隐的预览。 |
expand-toggle | summary 在标记中排在最前,显示在正文下方,带可翻转的箭头。 |
expand-chevron | 切换控件在文字之后所含的箭头图标,正文展开时翻转 |
expand-more | 区域折叠时切换控件显示的文案 |
expand-less | 区域展开时切换控件显示的文案 |
#自定义属性
| 属性 | 默认值 | 说明 |
|---|---|---|
--expand-lines | 4 | 预览显示的行数,按正文自身的行高计算 |