pointer-events

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

pointer-events 屬性是一個表示屬性,用於定義元素何時可能成為滑鼠事件的目標。

注意: 作為一個表示屬性,pointer-events 還有一個對應的 CSS 屬性:pointer-events。當兩者都指定時,CSS 屬性具有更高的優先順序。

你可以將此屬性與以下 SVG 元素一起使用

示例

html
<svg viewBox="0 0 20 10" xmlns="http://www.w3.org/2000/svg">
  <!--
  The circle will always intercept the mouse event.
  To change the color of the rect underneath you have
  to click outside the circle
  -->
  <rect x="0" y="0" height="10" width="10" fill="black" />
  <circle cx="5" cy="5" r="4" fill="white" pointer-events="visiblePainted" />

  <!--
  The circle below will never catch a mouse event.
  The rect underneath will change color whether you
  are clicking on the circle or the rect itself
  -->
  <rect x="10" y="0" height="10" width="10" fill="black" />
  <circle cx="15" cy="5" r="4" fill="white" pointer-events="none" />
</svg>
js
window.addEventListener("mouseup", (e) => {
  // Let's pick a random color between #000000 and #FFFFFF
  const color = Math.round(Math.random() * 0xffffff);

  // Let's format the color to fit CSS requirements
  const fill = `#${color.toString(16).padStart(6, "0")}`;

  // Let's apply our color in the
  // element we actually clicked on
  e.target.style.fill = fill;
});

用法說明

bounding-box | visiblePainted | visibleFill | visibleStroke | visible | painted | fill | stroke | all | none
預設值 visiblePainted
可動畫的

有關每個可能值的詳細說明,請參閱 CSS pointer-events 文件。

規範

規範
Scalable Vector Graphics (SVG) 2
# PointerEventsProperty

瀏覽器相容性

另見