網站導覽

v2.1.0
媒體

圖片熱點

在圖片上加上標記,說明各處的內容,例如產品的各個部件或平面圖上的位置。

本頁內容

圖片熱點可用來標示產品的各個零件、平面圖上的位置,或螢幕擷取畫面中值得留意的細節。熱點容器包住圖片,每個標記都是以百分比定位的按鈕。標記沿用按鈕的原生行為,因此開啟彈出面板無須腳本。

圖片熱點

<figure class="figure hotspot w-full max-w(480)">  <img class="figure-media" src="/demo/moonstone.svg" alt="A pale stone hanging over two ranges of grey hills" style="--figure-ratio: 16 / 9">  <button class="hotspot-pin" type="button" style="--x: 72%; --y: 32%" popovertarget="spot-stone" aria-label="The stone">    1  </button>  <div class="popover" id="spot-stone" popover>    <p class="m(0) fg-text-strong font-medium">The stone</p>    <p class="m(0) fg-text-muted">      Moonstone, lit from above and slightly left, as every surface in the theme is.    </p>  </div>  <button class="hotspot-pin" type="button" style="--x: 30%; --y: 62%" popovertarget="spot-hills" aria-label="The far hills">    2  </button>  <div class="popover" id="spot-hills" popover>    <p class="m(0) fg-text-strong font-medium">The far hills</p>    <p class="m(0) fg-text-muted">One step of the same grey lighter than the near ones.</p>  </div></figure>
<figure className="figure hotspot w-full max-w(480)">  <img className="figure-media" src="/demo/moonstone.svg" alt="A pale stone hanging over two ranges of grey hills" style={{ '--figure-ratio': '16 / 9' } as React.CSSProperties} />  <button className="hotspot-pin" type="button" style={{ '--x': '72%', '--y': '32%' } as React.CSSProperties} popoverTarget="spot-stone" aria-label="The stone">    1  </button>  <div className="popover" id="spot-stone" popover="">    <p className="m(0) fg-text-strong font-medium">The stone</p>    <p className="m(0) fg-text-muted">      Moonstone, lit from above and slightly left, as every surface in the theme is.    </p>  </div>  <button className="hotspot-pin" type="button" style={{ '--x': '30%', '--y': '62%' } as React.CSSProperties} popoverTarget="spot-hills" aria-label="The far hills">    2  </button>  <div className="popover" id="spot-hills" popover="">    <p className="m(0) fg-text-strong font-medium">The far hills</p>    <p className="m(0) fg-text-muted">One step of the same grey lighter than the near ones.</p>  </div></figure>
<figure class="figure hotspot w-full max-w(480)">  <img class="figure-media" src="/demo/moonstone.svg" alt="A pale stone hanging over two ranges of grey hills" style="--figure-ratio: 16 / 9">  <button class="hotspot-pin" type="button" style="--x: 72%; --y: 32%" popovertarget="spot-stone" aria-label="The stone">    1  </button>  <div class="popover" id="spot-stone" popover>    <p class="m(0) fg-text-strong font-medium">The stone</p>    <p class="m(0) fg-text-muted">      Moonstone, lit from above and slightly left, as every surface in the theme is.    </p>  </div>  <button class="hotspot-pin" type="button" style="--x: 30%; --y: 62%" popovertarget="spot-hills" aria-label="The far hills">    2  </button>  <div class="popover" id="spot-hills" popover>    <p class="m(0) fg-text-strong font-medium">The far hills</p>    <p class="m(0) fg-text-muted">One step of the same grey lighter than the near ones.</p>  </div></figure>
<template>  <figure class="figure hotspot w-full max-w(480)">    <img class="figure-media" src="/demo/moonstone.svg" alt="A pale stone hanging over two ranges of grey hills" style="--figure-ratio: 16 / 9">    <button class="hotspot-pin" type="button" style="--x: 72%; --y: 32%" popovertarget="spot-stone" aria-label="The stone">      1    </button>    <div class="popover" id="spot-stone" popover>      <p class="m(0) fg-text-strong font-medium">The stone</p>      <p class="m(0) fg-text-muted">        Moonstone, lit from above and slightly left, as every surface in the theme is.      </p>    </div>    <button class="hotspot-pin" type="button" style="--x: 30%; --y: 62%" popovertarget="spot-hills" aria-label="The far hills">      2    </button>    <div class="popover" id="spot-hills" popover>      <p class="m(0) fg-text-strong font-medium">The far hills</p>      <p class="m(0) fg-text-muted">One step of the same grey lighter than the near ones.</p>    </div>  </figure></template>
按一下標記。

#標記定位

hotspot 讓圖片的外層元素與圖片同寬,並成為標記的定位基準。每個 hotspot-pin 在自己的樣式中設定 --x 和 --y,以圖片左上角為原點,分別用圖片寬度和高度的百分比指定位置,讓標記中心對準該點。使用百分比可讓標記在圖片縮放時維持在同一處。

標記使用 <button>,若要導向其他位置則使用連結。使用 popovertarget 可開啟彈出面板。瀏覽器支援錨點定位時,會將彈出面板放在標記旁,否則會放在檢視區中央。使用 interestfor 時,指標停留在標記上或標記取得焦點,就會顯示工具提示。

#標記

標記是直徑 24px 的圓形,以文字色填滿,並帶有白色外框與外側的深色陰影。圖片本身不隨主題變化,因此兩種主題都沿用相同的外框和陰影,讓標記在淺色和深色圖片上都清楚可見。標記可加上編號,對應文字清單中的相同項目,也可留空。

每隔幾秒會有一圈波紋從標記向外擴散,提示使用者可以按下標記。啟用減少動態效果的偏好設定時,波紋會保持靜止。在觸控螢幕上,標記維持原本大小,可點按範圍則擴大至 44px。

無編號標記

<figure class="figure hotspot w-full max-w(360)">  <img class="figure-media" src="/demo/moonstone.svg" alt="A pale stone hanging over two ranges of grey hills" style="--figure-ratio: 16 / 9">  <button class="hotspot-pin" type="button" style="--x: 72%; --y: 32%" popovertarget="plain-stone" aria-label="The stone"></button>  <div class="popover" id="plain-stone" popover>The stone the light theme is named for.</div>  <button class="hotspot-pin" type="button" style="--x: 22%; --y: 78%" popovertarget="plain-hills" aria-label="The near hills"></button>  <div class="popover" id="plain-hills" popover>The near hills, in the ink of the page.</div></figure>
<figure className="figure hotspot w-full max-w(360)">  <img className="figure-media" src="/demo/moonstone.svg" alt="A pale stone hanging over two ranges of grey hills" style={{ '--figure-ratio': '16 / 9' } as React.CSSProperties} />  <button className="hotspot-pin" type="button" style={{ '--x': '72%', '--y': '32%' } as React.CSSProperties} popoverTarget="plain-stone" aria-label="The stone"></button>  <div className="popover" id="plain-stone" popover="">The stone the light theme is named for.</div>  <button className="hotspot-pin" type="button" style={{ '--x': '22%', '--y': '78%' } as React.CSSProperties} popoverTarget="plain-hills" aria-label="The near hills"></button>  <div className="popover" id="plain-hills" popover="">The near hills, in the ink of the page.</div></figure>
<figure class="figure hotspot w-full max-w(360)">  <img class="figure-media" src="/demo/moonstone.svg" alt="A pale stone hanging over two ranges of grey hills" style="--figure-ratio: 16 / 9">  <button class="hotspot-pin" type="button" style="--x: 72%; --y: 32%" popovertarget="plain-stone" aria-label="The stone"></button>  <div class="popover" id="plain-stone" popover>The stone the light theme is named for.</div>  <button class="hotspot-pin" type="button" style="--x: 22%; --y: 78%" popovertarget="plain-hills" aria-label="The near hills"></button>  <div class="popover" id="plain-hills" popover>The near hills, in the ink of the page.</div></figure>
<template>  <figure class="figure hotspot w-full max-w(360)">    <img class="figure-media" src="/demo/moonstone.svg" alt="A pale stone hanging over two ranges of grey hills" style="--figure-ratio: 16 / 9">    <button class="hotspot-pin" type="button" style="--x: 72%; --y: 32%" popovertarget="plain-stone" aria-label="The stone"></button>    <div class="popover" id="plain-stone" popover>The stone the light theme is named for.</div>    <button class="hotspot-pin" type="button" style="--x: 22%; --y: 78%" popovertarget="plain-hills" aria-label="The near hills"></button>    <div class="popover" id="plain-hills" popover>The near hills, in the ink of the page.</div>  </figure></template>

#無障礙

  • 每個標記都需要可存取名稱,說明它指向什麼,例如 aria-label="The arm"。請用具體的名稱,避免只寫「標記 1」或只提供標記內的數字。
  • 按 Tab 會依標記在文件中出現的順序移動焦點,因此請依讀者查看圖片的順序撰寫標記。
  • 圖片的 alt 用來描述整張圖片。請在圖片下方以相同編號列出各標記的說明,讓讀者也能直接從文字取得資訊。
  • 若圖片可能在手機上瀏覽,請讓標記彼此至少相距 44px,避免點按範圍重疊。

#類別

類別說明
hotspot與圖片同寬的外框,圖釘以此外框為定位基準
hotspot-pin標記使用具有無障礙名稱的按鈕,呈現為帶白邊的 24px 前景色實心圓。圓心由 --x 和 --y 定位,圓內可顯示數字。圓環會不時向外擴散,指標停在標記上或標記取得焦點時會暫停。

#自訂屬性

屬性預設值說明
--x圖釘的水平位置,以距圖片左邊緣的百分比表示。設定於 hotspot-pin
--y圖釘的垂直位置,以距圖片上邊緣的百分比表示。設定於 hotspot-pin

#原始碼

@master/ui/components/hotspot.css
/* * Hotspot: points on a picture that say what is there. * The hotspot is the frame around a picture; each pin is a button placed at --x and --y of it, in percent. * A pin opens whatever its button opens: a popover with popovertarget, a tooltip with interestfor. * A pin is a small disc of ink with a white rim and a dark shade outside the rim, so it reads on any * picture in either theme. It may hold a number. A ring leaves it now and then, to say it can be pressed; * with reduced motion the ring rests. * Geometry: a 24px disc, and a 44px target on a touch screen. */@layer components.ui {  .hotspot {    position: relative;    display: block;    width: fit-content;    max-width: 100%;    min-width: 0;    margin: 0;  }  /* One pin: its middle stands on the point. */  .hotspot-pin {    --hotspot-size: 1.5rem;    position: absolute;    top: var(--y, 50%);    left: var(--x, 50%);    display: grid;    place-items: center;    width: var(--hotspot-size);    height: var(--hotspot-size);    margin: 0;    padding: 0;    border: 0;    border-radius: 999px;    corner-shape: round;    background: var(--color-surface-inverse);    color: var(--color-text-inverse);    box-shadow: 0 0 0 1.5px var(--color-on-image), 0 0 0 2px var(--color-on-image-shade), 0 2px 6px 1px var(--color-on-image-shade);    font: var(--font-weight-control) .6875rem/1 var(--font-family-sans);    font-feature-settings: inherit;    font-variant-numeric: tabular-nums;    /* A pin written as a link holds its number without the link's line. */    text-decoration: none;    translate: -50% -50%;    appearance: none;    cursor: pointer;    touch-action: manipulation;    user-select: none;    -webkit-user-select: none;    -webkit-tap-highlight-color: transparent;    transition: scale var(--duration-quick) var(--easing-glide);    /* The ring that leaves the pin. */    &::before {      content: '';      position: absolute;      inset: 0;      border-radius: inherit;      corner-shape: inherit;      box-shadow: 0 0 0 0 color-mix(in oklab, var(--color-on-image) 70%, transparent);      pointer-events: none;      animation: hotspot-ring 2.4s var(--easing-glide) infinite;    }    /* The target a finger needs, around the disc. */    &::after {      content: '';      position: absolute;      inset: 0;      @media (pointer: coarse) {        inset: calc((var(--hotspot-size) - 2.75rem) / 2);      }    }    /*     * Found: the ring that asks to be noticed rests while the pin is under the pointer or has focus. The pin in     * use stands above its neighbors, so its ring and its grown disc are never under another pin.     */    @media (hover: hover) {      &:hover {        z-index: 1;        scale: 1.12;      }      &:hover::before {        animation: none;      }    }    &:focus-visible::before {      animation: none;    }    /* While what it opens, right after it, is showing, the pin has been found: its ring rests and it stands above. */    &:has(+ :is(.popover, .tooltip):popover-open) {      z-index: 1;      &::before {        animation: none;      }    }    &:active {      scale: .94;      transition-duration: var(--duration-press);    }    &:focus-visible {      z-index: 1;      outline: 2px solid var(--color-on-image);      outline-offset: 3px;      box-shadow: 0 0 0 1.5px var(--color-on-image), 0 0 0 3px var(--color-focus), 0 0 0 7px var(--color-on-image-shade);    }    @media (prefers-reduced-motion: reduce) {      transition: none;      &::before {        animation: none;      }      &:hover, &:active {        scale: none;      }    }    @media (forced-colors: active) {      outline: 2px solid ButtonText;      outline-offset: -2px;      background: ButtonFace;      color: ButtonText;      &::before {        animation: none;      }      /* Keyboard focus is a ring of the highlight, as on a button. */      &:focus-visible {        outline-color: Highlight;      }    }  }}@keyframes hotspot-ring {  0%, 20% {    box-shadow: 0 0 0 0 color-mix(in oklab, var(--color-on-image) 70%, transparent);  }  70%, 100% {    box-shadow: 0 0 0 .75rem transparent;  }}