pointer-events - SVG: Scalable Vector Graphics 编辑

The pointer-events attribute is a presentation attribute that allows defining whether or when an element may be the target of a mouse event.

Note: As a presentation attribute pointer-events can be used as a CSS property.

html,body,svg { height:100% }
<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>
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
})

As a presentation attribute, it can be applied to any element but it is mostly relevant only on the following twenty-three elements: <a>, <circle>, <clipPath>, <defs>, <ellipse>, <foreignObject>, <g>, <image>, <line>, <marker>, <mask>, <path>, <pattern>, <polygon>, <polyline>, <rect>, <svg>, <switch>, <symbol>, <text>, <textPath>, <tspan>, <use>

Usage notes

Valuebounding-box | visiblePainted | visibleFill | visibleStroke | visible | painted | fill | stroke | all | none
Default valuevisiblePainted
AnimatableYes

For a detailed explanation of each possible value, have a look at the CSS  pointer-events documentation.

Browser compatibility

BCD tables only load in the browser

Specifications

SpecificationStatusComment
Scalable Vector Graphics (SVG) 2
The definition of 'pointer-events' in that specification.
Candidate Recommendation
Scalable Vector Graphics (SVG) 1.1 (Second Edition)
The definition of 'pointer-events' in that specification.
RecommendationInitial definition

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:63 次

字数:6118

最后编辑:8 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文