导航

v2.1.0
媒体

轮播

仅用 CSS 实现的横向或纵向幻灯片序列,可滚动并吸附定位。

本页内容

轮播是一排(或一列)可滚动并吸附到指定位置的幻灯片。它完全由 CSS 实现,轨道是启用了滚动吸附的滚动容器,因此触屏、触控板、键盘和辅助技术都可以按常规方式滚动。在支持相应功能的浏览器中,样式表会添加上一张和下一张按钮,并为每张幻灯片添加一个圆点。浏览器会将它们生成为可聚焦的实际控件。

轮播

<div class="carousel" role="region" aria-label="Featured projects" tabindex="0">  <article class="carousel-slide card card-flat">    <header class="card-header">      <h3 class="card-title">Atlas</h3>      <p class="card-description">A map of every deployment, live.</p>    </header>    <div class="card-body">1.28M requests in the last 24 hours.</div>  </article>  <article class="carousel-slide card card-flat">    <header class="card-header">      <h3 class="card-title">Borealis</h3>      <p class="card-description">Edge functions close to the reader.</p>    </header>    <div class="card-body">Served from 14 regions.</div>  </article>  <article class="carousel-slide card card-flat">    <header class="card-header">      <h3 class="card-title">Cirrus</h3>      <p class="card-description">Storage that follows the project.</p>    </header>    <div class="card-body">7.4 of 10 GB in use.</div>  </article></div>
<div className="carousel" role="region" aria-label="Featured projects" tabIndex={0}>  <article className="carousel-slide card card-flat">    <header className="card-header">      <h3 className="card-title">Atlas</h3>      <p className="card-description">A map of every deployment, live.</p>    </header>    <div className="card-body">1.28M requests in the last 24 hours.</div>  </article>  <article className="carousel-slide card card-flat">    <header className="card-header">      <h3 className="card-title">Borealis</h3>      <p className="card-description">Edge functions close to the reader.</p>    </header>    <div className="card-body">Served from 14 regions.</div>  </article>  <article className="carousel-slide card card-flat">    <header className="card-header">      <h3 className="card-title">Cirrus</h3>      <p className="card-description">Storage that follows the project.</p>    </header>    <div className="card-body">7.4 of 10 GB in use.</div>  </article></div>
<div class="carousel" role="region" aria-label="Featured projects" tabindex="0">  <article class="carousel-slide card card-flat">    <header class="card-header">      <h3 class="card-title">Atlas</h3>      <p class="card-description">A map of every deployment, live.</p>    </header>    <div class="card-body">1.28M requests in the last 24 hours.</div>  </article>  <article class="carousel-slide card card-flat">    <header class="card-header">      <h3 class="card-title">Borealis</h3>      <p class="card-description">Edge functions close to the reader.</p>    </header>    <div class="card-body">Served from 14 regions.</div>  </article>  <article class="carousel-slide card card-flat">    <header class="card-header">      <h3 class="card-title">Cirrus</h3>      <p class="card-description">Storage that follows the project.</p>    </header>    <div class="card-body">7.4 of 10 GB in use.</div>  </article></div>
<template>  <div class="carousel" role="region" aria-label="Featured projects" tabindex="0">    <article class="carousel-slide card card-flat">      <header class="card-header">        <h3 class="card-title">Atlas</h3>        <p class="card-description">A map of every deployment, live.</p>      </header>      <div class="card-body">1.28M requests in the last 24 hours.</div>    </article>    <article class="carousel-slide card card-flat">      <header class="card-header">        <h3 class="card-title">Borealis</h3>        <p class="card-description">Edge functions close to the reader.</p>      </header>      <div class="card-body">Served from 14 regions.</div>    </article>    <article class="carousel-slide card card-flat">      <header class="card-header">        <h3 class="card-title">Cirrus</h3>        <p class="card-description">Storage that follows the project.</p>      </header>      <div class="card-body">7.4 of 10 GB in use.</div>    </article>  </div></template>

#控件

按钮和圆点均由伪元素生成,无需在标记中添加对应元素:

  • ::scroll-button(inline-start) 和 ::scroll-button(inline-end) 每次将轨道滚动一页。它们和其他按钮一样,默认凸起,指针悬停时抬高,按下时下沉。滚动到任一端时,浏览器会禁用对应的按钮,样式表会将其淡出并隐藏,露出下方的幻灯片。
  • 每张幻灯片上的 ::scroll-marker 生成一个圆点,::scroll-marker-group 将这些圆点集中在轨道下方。当前可见幻灯片的圆点匹配 :target-current,会拉长并使用墨色。每个圆点都通过内边距扩大点击区域,在触屏上还会进一步扩大,无需用手指精确点中 6 像素宽的圆点。

这些控件通过锚点定位放在各自的轮播上。锚点名称限定在各自的作用域内,因此同一页面上的多个轮播各用各的控件。浏览器不支持这些伪元素时,轮播只会少了这些控件,仍可滑动和滚动。此时会显示一条细滚动条,让无法滑动的鼠标也能左右移动。浏览器绘制按钮和圆点时,以及在 carousel-view 中,滚动条会隐藏。

#同时显示多张

carousel-2 和 carousel-3 分别同时显示两张和三张幻灯片。从轨道宽度中扣除可见幻灯片之间的间距,再除以同时显示的张数,得到每张幻灯片的宽度,使最后一张可见幻灯片与轨道边缘对齐。

同时显示多张

<div class="carousel carousel-3" role="region" aria-label="Team" tabindex="0">  <div class="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">    <span class="avatar avatar-lg"><img class="avatar-image" src="/demo/portrait-1.svg" alt></span>    <span class="text-sm fg-text-strong">Ada</span>  </div>  <div class="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">    <span class="avatar avatar-lg"><img class="avatar-image" src="/demo/portrait-2.svg" alt></span>    <span class="text-sm fg-text-strong">Alan</span>  </div>  <div class="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">    <span class="avatar avatar-lg"><img class="avatar-image" src="/demo/portrait-3.svg" alt></span>    <span class="text-sm fg-text-strong">Katherine</span>  </div>  <div class="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">    <span class="avatar avatar-lg" role="img" aria-label="Grace">GH</span>    <span class="text-sm fg-text-strong">Grace</span>  </div>  <div class="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">    <span class="avatar avatar-lg" role="img" aria-label="Margaret">MH</span>    <span class="text-sm fg-text-strong">Margaret</span>  </div></div>
<div className="carousel carousel-3" role="region" aria-label="Team" tabIndex={0}>  <div className="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">    <span className="avatar avatar-lg">      <img className="avatar-image" src="/demo/portrait-1.svg" alt="" />    </span>    <span className="text-sm fg-text-strong">Ada</span>  </div>  <div className="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">    <span className="avatar avatar-lg">      <img className="avatar-image" src="/demo/portrait-2.svg" alt="" />    </span>    <span className="text-sm fg-text-strong">Alan</span>  </div>  <div className="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">    <span className="avatar avatar-lg">      <img className="avatar-image" src="/demo/portrait-3.svg" alt="" />    </span>    <span className="text-sm fg-text-strong">Katherine</span>  </div>  <div className="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">    <span className="avatar avatar-lg" role="img" aria-label="Grace">GH</span>    <span className="text-sm fg-text-strong">Grace</span>  </div>  <div className="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">    <span className="avatar avatar-lg" role="img" aria-label="Margaret">MH</span>    <span className="text-sm fg-text-strong">Margaret</span>  </div></div>
<div class="carousel carousel-3" role="region" aria-label="Team" tabindex="0">  <div class="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">    <span class="avatar avatar-lg"><img class="avatar-image" src="/demo/portrait-1.svg" alt></span>    <span class="text-sm fg-text-strong">Ada</span>  </div>  <div class="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">    <span class="avatar avatar-lg"><img class="avatar-image" src="/demo/portrait-2.svg" alt></span>    <span class="text-sm fg-text-strong">Alan</span>  </div>  <div class="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">    <span class="avatar avatar-lg"><img class="avatar-image" src="/demo/portrait-3.svg" alt></span>    <span class="text-sm fg-text-strong">Katherine</span>  </div>  <div class="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">    <span class="avatar avatar-lg" role="img" aria-label="Grace">GH</span>    <span class="text-sm fg-text-strong">Grace</span>  </div>  <div class="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">    <span class="avatar avatar-lg" role="img" aria-label="Margaret">MH</span>    <span class="text-sm fg-text-strong">Margaret</span>  </div></div>
<template>  <div class="carousel carousel-3" role="region" aria-label="Team" tabindex="0">    <div class="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">      <span class="avatar avatar-lg">        <img class="avatar-image" src="/demo/portrait-1.svg" alt>      </span>      <span class="text-sm fg-text-strong">Ada</span>    </div>    <div class="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">      <span class="avatar avatar-lg">        <img class="avatar-image" src="/demo/portrait-2.svg" alt>      </span>      <span class="text-sm fg-text-strong">Alan</span>    </div>    <div class="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">      <span class="avatar avatar-lg">        <img class="avatar-image" src="/demo/portrait-3.svg" alt>      </span>      <span class="text-sm fg-text-strong">Katherine</span>    </div>    <div class="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">      <span class="avatar avatar-lg" role="img" aria-label="Grace">GH</span>      <span class="text-sm fg-text-strong">Grace</span>    </div>    <div class="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">      <span class="avatar avatar-lg" role="img" aria-label="Margaret">MH</span>      <span class="text-sm fg-text-strong">Margaret</span>    </div>  </div></template>

#露出下一张的边缘

将 --carousel-slide 设为小于轨道宽度的值,下一张幻灯片就会露出边缘。用户在看到圆点之前,就能从露出的边缘看出后面还有内容。浏览器不显示圆点时,这是唯一的提示。图片使用 图文块,轨道上的 --figure-ratio 在图片加载前就为每张图预留相同形状的空间,避免整排内容跳动。

露出下一张的边缘

<div class="carousel" style="--carousel-slide: 84%; --figure-ratio: 16 / 10" role="region" aria-label="Field notes" tabindex="0">  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/harbor.svg" alt="A wooden jetty running out over calm water">    <figcaption class="figure-caption">The jetty at high water</figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/tide.svg" alt="A pale sun low over flat water">    <figcaption class="figure-caption">Low tide at seven</figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/moonstone.svg" alt="A full moon above two ridges">    <figcaption class="figure-caption">Moonrise over the ridges</figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/pines.svg" alt="A line of pines in mist">    <figcaption class="figure-caption">Pines in the morning fog</figcaption>  </figure></div>
<div className="carousel" style={{ '--carousel-slide': '84%', '--figure-ratio': '16 / 10' } as React.CSSProperties} role="region" aria-label="Field notes" tabIndex={0}>  <figure className="carousel-slide figure">    <img className="figure-media" src="/demo/harbor.svg" alt="A wooden jetty running out over calm water" />    <figcaption className="figure-caption">The jetty at high water</figcaption>  </figure>  <figure className="carousel-slide figure">    <img className="figure-media" src="/demo/tide.svg" alt="A pale sun low over flat water" />    <figcaption className="figure-caption">Low tide at seven</figcaption>  </figure>  <figure className="carousel-slide figure">    <img className="figure-media" src="/demo/moonstone.svg" alt="A full moon above two ridges" />    <figcaption className="figure-caption">Moonrise over the ridges</figcaption>  </figure>  <figure className="carousel-slide figure">    <img className="figure-media" src="/demo/pines.svg" alt="A line of pines in mist" />    <figcaption className="figure-caption">Pines in the morning fog</figcaption>  </figure></div>
<div class="carousel" style="--carousel-slide: 84%; --figure-ratio: 16 / 10" role="region" aria-label="Field notes" tabindex="0">  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/harbor.svg" alt="A wooden jetty running out over calm water">    <figcaption class="figure-caption">The jetty at high water</figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/tide.svg" alt="A pale sun low over flat water">    <figcaption class="figure-caption">Low tide at seven</figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/moonstone.svg" alt="A full moon above two ridges">    <figcaption class="figure-caption">Moonrise over the ridges</figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/pines.svg" alt="A line of pines in mist">    <figcaption class="figure-caption">Pines in the morning fog</figcaption>  </figure></div>
<template>  <div class="carousel" style="--carousel-slide: 84%; --figure-ratio: 16 / 10" role="region" aria-label="Field notes" tabindex="0">    <figure class="carousel-slide figure">      <img class="figure-media" src="/demo/harbor.svg" alt="A wooden jetty running out over calm water">      <figcaption class="figure-caption">The jetty at high water</figcaption>    </figure>    <figure class="carousel-slide figure">      <img class="figure-media" src="/demo/tide.svg" alt="A pale sun low over flat water">      <figcaption class="figure-caption">Low tide at seven</figcaption>    </figure>    <figure class="carousel-slide figure">      <img class="figure-media" src="/demo/moonstone.svg" alt="A full moon above two ridges">      <figcaption class="figure-caption">Moonrise over the ridges</figcaption>    </figure>    <figure class="carousel-slide figure">      <img class="figure-media" src="/demo/pines.svg" alt="A line of pines in mist">      <figcaption class="figure-caption">Pines in the morning fog</figcaption>    </figure>  </div></template>

#居中显示

carousel-center 将当前幻灯片放在中间,两侧各露出相邻幻灯片的边缘。轨道在第一张之前和最后一张之后留出空间,让首尾两张也能居中。每张幻灯片占轨道宽度的 80%,可设置 --carousel-slide 调整相邻幻灯片露出的多少。两端各用一个空的 Flex 项留出空间,无需设置内边距,因此幻灯片的宽度仍按整个轨道的宽度计算。

居中显示

<div class="carousel carousel-center" style="--figure-ratio: 16 / 10" role="region" aria-label="Material studies" tabindex="0">  <figure class="carousel-slide figure">    <img class="figure-media" src="/home/slides/pearl.svg" alt="A pearl on a stone plinth, in soft morning light">    <figcaption class="figure-caption">Pearl, in morning light</figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/home/slides/obsidian.svg" alt="A polished obsidian sphere on a dark glossy floor, a softbox reflected in it">    <figcaption class="figure-caption">Obsidian, under a softbox</figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/home/slides/dune.svg" alt="Dunes at first light, their ridges catching a low sun">    <figcaption class="figure-caption">Dunes, at first light</figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/home/slides/tide.svg" alt="A still sea at dusk, the moon low over the horizon and its light broken on the water">    <figcaption class="figure-caption">Tide, at dusk</figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/home/slides/arches.svg" alt="A plaster hall with three arched openings, low sun laying their shapes across the floor">    <figcaption class="figure-caption">Arches, in low sun</figcaption>  </figure></div>
<div className="carousel carousel-center" style={{ '--figure-ratio': '16 / 10' } as React.CSSProperties} role="region" aria-label="Material studies" tabIndex={0}>  <figure className="carousel-slide figure">    <img className="figure-media" src="/home/slides/pearl.svg" alt="A pearl on a stone plinth, in soft morning light" />    <figcaption className="figure-caption">Pearl, in morning light</figcaption>  </figure>  <figure className="carousel-slide figure">    <img className="figure-media" src="/home/slides/obsidian.svg" alt="A polished obsidian sphere on a dark glossy floor, a softbox reflected in it" />    <figcaption className="figure-caption">Obsidian, under a softbox</figcaption>  </figure>  <figure className="carousel-slide figure">    <img className="figure-media" src="/home/slides/dune.svg" alt="Dunes at first light, their ridges catching a low sun" />    <figcaption className="figure-caption">Dunes, at first light</figcaption>  </figure>  <figure className="carousel-slide figure">    <img className="figure-media" src="/home/slides/tide.svg" alt="A still sea at dusk, the moon low over the horizon and its light broken on the water" />    <figcaption className="figure-caption">Tide, at dusk</figcaption>  </figure>  <figure className="carousel-slide figure">    <img className="figure-media" src="/home/slides/arches.svg" alt="A plaster hall with three arched openings, low sun laying their shapes across the floor" />    <figcaption className="figure-caption">Arches, in low sun</figcaption>  </figure></div>
<div class="carousel carousel-center" style="--figure-ratio: 16 / 10" role="region" aria-label="Material studies" tabindex="0">  <figure class="carousel-slide figure">    <img class="figure-media" src="/home/slides/pearl.svg" alt="A pearl on a stone plinth, in soft morning light">    <figcaption class="figure-caption">Pearl, in morning light</figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/home/slides/obsidian.svg" alt="A polished obsidian sphere on a dark glossy floor, a softbox reflected in it">    <figcaption class="figure-caption">Obsidian, under a softbox</figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/home/slides/dune.svg" alt="Dunes at first light, their ridges catching a low sun">    <figcaption class="figure-caption">Dunes, at first light</figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/home/slides/tide.svg" alt="A still sea at dusk, the moon low over the horizon and its light broken on the water">    <figcaption class="figure-caption">Tide, at dusk</figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/home/slides/arches.svg" alt="A plaster hall with three arched openings, low sun laying their shapes across the floor">    <figcaption class="figure-caption">Arches, in low sun</figcaption>  </figure></div>
<template>  <div class="carousel carousel-center" style="--figure-ratio: 16 / 10" role="region" aria-label="Material studies" tabindex="0">    <figure class="carousel-slide figure">      <img class="figure-media" src="/home/slides/pearl.svg" alt="A pearl on a stone plinth, in soft morning light">      <figcaption class="figure-caption">Pearl, in morning light</figcaption>    </figure>    <figure class="carousel-slide figure">      <img class="figure-media" src="/home/slides/obsidian.svg" alt="A polished obsidian sphere on a dark glossy floor, a softbox reflected in it">      <figcaption class="figure-caption">Obsidian, under a softbox</figcaption>    </figure>    <figure class="carousel-slide figure">      <img class="figure-media" src="/home/slides/dune.svg" alt="Dunes at first light, their ridges catching a low sun">      <figcaption class="figure-caption">Dunes, at first light</figcaption>    </figure>    <figure class="carousel-slide figure">      <img class="figure-media" src="/home/slides/tide.svg" alt="A still sea at dusk, the moon low over the horizon and its light broken on the water">      <figcaption class="figure-caption">Tide, at dusk</figcaption>    </figure>    <figure class="carousel-slide figure">      <img class="figure-media" src="/home/slides/arches.svg" alt="A plaster hall with three arched openings, low sun laying their shapes across the floor">      <figcaption class="figure-caption">Arches, in low sun</figcaption>    </figure>  </div></template>

#等宽横排

每张幻灯片采用相同的固定宽度,轨道能放下几张就完整显示几张,下一张会在边缘处截断。采用就近吸附时,快速滑动可以越过多张幻灯片,并在松手后停在滑动终点附近,靠近最近的一张,无需逐张停靠。

等宽横排

<div class="carousel (scroll-snap-type:x|proximity)" style="--carousel-slide: 8.5rem; --carousel-gap: .75rem; --figure-ratio: 4 / 5" role="region" aria-label="Prints" tabindex="0">  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/cairn.svg" alt="Five stones balanced on a beach">    <figcaption class="figure-caption">      <span class="block fg-text-strong">Cairn</span>$48    </figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/dunes.svg" alt="Dunes in the afternoon, their lee sides in shadow">    <figcaption class="figure-caption">      <span class="block fg-text-strong">Dunes</span>$56    </figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/lighthouse.svg" alt="A lighthouse on a dark headland, its beam over the sea">    <figcaption class="figure-caption">      <span class="block fg-text-strong">Headland</span>$64    </figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/pines.svg" alt="Rows of pines fading into fog">    <figcaption class="figure-caption">      <span class="block fg-text-strong">Pines</span>$48    </figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/steps.svg" alt="Concrete steps half in shadow">    <figcaption class="figure-caption">      <span class="block fg-text-strong">Steps</span>$40    </figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/harbor.svg" alt="Jetty posts standing in the water">    <figcaption class="figure-caption">      <span class="block fg-text-strong">Jetty</span>$56    </figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/tide.svg" alt="The sea at slack tide">    <figcaption class="figure-caption">      <span class="block fg-text-strong">Slack tide</span>$48    </figcaption>  </figure></div>
<div className="carousel (scroll-snap-type:x|proximity)" style={{ '--carousel-slide': '8.5rem', '--carousel-gap': '.75rem', '--figure-ratio': '4 / 5' } as React.CSSProperties} role="region" aria-label="Prints" tabIndex={0}>  <figure className="carousel-slide figure">    <img className="figure-media" src="/demo/cairn.svg" alt="Five stones balanced on a beach" />    <figcaption className="figure-caption">      <span className="block fg-text-strong">Cairn</span>$48    </figcaption>  </figure>  <figure className="carousel-slide figure">    <img className="figure-media" src="/demo/dunes.svg" alt="Dunes in the afternoon, their lee sides in shadow" />    <figcaption className="figure-caption">      <span className="block fg-text-strong">Dunes</span>$56    </figcaption>  </figure>  <figure className="carousel-slide figure">    <img className="figure-media" src="/demo/lighthouse.svg" alt="A lighthouse on a dark headland, its beam over the sea" />    <figcaption className="figure-caption">      <span className="block fg-text-strong">Headland</span>$64    </figcaption>  </figure>  <figure className="carousel-slide figure">    <img className="figure-media" src="/demo/pines.svg" alt="Rows of pines fading into fog" />    <figcaption className="figure-caption">      <span className="block fg-text-strong">Pines</span>$48    </figcaption>  </figure>  <figure className="carousel-slide figure">    <img className="figure-media" src="/demo/steps.svg" alt="Concrete steps half in shadow" />    <figcaption className="figure-caption">      <span className="block fg-text-strong">Steps</span>$40    </figcaption>  </figure>  <figure className="carousel-slide figure">    <img className="figure-media" src="/demo/harbor.svg" alt="Jetty posts standing in the water" />    <figcaption className="figure-caption">      <span className="block fg-text-strong">Jetty</span>$56    </figcaption>  </figure>  <figure className="carousel-slide figure">    <img className="figure-media" src="/demo/tide.svg" alt="The sea at slack tide" />    <figcaption className="figure-caption">      <span className="block fg-text-strong">Slack tide</span>$48    </figcaption>  </figure></div>
<div class="carousel (scroll-snap-type:x|proximity)" style="--carousel-slide: 8.5rem; --carousel-gap: .75rem; --figure-ratio: 4 / 5" role="region" aria-label="Prints" tabindex="0">  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/cairn.svg" alt="Five stones balanced on a beach">    <figcaption class="figure-caption">      <span class="block fg-text-strong">Cairn</span>$48    </figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/dunes.svg" alt="Dunes in the afternoon, their lee sides in shadow">    <figcaption class="figure-caption">      <span class="block fg-text-strong">Dunes</span>$56    </figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/lighthouse.svg" alt="A lighthouse on a dark headland, its beam over the sea">    <figcaption class="figure-caption">      <span class="block fg-text-strong">Headland</span>$64    </figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/pines.svg" alt="Rows of pines fading into fog">    <figcaption class="figure-caption">      <span class="block fg-text-strong">Pines</span>$48    </figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/steps.svg" alt="Concrete steps half in shadow">    <figcaption class="figure-caption">      <span class="block fg-text-strong">Steps</span>$40    </figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/harbor.svg" alt="Jetty posts standing in the water">    <figcaption class="figure-caption">      <span class="block fg-text-strong">Jetty</span>$56    </figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/tide.svg" alt="The sea at slack tide">    <figcaption class="figure-caption">      <span class="block fg-text-strong">Slack tide</span>$48    </figcaption>  </figure></div>
<template>  <div class="carousel (scroll-snap-type:x|proximity)" style="--carousel-slide: 8.5rem; --carousel-gap: .75rem; --figure-ratio: 4 / 5" role="region" aria-label="Prints" tabindex="0">    <figure class="carousel-slide figure">      <img class="figure-media" src="/demo/cairn.svg" alt="Five stones balanced on a beach">      <figcaption class="figure-caption">        <span class="block fg-text-strong">Cairn</span>$48      </figcaption>    </figure>    <figure class="carousel-slide figure">      <img class="figure-media" src="/demo/dunes.svg" alt="Dunes in the afternoon, their lee sides in shadow">      <figcaption class="figure-caption">        <span class="block fg-text-strong">Dunes</span>$56      </figcaption>    </figure>    <figure class="carousel-slide figure">      <img class="figure-media" src="/demo/lighthouse.svg" alt="A lighthouse on a dark headland, its beam over the sea">      <figcaption class="figure-caption">        <span class="block fg-text-strong">Headland</span>$64      </figcaption>    </figure>    <figure class="carousel-slide figure">      <img class="figure-media" src="/demo/pines.svg" alt="Rows of pines fading into fog">      <figcaption class="figure-caption">        <span class="block fg-text-strong">Pines</span>$48      </figcaption>    </figure>    <figure class="carousel-slide figure">      <img class="figure-media" src="/demo/steps.svg" alt="Concrete steps half in shadow">      <figcaption class="figure-caption">        <span class="block fg-text-strong">Steps</span>$40      </figcaption>    </figure>    <figure class="carousel-slide figure">      <img class="figure-media" src="/demo/harbor.svg" alt="Jetty posts standing in the water">      <figcaption class="figure-caption">        <span class="block fg-text-strong">Jetty</span>$56      </figcaption>    </figure>    <figure class="carousel-slide figure">      <img class="figure-media" src="/demo/tide.svg" alt="The sea at slack tide">      <figcaption class="figure-caption">        <span class="block fg-text-strong">Slack tide</span>$48      </figcaption>    </figure>  </div></template>

#按可用宽度显示

显示张数可由轮播获得的空间决定,无需依据窗口宽度。将父元素设为查询容器,再按各个宽度设置 --carousel-slide。计算方式与 carousel-2 和 carousel-3 相同,从轨道宽度中扣除可见幻灯片之间的间距,再除以该宽度下同时显示的张数。缩窄预览,即可看到显示张数从三张变为两张,再变为一张。

按可用宽度显示

656 px
<div class="container-inline">  <div class="carousel (--carousel-slide:calc((100%-1rem)/2))@container(--sm) (--carousel-slide:calc((100%-2rem)/3))@container(--xl)" role="region" aria-label="Projects" tabindex="0">    <article class="carousel-slide card card-flat">      <header class="card-header">        <h3 class="card-title">Atlas</h3>        <p class="card-description">A map of every deployment.</p>      </header>    </article>    <article class="carousel-slide card card-flat">      <header class="card-header">        <h3 class="card-title">Borealis</h3>        <p class="card-description">Edge functions near the reader.</p>      </header>    </article>    <article class="carousel-slide card card-flat">      <header class="card-header">        <h3 class="card-title">Cirrus</h3>        <p class="card-description">Storage that follows the project.</p>      </header>    </article>    <article class="carousel-slide card card-flat">      <header class="card-header">        <h3 class="card-title">Delta</h3>        <p class="card-description">Previews for every branch.</p>      </header>    </article>    <article class="carousel-slide card card-flat">      <header class="card-header">        <h3 class="card-title">Ember</h3>        <p class="card-description">Logs that keep a week.</p>      </header>    </article>  </div></div>
<div className="container-inline">  <div className="carousel (--carousel-slide:calc((100%-1rem)/2))@container(--sm) (--carousel-slide:calc((100%-2rem)/3))@container(--xl)" role="region" aria-label="Projects" tabIndex={0}>    <article className="carousel-slide card card-flat">      <header className="card-header">        <h3 className="card-title">Atlas</h3>        <p className="card-description">A map of every deployment.</p>      </header>    </article>    <article className="carousel-slide card card-flat">      <header className="card-header">        <h3 className="card-title">Borealis</h3>        <p className="card-description">Edge functions near the reader.</p>      </header>    </article>    <article className="carousel-slide card card-flat">      <header className="card-header">        <h3 className="card-title">Cirrus</h3>        <p className="card-description">Storage that follows the project.</p>      </header>    </article>    <article className="carousel-slide card card-flat">      <header className="card-header">        <h3 className="card-title">Delta</h3>        <p className="card-description">Previews for every branch.</p>      </header>    </article>    <article className="carousel-slide card card-flat">      <header className="card-header">        <h3 className="card-title">Ember</h3>        <p className="card-description">Logs that keep a week.</p>      </header>    </article>  </div></div>
<div class="container-inline">  <div class="carousel (--carousel-slide:calc((100%-1rem)/2))@container(--sm) (--carousel-slide:calc((100%-2rem)/3))@container(--xl)" role="region" aria-label="Projects" tabindex="0">    <article class="carousel-slide card card-flat">      <header class="card-header">        <h3 class="card-title">Atlas</h3>        <p class="card-description">A map of every deployment.</p>      </header>    </article>    <article class="carousel-slide card card-flat">      <header class="card-header">        <h3 class="card-title">Borealis</h3>        <p class="card-description">Edge functions near the reader.</p>      </header>    </article>    <article class="carousel-slide card card-flat">      <header class="card-header">        <h3 class="card-title">Cirrus</h3>        <p class="card-description">Storage that follows the project.</p>      </header>    </article>    <article class="carousel-slide card card-flat">      <header class="card-header">        <h3 class="card-title">Delta</h3>        <p class="card-description">Previews for every branch.</p>      </header>    </article>    <article class="carousel-slide card card-flat">      <header class="card-header">        <h3 class="card-title">Ember</h3>        <p class="card-description">Logs that keep a week.</p>      </header>    </article>  </div></div>
<template>  <div class="container-inline">    <div class="carousel (--carousel-slide:calc((100%-1rem)/2))@container(--sm) (--carousel-slide:calc((100%-2rem)/3))@container(--xl)" role="region" aria-label="Projects" tabindex="0">      <article class="carousel-slide card card-flat">        <header class="card-header">          <h3 class="card-title">Atlas</h3>          <p class="card-description">A map of every deployment.</p>        </header>      </article>      <article class="carousel-slide card card-flat">        <header class="card-header">          <h3 class="card-title">Borealis</h3>          <p class="card-description">Edge functions near the reader.</p>        </header>      </article>      <article class="carousel-slide card card-flat">        <header class="card-header">          <h3 class="card-title">Cirrus</h3>          <p class="card-description">Storage that follows the project.</p>        </header>      </article>      <article class="carousel-slide card card-flat">        <header class="card-header">          <h3 class="card-title">Delta</h3>          <p class="card-description">Previews for every branch.</p>        </header>      </article>      <article class="carousel-slide card card-flat">        <header class="card-header">          <h3 class="card-title">Ember</h3>          <p class="card-description">Logs that keep a week.</p>        </header>      </article>    </div>  </div></template>

#纵向

carousel-vertical 把横排变成一列,高度为 --carousel-viewport(默认 20rem),上下滚动并吸附。每张幻灯片占高度的 --carousel-slide。按钮位于上下两条边的中点,圆点沿末端一侧纵向排列。

纵向

<div class="carousel carousel-vertical w-full max-w(360)" style="--carousel-viewport: 15rem; --carousel-slide: 50%" role="region" aria-label="This week" tabindex="0">  <article class="carousel-slide card card-flat">    <header class="card-header">      <h3 class="card-title">Monday</h3>      <p class="card-description">Planning, then the design review.</p>    </header>  </article>  <article class="carousel-slide card card-flat">    <header class="card-header">      <h3 class="card-title">Tuesday</h3>      <p class="card-description">Pairing on the schedule view.</p>    </header>  </article>  <article class="carousel-slide card card-flat">    <header class="card-header">      <h3 class="card-title">Wednesday</h3>      <p class="card-description">Release notes and the changelog.</p>    </header>  </article></div>
<div className="carousel carousel-vertical w-full max-w(360)" style={{ '--carousel-viewport': '15rem', '--carousel-slide': '50%' } as React.CSSProperties} role="region" aria-label="This week" tabIndex={0}>  <article className="carousel-slide card card-flat">    <header className="card-header">      <h3 className="card-title">Monday</h3>      <p className="card-description">Planning, then the design review.</p>    </header>  </article>  <article className="carousel-slide card card-flat">    <header className="card-header">      <h3 className="card-title">Tuesday</h3>      <p className="card-description">Pairing on the schedule view.</p>    </header>  </article>  <article className="carousel-slide card card-flat">    <header className="card-header">      <h3 className="card-title">Wednesday</h3>      <p className="card-description">Release notes and the changelog.</p>    </header>  </article></div>
<div class="carousel carousel-vertical w-full max-w(360)" style="--carousel-viewport: 15rem; --carousel-slide: 50%" role="region" aria-label="This week" tabindex="0">  <article class="carousel-slide card card-flat">    <header class="card-header">      <h3 class="card-title">Monday</h3>      <p class="card-description">Planning, then the design review.</p>    </header>  </article>  <article class="carousel-slide card card-flat">    <header class="card-header">      <h3 class="card-title">Tuesday</h3>      <p class="card-description">Pairing on the schedule view.</p>    </header>  </article>  <article class="carousel-slide card card-flat">    <header class="card-header">      <h3 class="card-title">Wednesday</h3>      <p class="card-description">Release notes and the changelog.</p>    </header>  </article></div>
<template>  <div class="carousel carousel-vertical w-full max-w(360)" style="--carousel-viewport: 15rem; --carousel-slide: 50%" role="region" aria-label="This week" tabindex="0">    <article class="carousel-slide card card-flat">      <header class="card-header">        <h3 class="card-title">Monday</h3>        <p class="card-description">Planning, then the design review.</p>      </header>    </article>    <article class="carousel-slide card card-flat">      <header class="card-header">        <h3 class="card-title">Tuesday</h3>        <p class="card-description">Pairing on the schedule view.</p>      </header>    </article>    <article class="carousel-slide card card-flat">      <header class="card-header">        <h3 class="card-title">Wednesday</h3>        <p class="card-description">Release notes and the changelog.</p>      </header>    </article>  </div></template>

#读取和设置当前幻灯片

如果页面需要知道当前显示的是哪张幻灯片,或者要切换到某一张,就把轨道放进 carousel-view,并自行提供上一张、下一张按钮和一排圆点。在 carousel-view 内部,浏览器生成的控件会让位,因此这个轮播只有一组按钮。这些控件由 carousel 行为脚本驱动:

  • IntersectionObserver 会追踪每张幻灯片的可见比例。滚动在 scrollend 时停止后,可见比例最大的幻灯片成为当前项。不支持此事件的浏览器会在短暂停顿后判定。可见比例相同时,如果当前幻灯片也在其中,就保留选中状态,否则取文档顺序中的第一张。视图可以同时容纳多张幻灯片时,浏览器报告的吸附目标可能是另一张。
  • 上一张、下一张按钮和圆点会请求平台按幻灯片自身的吸附对齐方式将其滚动到视图中;启用减少动态效果时则立即跳转。行为脚本从不直接设置滚动位置。
  • 当前幻灯片的圆点带有 aria-current="true",到达两端时,上一张或下一张按钮会被禁用。视图中可能同时显示多张幻灯片,但当前幻灯片始终只有一张,其余幻灯片也不会被隐藏。
  • 幻灯片以其 data-value 或 id 作为标识,因此在当前幻灯片之前插入新幻灯片时,当前幻灯片保持不变。读者造成的变化会派发 input 和 change 事件;行为脚本返回的函数带有可读写的 value,通过代码设置它不会派发任何事件。

读取和设置当前幻灯片

<div class="carousel-view w-full">  <div class="carousel" role="region" aria-label="Releases" tabindex="0">    <article class="carousel-slide card card-flat" id="release-atlas" data-value="atlas">      <header class="card-header">        <h3 class="card-title">Atlas</h3>        <p class="card-description">A map of every deployment, live.</p>      </header>    </article>    <article class="carousel-slide card card-flat" id="release-borealis" data-value="borealis">      <header class="card-header">        <h3 class="card-title">Borealis</h3>        <p class="card-description">Edge functions close to the reader.</p>      </header>    </article>    <article class="carousel-slide card card-flat" id="release-cirrus" data-value="cirrus">      <header class="card-header">        <h3 class="card-title">Cirrus</h3>        <p class="card-description">Storage that follows the project.</p>      </header>    </article>  </div>  <button type="button" class="btn btn-square btn-sm btn-pill carousel-previous" aria-label="Previous slide">    <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="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" transform="rotate(180 8 8)" />    </svg>  </button>  <button type="button" class="btn btn-square btn-sm btn-pill carousel-next" aria-label="Next slide">    <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="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" />    </svg>  </button>  <div class="carousel-dots">    <button type="button" class="carousel-dot" aria-controls="release-atlas" aria-label="Slide 1 of 3"></button>    <button type="button" class="carousel-dot" aria-controls="release-borealis" aria-label="Slide 2 of 3"></button>    <button type="button" class="carousel-dot" aria-controls="release-cirrus" aria-label="Slide 3 of 3"></button>  </div></div><script type="module">  import { attach } from '@master/ui/behaviors'  attach()</script>
import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'import ChevronLeftIcon from '@master/icons-react/native/16/soft/chevron-left'import ChevronRightIcon from '@master/icons-react/native/16/soft/chevron-right'export function Example() {  const root = useRef<HTMLDivElement>(null)  useEffect(() => attach(root.current!), [])  return (    <div ref={root} className="carousel-view w-full">      <div className="carousel" role="region" aria-label="Releases" tabIndex={0}>        <article className="carousel-slide card card-flat" id="release-atlas" data-value="atlas">          <header className="card-header">            <h3 className="card-title">Atlas</h3>            <p className="card-description">A map of every deployment, live.</p>          </header>        </article>        <article className="carousel-slide card card-flat" id="release-borealis" data-value="borealis">          <header className="card-header">            <h3 className="card-title">Borealis</h3>            <p className="card-description">Edge functions close to the reader.</p>          </header>        </article>        <article className="carousel-slide card card-flat" id="release-cirrus" data-value="cirrus">          <header className="card-header">            <h3 className="card-title">Cirrus</h3>            <p className="card-description">Storage that follows the project.</p>          </header>        </article>      </div>      <button type="button" className="btn btn-square btn-sm btn-pill carousel-previous" aria-label="Previous slide">        <ChevronLeftIcon className="icon" />      </button>      <button type="button" className="btn btn-square btn-sm btn-pill carousel-next" aria-label="Next slide">        <ChevronRightIcon className="icon" />      </button>      <div className="carousel-dots">        <button type="button" className="carousel-dot" aria-controls="release-atlas" aria-label="Slide 1 of 3"></button>        <button type="button" className="carousel-dot" aria-controls="release-borealis" aria-label="Slide 2 of 3"></button>        <button type="button" className="carousel-dot" aria-controls="release-cirrus" aria-label="Slide 3 of 3"></button>      </div>    </div>  )}
<script>  import { attach } from '@master/ui/behaviors'  import ChevronLeftIcon from '@master/icons-svelte/native/16/soft/chevron-left'  import ChevronRightIcon from '@master/icons-svelte/native/16/soft/chevron-right'</script><div class="carousel-view w-full" {@attach attach}>  <div class="carousel" role="region" aria-label="Releases" tabindex="0">    <article class="carousel-slide card card-flat" id="release-atlas" data-value="atlas">      <header class="card-header">        <h3 class="card-title">Atlas</h3>        <p class="card-description">A map of every deployment, live.</p>      </header>    </article>    <article class="carousel-slide card card-flat" id="release-borealis" data-value="borealis">      <header class="card-header">        <h3 class="card-title">Borealis</h3>        <p class="card-description">Edge functions close to the reader.</p>      </header>    </article>    <article class="carousel-slide card card-flat" id="release-cirrus" data-value="cirrus">      <header class="card-header">        <h3 class="card-title">Cirrus</h3>        <p class="card-description">Storage that follows the project.</p>      </header>    </article>  </div>  <button type="button" class="btn btn-square btn-sm btn-pill carousel-previous" aria-label="Previous slide">    <ChevronLeftIcon class="icon" />  </button>  <button type="button" class="btn btn-square btn-sm btn-pill carousel-next" aria-label="Next slide">    <ChevronRightIcon class="icon" />  </button>  <div class="carousel-dots">    <button type="button" class="carousel-dot" aria-controls="release-atlas" aria-label="Slide 1 of 3"></button>    <button type="button" class="carousel-dot" aria-controls="release-borealis" aria-label="Slide 2 of 3"></button>    <button type="button" class="carousel-dot" aria-controls="release-cirrus" aria-label="Slide 3 of 3"></button>  </div></div>
<script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'import ChevronLeftIcon from '@master/icons-vue/native/16/soft/chevron-left'import ChevronRightIcon from '@master/icons-vue/native/16/soft/chevron-right'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template>  <div ref="root" class="carousel-view w-full">    <div class="carousel" role="region" aria-label="Releases" tabindex="0">      <article class="carousel-slide card card-flat" id="release-atlas" data-value="atlas">        <header class="card-header">          <h3 class="card-title">Atlas</h3>          <p class="card-description">A map of every deployment, live.</p>        </header>      </article>      <article class="carousel-slide card card-flat" id="release-borealis" data-value="borealis">        <header class="card-header">          <h3 class="card-title">Borealis</h3>          <p class="card-description">Edge functions close to the reader.</p>        </header>      </article>      <article class="carousel-slide card card-flat" id="release-cirrus" data-value="cirrus">        <header class="card-header">          <h3 class="card-title">Cirrus</h3>          <p class="card-description">Storage that follows the project.</p>        </header>      </article>    </div>    <button type="button" class="btn btn-square btn-sm btn-pill carousel-previous" aria-label="Previous slide">      <ChevronLeftIcon class="icon" />    </button>    <button type="button" class="btn btn-square btn-sm btn-pill carousel-next" aria-label="Next slide">      <ChevronRightIcon class="icon" />    </button>    <div class="carousel-dots">      <button type="button" class="carousel-dot" aria-controls="release-atlas" aria-label="Slide 1 of 3"></button>      <button type="button" class="carousel-dot" aria-controls="release-borealis" aria-label="Slide 2 of 3"></button>      <button type="button" class="carousel-dot" aria-controls="release-cirrus" aria-label="Slide 3 of 3"></button>    </div>  </div></template>
app.js
import { carousel } from '@master/ui/behaviors/carousel.js'const releases = carousel(document.querySelector('.carousel-view'), { value: 'borealis' })releases.value // 'borealis'

#无障碍

  • 为轨道设置 role="region"、无障碍名称和 tabindex="0",使用户可以用方向键滚动轨道。
  • 样式表将生成的按钮分别命名为 "Previous slide" 和 "Next slide"。用 --carousel-previous-label 和 --carousel-next-label 以页面的语言为它们命名:两者都是带引号的字符串,可设在轮播或根元素等祖先元素上。每个圆点使用对应幻灯片内容的名称。
  • 轮播不会自动切换幻灯片。如果内容定时切换,需要由脚本提供暂停控件。
  • 启用减少动态效果后,轨道会直接跳到另一张幻灯片,省去平滑滚动。
  • 在强制颜色模式下,按钮使用系统按钮颜色绘制轮廓,圆点沿用系统颜色,当前可见幻灯片的圆点使用系统高亮色。
  • 在 carousel-view 中,用页面所用的语言为按钮和圆点命名;每个圆点都应说明它跳转到总共几张中的第几张。

#React、Svelte 和 Vue

carousel 组件根据数据项渲染幻灯片,每项都有一个在数据项变化时保持不变的 value,并在旁边渲染 carousel-view 的按钮和圆点;组件的值就是当前幻灯片。用 npx master-ui add carousel 将组件复制到项目中。React、Svelte 和 Vue 介绍了这些组件的实现和用法。如果不需要读取当前幻灯片,就不需要这个组件,上面的标记就足够了。

ui/carousel.tsx
'use client'import { useId, useState, type ComponentProps, type ReactNode } from 'react'import { carousel, type CarouselOptions } from '@master/ui/behaviors/carousel.js'import { useBehavior } from './use-behavior'export interface CarouselItem {  /** What the slide stands for: the carousel's value while it is current. Keep it the same when items move. */  value: string}export interface CarouselProps<T extends CarouselItem> extends Omit<ComponentProps<'div'>, 'children' | 'defaultValue' | 'ref'> {  items: T[]  /** The current slide, as its item's value. The first slide by default. */  value?: string  defaultValue?: string  onValueChange?: (value: string) => void  /** What a slide shows. */  children: (item: T) => ReactNode  /** The accessible name of the track. */  label: string  /** A column that scrolls up and down. */  vertical?: boolean  /** Classes of the track, such as `carousel-2` or `carousel-center`. */  trackClassName?: string  /** Classes of each slide. */  slideClassName?: string  previousLabel?: string  nextLabel?: string  /** The accessible name of a dot. `Slide 2 of 5` by default. */  dotLabel?: (index: number, count: number) => string}const chevron = (turn?: number) => (  <svg className="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1" strokeLinecap="round" strokeLinejoin="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={turn ? `rotate(${turn} 8 8)` : undefined} /></svg>)/** * A carousel whose slide in view the application can read and set. React writes the slides from the items * and the controls beside them; the carousel behavior follows the scroll, marks the current dot and moves the * track through the platform when a control is pressed. */export function Carousel<T extends CarouselItem>({ items, value, defaultValue, onValueChange, children, label, vertical, trackClassName, slideClassName, previousLabel = 'Previous slide', nextLabel = 'Next slide', dotLabel = (index, count) => `Slide ${index + 1} of ${count}`, className, ...root }: CarouselProps<T>) {  const id = useId()  // The first value is drawn into the first render; after that the behavior marks what is current.  const [first] = useState(() => value ?? defaultValue ?? items[0]?.value)  // Drawn into the first render only; after that the behavior marks the buttons, and a render with other items  // does not draw the first state over the one it gave.  const [at] = useState(() => items.findIndex(item => item.value === first))  // The behavior's value is null only for a carousel with no slides, which never reports a change.  const ref = useBehavior<HTMLDivElement, CarouselOptions, string | null>(carousel, { value: first }, { value, defaultValue, onValueChange: onValueChange as ((value: string | null) => void) | undefined })  const track = ['carousel', vertical && 'carousel-vertical', trackClassName].filter(Boolean).join(' ')  return (    <div className={className ? `carousel-view ${className}` : 'carousel-view'} ref={ref} {...root}>      <div className={track} role="region" aria-label={label} tabIndex={0}>        {items.map(item => (          <div className={slideClassName ? `carousel-slide ${slideClassName}` : 'carousel-slide'} id={`${id}${item.value}`} data-value={item.value} key={item.value}>{children(item)}</div>        ))}      </div>      <button type="button" className="btn btn-square btn-sm btn-pill carousel-previous" aria-label={previousLabel} disabled={at <= 0}>{chevron(vertical ? -90 : 180)}</button>      <button type="button" className="btn btn-square btn-sm btn-pill carousel-next" aria-label={nextLabel} disabled={at >= items.length - 1}>{chevron(vertical ? 90 : 0)}</button>      <div className="carousel-dots">        {items.map((item, index) => (          <button type="button" className="carousel-dot" aria-controls={`${id}${item.value}`} aria-label={dotLabel(index, items.length)} aria-current={item.value === first ? 'true' : undefined} key={item.value} />        ))}      </div>    </div>  )}
ui/Carousel.svelte
<script lang="ts" module>  import type { Snippet } from 'svelte'  import type { HTMLAttributes } from 'svelte/elements'  export interface CarouselItem {    /** What the slide stands for: the carousel's value while it is current. Keep it the same when items move. */    value: string  }  export interface CarouselProps<T extends CarouselItem> extends Omit<HTMLAttributes<HTMLDivElement>, 'children'> {    items: T[]    /** The current slide, as its item's value. Bind it, or leave it out and the first slide is current. */    value?: string    onValueChange?: (value: string) => void    /** What a slide shows. */    children: Snippet<[T]>    /** The accessible name of the track. */    label: string    /** A column that scrolls up and down. */    vertical?: boolean    /** Classes of the track, such as `carousel-2` or `carousel-center`. */    trackClass?: string    /** Classes of each slide. */    slideClass?: string    previousLabel?: string    nextLabel?: string    /** The accessible name of a dot. `Slide 2 of 5` by default. */    dotLabel?: (index: number, count: number) => string  }</script><script lang="ts" generics="T extends CarouselItem">  import { untrack } from 'svelte'  import { carousel } from '@master/ui/behaviors/carousel.js'  import { behavior } from './behavior.svelte.js'  let { items, value = $bindable(), onValueChange, children, label, vertical, trackClass, slideClass, previousLabel = 'Previous slide', nextLabel = 'Next slide', dotLabel = (index, count) => `Slide ${index + 1} of ${count}`, class: className, ...root }: CarouselProps<T> = $props()  const id = $props.id()  // The first value is drawn into the first render; after that the behavior marks what is current.  const first = untrack(() => value ?? items[0]?.value)  // Drawn into the first render only; after that the behavior marks the buttons and the dots. The first state is  // spread from a constant, so the template's own updates never draw it over the state the behavior gave.  const atStart = { disabled: untrack(() => items.findIndex(item => item.value === first) <= 0) }  const atEnd = { disabled: untrack(() => items.findIndex(item => item.value === first) >= items.length - 1) }  // A dot written later, from new items, is drawn with the slide current then, which is not read as state.  const current = { value: first }  const attach = behavior(carousel, () => ({ value: first }), {    value: () => value,    // The behavior's value is null only for a carousel with no slides, which never reports a change.    change: (next) => { if (next === null) return; current.value = next; value = next; onValueChange?.(next) },    settled: (held) => { if (held !== null) current.value = held },  })</script><!--  @component  A carousel whose slide in view the application can read and set. Svelte writes the slides from the items  and the controls beside them; the carousel behavior follows the scroll, marks the current dot and moves the  track through the platform when a control is pressed.--><div class={['carousel-view', className]} {@attach attach} {...root}>  <!-- The track is a scroll container: in the tab order, so the arrow keys scroll it. -->  <!-- svelte-ignore a11y_no_noninteractive_tabindex -->  <div class={['carousel', vertical && 'carousel-vertical', trackClass]} role="region" aria-label={label} tabindex="0">    {#each items as item (item.value)}      <div class={['carousel-slide', slideClass]} id="{id}{item.value}" data-value={item.value}>{@render children(item)}</div>    {/each}  </div>  <button type="button" class="btn btn-square btn-sm btn-pill carousel-previous" aria-label={previousLabel} {...atStart}>    <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="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" transform={vertical ? 'rotate(-90 8 8)' : 'rotate(180 8 8)'} /></svg>  </button>  <button type="button" class="btn btn-square btn-sm btn-pill carousel-next" aria-label={nextLabel} {...atEnd}>    <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="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" transform={vertical ? 'rotate(90 8 8)' : undefined} /></svg>  </button>  <div class="carousel-dots">    {#each items as item, index (item.value)}      <button type="button" class="carousel-dot" aria-controls="{id}{item.value}" aria-label={dotLabel(index, items.length)} aria-current={item.value === current.value ? 'true' : undefined}></button>    {/each}  </div></div>
ui/Carousel.vue
<script lang="ts">export interface CarouselItem {  /** What the slide stands for: the carousel's value while it is current. Keep it the same when items move. */  value: string}</script><script setup lang="ts" generic="T extends CarouselItem">import { useId, useTemplateRef } from 'vue'import { carousel } from '@master/ui/behaviors/carousel.js'import { useBehavior } from './use-behavior'defineOptions({ inheritAttrs: false })const { items, label, vertical, trackClass, slideClass, previousLabel = 'Previous slide', nextLabel = 'Next slide', dotLabel = (index: number, count: number) => `Slide ${index + 1} of ${count}` } = defineProps<{  /** The slides. An item's `value` is what the slide stands for; keep it the same when items move. */  items: T[]  /** The accessible name of the track. */  label: string  /** A column that scrolls up and down. */  vertical?: boolean  /** Classes of the track, such as `carousel-2` or `carousel-center`. */  trackClass?: string  /** Classes of each slide. */  slideClass?: string  previousLabel?: string  nextLabel?: string  /** The accessible name of a dot. `Slide 2 of 5` by default. */  dotLabel?: (index: number, count: number) => string}>()/** The current slide, as its item's value: `v-model`. */const model = defineModel<string>()defineSlots<{  /** What a slide shows. */  default: (props: { item: T }) => unknown}>()/* * A carousel whose slide in view the application can read and set. Vue writes the slides from the items and * the controls beside them; the carousel behavior follows the scroll, marks the current dot and moves the * track through the platform when a control is pressed. */const id = useId()// The first value is drawn into the first render; after that the behavior marks what is current.const first = model.value ?? items[0]?.valueconst at = items.findIndex(item => item.value === first)const root = useTemplateRef<HTMLDivElement>('root')useBehavior(root, carousel, () => ({ value: first }), model)</script><template>  <div class="carousel-view" ref="root" v-bind="$attrs">    <div class="carousel" :class="[{ 'carousel-vertical': vertical }, trackClass]" role="region" :aria-label="label" tabindex="0">      <div v-for="item in items" :key="item.value" class="carousel-slide" :class="slideClass" :id="`${id}-${item.value}`" :data-value="item.value"><slot :item="item" /></div>    </div>    <button type="button" class="btn btn-square btn-sm btn-pill carousel-previous" :aria-label="previousLabel" :disabled="at <= 0">      <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="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" :transform="vertical ? 'rotate(-90 8 8)' : 'rotate(180 8 8)'" /></svg>    </button>    <button type="button" class="btn btn-square btn-sm btn-pill carousel-next" :aria-label="nextLabel" :disabled="at >= items.length - 1">      <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="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" :transform="vertical ? 'rotate(90 8 8)' : undefined" /></svg>    </button>    <div class="carousel-dots">      <button v-for="(item, index) in items" :key="item.value" type="button" class="carousel-dot" :aria-controls="`${id}-${item.value}`" :aria-label="dotLabel(index, items.length)" :aria-current="item.value === first ? 'true' : undefined"></button>    </div>  </div></template>

#类名

类名说明
carousel支持滚动吸附的轨道。设置 role="region"、无障碍名称和 tabindex="0",以便用方向键滚动。
carousel-slide轮播项占轨道宽度的 --carousel-slide,默认吸附到轨道起始处,使用 carousel-center 时吸附到轨道中央。
carousel-2同时显示两张幻灯片。与 carousel 一同使用
carousel-3同时显示三张幻灯片。与 carousel 一同使用
carousel-center当前幻灯片居中,两侧露出相邻幻灯片的边缘。首尾两张也能滚动到中央。每张幻灯片默认占轨道的 80%,可用 --carousel-slide 调整。与 carousel 一同使用
carousel-vertical高度为 --carousel-viewport 的纵向列,上下滚动并吸附;每张幻灯片占其高度的 --carousel-slide,按钮位于顶部和底部,圆点位于末端一侧与 carousel 一同使用
carousel-view容纳一个轮播,以及页面自有的上一张、下一张按钮和圆点,由 carousel 行为脚本驱动;其中自动生成的控件会让位
carousel-previous转到当前幻灯片的上一张;位于轨道起始边上方,纵向视图中位于顶部。在第一张时禁用
carousel-next转到当前幻灯片的下一张;位于轨道末端边上方,纵向视图中位于底部。在最后一张时禁用
carousel-dots轨道下方的一行圆点,纵向轮播时为其旁边的一列
carousel-dot转到 aria-controls 所指的幻灯片;无障碍名称说明是第几张、共几张

#自定义属性

属性默认值说明
--carousel-gap1rem幻灯片间距
--carousel-slide100%幻灯片的宽度,在 carousel-center 中为 80%。可设置任意宽度,让相邻幻灯片露出一部分
--carousel-viewport20rem纵向轮播的高度在 carousel-vertical 上设置
--carousel-control2rem浏览器绘制的上一项和下一项按钮的大小
--carousel-previous-label浏览器绘制的“上一张”按钮名称,以带引号的字符串指定;未设置时为 'Previous slide'。请在轮播或其祖先元素上用页面语言设置。
--carousel-next-label浏览器绘制的“下一张”按钮名称,以带引号的字符串指定;未设置时为 'Next slide'。

#源码

@master/ui/components/carousel.css
/* * Carousel: a row of slides that scrolls and snaps, built from CSS alone. * The track is a scroll container with scroll snapping, so touch, trackpad, keys and assistive technology * all work as scrolling does. Where the browser supports them, ::scroll-button() adds previous and next * buttons and ::scroll-marker adds a dot per slide that follows the scroll position; both are real, * focusable controls the browser generates, named by --carousel-previous-label and --carousel-next-label, so a * page names them in its own language. Elsewhere the carousel is a swipeable, scrollable row with a thin scrollbar. * carousel-vertical turns the row into a column --carousel-viewport tall, with its buttons above and below. * * A carousel-view holds a track and controls of the page's own: previous and next buttons and a row of dots, * which the carousel behavior drives, so a page or a framework can read and set the slide in view. Inside a * view the generated controls step aside, so there is never a second set of buttons. */@layer components.ui {  .carousel {    --carousel-gap: 1rem;    --carousel-slide: 100%;    --carousel-control: 2rem;    /* A neutral mid-grey chevron reads on moonstone and obsidian alike. */    --carousel-chevron: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='%23777' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5'/%3E%3C/svg%3E");    position: relative;    display: flex;    gap: var(--carousel-gap);    min-width: 0;    margin: 0;    padding: 0;    overflow: auto hidden;    overscroll-behavior-x: contain;    list-style: none;    scroll-snap-type: x mandatory;    scroll-behavior: smooth;    /*     * Without generated buttons, a thin scrollbar is the way across for a mouse that cannot swipe. Where the browser     * draws the buttons and the dots, or in a view with buttons of the page's own, the scrollbar steps aside.     */    scrollbar-width: thin;    scrollbar-color: var(--color-line-strong) transparent;    .carousel-view & {      scrollbar-width: none;    }    .carousel-view &::-webkit-scrollbar {      display: none;    }    &:focus-visible {      @apply --focus-ring;    }    @media (prefers-reduced-motion: reduce) {      scroll-behavior: auto;    }    /*     * Centered: the slide in view stands in the middle and its neighbors show an edge on either side. Room before     * the first slide and after the last lets both reach the middle too. That room is two empty flex items, not     * padding: a percentage in a slide is read from the track less its padding, so padding would shrink the slide     * it is measured from. A flex item's percentage is read from the whole track, as the slide's is.     */    &.carousel-center {      --carousel-slide: 80%;      &::before, &::after {        content: '';        flex: 0 0 max(0px, calc((100% - var(--carousel-slide)) / 2 - var(--carousel-gap)));      }      .carousel-slide {        scroll-snap-align: center;      }    }    /*     * Vertical: a column of slides --carousel-viewport tall that scrolls and snaps up and down. A slide takes     * --carousel-slide of the height; the buttons stand at the middle of the top and the bottom edges, and the     * dots in a column beside the end edge.     */    &.carousel-vertical {      --carousel-viewport: 20rem;      flex-direction: column;      height: var(--carousel-viewport);      overflow: hidden auto;      overscroll-behavior: auto contain;      scroll-snap-type: y mandatory;      .carousel-slide {        min-height: 0;      }    }    @supports selector(::scroll-marker) {      scrollbar-width: none;      &::-webkit-scrollbar {        display: none;      }      /* Room under the track for the markers, which sit on the carousel's own edge. */      margin-block-end: 1.75rem;      /* The controls anchor to their own carousel: the name is scoped, so several on a page do not share it. */      anchor-name: --carousel;      anchor-scope: --carousel;      scroll-marker-group: after;      /*       * The dots: a row centered under the track. Each dot is padded into a larger target, so the row has no gap of       * its own: the padding on either side of a dot is the space between them, and the dot stays .75rem under the track.       */      &::scroll-marker-group {        position: fixed;        position-anchor: --carousel;        top: calc(anchor(bottom) + .5625rem);        left: anchor(left);        right: anchor(right);        display: flex;        justify-content: center;        /* A finger's dots are padded to 24px: the row moves up by the extra padding, so each dot stays where it was. */        @media (pointer: coarse) {          top: calc(anchor(bottom) + .1875rem);        }      }      /* Previous and next: small raised squares on the track's edges, at its middle. */      &::scroll-button(inline-start), &::scroll-button(inline-end) {        position: fixed;        position-anchor: --carousel;        top: calc(anchor(center) - var(--carousel-control) / 2);        display: grid;        place-items: center;        width: var(--carousel-control);        height: var(--carousel-control);        padding: 0;        border: 0;        border-radius: 999px;        background: var(--carousel-chevron) center / .875rem no-repeat, var(--color-surface-floating);        box-shadow: var(--shadow-raised);        cursor: pointer;        touch-action: manipulation;        -webkit-tap-highlight-color: transparent;        transition: box-shadow var(--duration-quick) var(--easing-glide), opacity var(--duration-quick) var(--easing-glide), visibility var(--duration-quick);      }      &::scroll-button(inline-start) {        content: '' / var(--carousel-previous-label, 'Previous slide');        left: calc(anchor(left) + .5rem);        scale: -1 1;      }      &::scroll-button(inline-end) {        content: '' / var(--carousel-next-label, 'Next slide');        right: calc(anchor(right) + .5rem);      }      /* Where slides run from the right, previous is on the right and both chevrons turn round. */      &:dir(rtl)::scroll-button(inline-start) {        right: calc(anchor(right) + .5rem);        left: auto;        scale: none;      }      &:dir(rtl)::scroll-button(inline-end) {        right: auto;        left: calc(anchor(left) + .5rem);        scale: -1 1;      }      @media (hover: hover) {        &::scroll-button(inline-start):hover, &::scroll-button(inline-end):hover {          box-shadow: var(--shadow-raised-hover);        }      }      /* Pressed, the button sinks into the well, as every raised control does. */      &::scroll-button(inline-start):active:not(:disabled), &::scroll-button(inline-end):active:not(:disabled) {        background: var(--carousel-chevron) center / .875rem no-repeat, var(--color-surface-inset);        box-shadow: var(--shadow-pressed);      }      &::scroll-button(inline-start):focus-visible, &::scroll-button(inline-end):focus-visible {        outline: 2px solid var(--color-focus);        outline-offset: 2px;      }      /* A column: its buttons go up and down at the middle of its top and bottom edges, its dots down its end. */      &.carousel-vertical {        margin-block-end: 0;        margin-inline-end: 1.75rem;        &::scroll-marker-group {          top: anchor(top);          bottom: anchor(bottom);          left: calc(anchor(right) + .5625rem);          right: auto;          flex-direction: column;          justify-content: center;        }        &:dir(rtl)::scroll-marker-group {          left: auto;          right: calc(anchor(left) + .5625rem);        }        /* A finger's dots are padded to 24px: the column moves in by the extra padding, so each dot stays where it was. */        @media (pointer: coarse) {          &::scroll-marker-group {            left: calc(anchor(right) + .1875rem);          }          &:dir(rtl)::scroll-marker-group {            left: auto;            right: calc(anchor(left) + .1875rem);          }        }        &::scroll-button(block-start), &::scroll-button(block-end) {          position: fixed;          position-anchor: --carousel;          left: calc(anchor(center) - var(--carousel-control) / 2);          display: grid;          width: var(--carousel-control);          height: var(--carousel-control);          padding: 0;          border: 0;          border-radius: 999px;          background: var(--carousel-chevron) center / .875rem no-repeat, var(--color-surface-floating);          box-shadow: var(--shadow-raised);          cursor: pointer;          touch-action: manipulation;          -webkit-tap-highlight-color: transparent;          transition: box-shadow var(--duration-quick) var(--easing-glide), opacity var(--duration-quick) var(--easing-glide), visibility var(--duration-quick);        }        &::scroll-button(block-start) {          content: '' / var(--carousel-previous-label, 'Previous slide');          top: calc(anchor(top) + .5rem);          rotate: -90deg;        }        &::scroll-button(block-end) {          content: '' / var(--carousel-next-label, 'Next slide');          top: calc(anchor(bottom) - .5rem - var(--carousel-control));          rotate: 90deg;        }        @media (hover: hover) {          &::scroll-button(block-start):hover, &::scroll-button(block-end):hover {            box-shadow: var(--shadow-raised-hover);          }        }        &::scroll-button(block-start):active:not(:disabled), &::scroll-button(block-end):active:not(:disabled) {          background: var(--carousel-chevron) center / .875rem no-repeat, var(--color-surface-inset);          box-shadow: var(--shadow-pressed);        }        &::scroll-button(block-start):focus-visible, &::scroll-button(block-end):focus-visible {          outline: 2px solid var(--color-focus);          outline-offset: 2px;        }        &::scroll-button(block-start):disabled, &::scroll-button(block-end):disabled,        &::scroll-button(inline-start), &::scroll-button(inline-end) {          opacity: 0;          visibility: hidden;          cursor: default;        }      }      /* In a view the page's own controls drive the track: the generated ones step aside. */      .carousel-view & {        margin: 0;        scroll-marker-group: none;        &::scroll-button(*) {          content: none;        }      }      /*       * At either end there is nowhere to go: the button fades out and then leaves, so it no longer covers what the       * slide under it holds.       */      &::scroll-button(inline-start):disabled, &::scroll-button(inline-end):disabled {        opacity: 0;        visibility: hidden;        cursor: default;      }    }    /* Shadows vanish in forced colors: an outline draws the buttons' edge without changing their size. */    @media (forced-colors: active) {      &::scroll-button(inline-start), &::scroll-button(inline-end), &::scroll-button(block-start), &::scroll-button(block-end) {        outline: 1px solid ButtonText;        outline-offset: -1px;      }    }  }  /*   * A view: the track, with the page's previous and next buttons over its edges and its dots under it. The   * buttons stand in the track's own cell, so they keep to its middle at any height; at either end the behavior   * disables the one that has nowhere to go, and it fades out of the way.   */  .carousel-view {    position: relative;    /* Its buttons stand over the track inside the view, not over a sticky header that scrolls past it. */    isolation: isolate;    display: grid;    grid-template: 'track' minmax(0, auto) 'dots' auto / minmax(0, 1fr);    min-width: 0;    .carousel {      grid-area: track;    }  }  .carousel-previous, .carousel-next {    z-index: 1;    grid-area: track;    align-self: center;    margin-inline: .5rem;    transition-property: box-shadow, translate, color, opacity, visibility;    &:disabled {      opacity: 0;      visibility: hidden;    }  }  .carousel-previous {    justify-self: start;  }  .carousel-next {    justify-self: end;  }  /*   * Back and on point the way the slides run: where they run from the right, previous stands on the right and the   * chevrons turn round. An up or down chevron of a vertical view is the same either way.   */  .carousel-previous, .carousel-next {    &:dir(rtl) {      scale: -1 1;    }  }  /* A vertical view: the buttons at the middle of the top and the bottom, the dots in a column at the end. */  .carousel-view:has(.carousel-vertical) {    grid-template: 'track dots' auto / minmax(0, 1fr) auto;    .carousel-previous, .carousel-next {      justify-self: center;      margin: .5rem 0;    }    .carousel-previous {      align-self: start;    }    .carousel-next {      align-self: end;    }    .carousel-dots {      flex-direction: column;      padding: 0;      padding-inline-start: .5625rem;      @media (pointer: coarse) {        padding-inline-start: .1875rem;      }    }  }  /*   * The dots: one button for each slide, centered under the track, the one in view lengthened and in ink. Each   * dot stands .75rem from the track, as the browser's own do.   */  .carousel-dots {    grid-area: dots;    display: flex;    justify-content: center;    align-items: center;    padding-block-start: .5625rem;    /* A finger's dots carry .375rem more padding: the row gives it back, so the dots stay where they were. */    @media (pointer: coarse) {      padding-block-start: .1875rem;    }  }  .carousel-dot {    display: block;    box-sizing: content-box;    width: .375rem;    height: .375rem;    margin: 0;    padding: .1875rem;    border: 0;    border-radius: 999px;    corner-shape: round;    background: var(--color-line-mark) content-box;    appearance: none;    cursor: pointer;    touch-action: manipulation;    -webkit-tap-highlight-color: transparent;    transition: width var(--duration-base) var(--easing-glide), height var(--duration-base) var(--easing-glide), background-color var(--duration-quick) var(--easing-glide);    /* Packed edge to edge, a dot is its own target: on touch it is padded to 24px, the floor, and the dot stays 6px. */    @media (pointer: coarse) {      padding: .5625rem;    }    @media (hover: hover) {      &:hover {        background-color: var(--color-text-muted);      }    }    &[aria-current="true"] {      width: 1rem;      background-color: var(--color-text-strong);      cursor: default;      .carousel-view:has(.carousel-vertical) & {        width: .375rem;        height: 1rem;      }    }    &:focus-visible {      outline: 2px solid var(--color-focus);      outline-offset: 0;    }    @media (prefers-reduced-motion: reduce) {      transition-property: background-color;    }    /* The dots keep system colors of their own, so their hover and their focus ring are system colors too. */    @media (forced-colors: active) {      forced-color-adjust: none;      background-color: GrayText;      @media (hover: hover) {        &:hover {          background-color: CanvasText;        }      }      &[aria-current="true"] {        background-color: Highlight;      }      &:focus-visible {        outline-color: Highlight;      }    }  }  /* A slide: it snaps to the start of the track and takes --carousel-slide of its width. */  .carousel-slide {    flex: 0 0 var(--carousel-slide);    min-width: 0;    scroll-snap-align: start;    @supports selector(::scroll-marker) {      /*       * A 6px dot painted in the middle of its padding, which is the target and the space beside it. The padding is       * even on every side: the round corner less the padding is what rounds the dot, so it stays a dot and a pill.       */      &::scroll-marker {        content: '';        box-sizing: content-box;        width: .375rem;        height: .375rem;        padding: .1875rem;        border-radius: 999px;        corner-shape: round;        background: var(--color-line-mark) content-box;        cursor: pointer;        -webkit-tap-highlight-color: transparent;        transition: width var(--duration-base) var(--easing-glide), height var(--duration-base) var(--easing-glide), background-color var(--duration-quick) var(--easing-glide);        /* On a touch screen the target grows to 24px, the floor for a target that cannot reach past itself; the dot does not. */        @media (pointer: coarse) {          padding: .5625rem;        }        @media (prefers-reduced-motion: reduce) {          transition-property: background-color;        }      }      @media (hover: hover) {        &::scroll-marker:hover {          background-color: var(--color-text-muted);        }      }      /* The slide in view: the dot lengthens and takes ink. */      &::scroll-marker:target-current {        width: 1rem;        background-color: var(--color-text-strong);        cursor: default;      }      /* In a column of dots the one in view lengthens down the column, as a view's own dot does. */      .carousel-vertical &::scroll-marker:target-current {        width: .375rem;        height: 1rem;      }      &::scroll-marker:focus-visible {        outline: 2px solid var(--color-focus);        outline-offset: 0;      }      /* The dots keep their system colors, and the one in view its highlight, instead of the forced canvas. */      @media (forced-colors: active) {        &::scroll-marker {          forced-color-adjust: none;          background-color: GrayText;        }        @media (hover: hover) {          &::scroll-marker:hover {            background-color: CanvasText;          }        }        &::scroll-marker:target-current {          background-color: Highlight;        }        &::scroll-marker:focus-visible {          outline-color: Highlight;        }      }    }  }  /* Several slides in view: each takes a share of the track, less the gaps between them. */  .carousel-2 {    --carousel-slide: calc((100% - var(--carousel-gap)) / 2);  }  .carousel-3 {    --carousel-slide: calc((100% - var(--carousel-gap) * 2) / 3);  }}