首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >悬停时应用SVG掩码

悬停时应用SVG掩码
EN

Stack Overflow用户
提问于 2022-09-05 16:53:26
回答 1查看 37关注 0票数 0

当光标进入svg圆圈时,我试图应用svg掩码。掩蔽正常工作,但事件不会触发。

如何正确地将事件侦听器设置为svg元素,以便当<svg id="container">中包含的圆圈悬空时,掩码被应用于<rect id="masked">

代码语言:javascript
复制
const svg = document.getElementById('container')

svg.addEventListener('onmouseover', (e) => {
  console.log('hover')

  const masked = document.getElementById('masked')
  masked.setAttribute('fill', 'grey')
  masked.setAttribute('fill-opacity', '70%')
  masked.setAttribute('mask', 'url(#myMask)')
})

svg.addEventListener('onmouseleave', (e) => {
  console.log('leave')

  const masked = document.getElementById('masked')
  masked.setAttribute('fill', 'none')
  masked.setAttribute('fill-opacity', '100%')
  masked.removeAttribute('mask')
})
代码语言:javascript
复制
html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
  background-color: yellow;
}

svg,
circle {
  pointer-events: all;
}
代码语言:javascript
复制
<svg id="mySVG" width="600" height="250">
  <mask id="myMask" x="0" y="0" width="600" height="250">
    <rect width="400" height="250" fill="white" />
    <circle cx="25" cy="25" r="25" fill="black" />
  </mask>
  <rect id="masked" x="0" y="0" width="600" height="250" fill="grey"></rect>
  <!-- The following svg element is supposed to trigger events -->
  <svg id="container">
    <!-- This circle overlaps the circle in <mask> -->
    <circle cx="25" cy="25" r="25" fill="none" stroke="black" />
  </svg>
</svg>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-06 10:41:32

这些事件被称为mouseovermouseleave,而不是onmouseoveronmouseleave

代码语言:javascript
复制
const svg = document.getElementById('container')

svg.addEventListener('mouseover', (e) => {
  console.log('hover')

  const masked = document.getElementById('masked')
  masked.setAttribute('fill', 'grey')
  masked.setAttribute('fill-opacity', '70%')
  masked.setAttribute('mask', 'url(#myMask)')
})

svg.addEventListener('mouseleave', (e) => {
  console.log('leave')

  const masked = document.getElementById('masked')
  masked.setAttribute('fill', 'none')
  masked.setAttribute('fill-opacity', '100%')
  masked.removeAttribute('mask')
})
代码语言:javascript
复制
html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
  background-color: yellow;
}

svg,
circle {
  pointer-events: all;
}
代码语言:javascript
复制
<svg id="mySVG" width="600" height="250">
  <mask id="myMask" x="0" y="0" width="600" height="250">
    <rect width="400" height="250" fill="white" />
    <circle cx="25" cy="25" r="25" fill="black" />
  </mask>
  <rect id="masked" x="0" y="0" width="600" height="250" fill="grey"></rect>
  <!-- The following svg element is supposed to trigger events -->
  <svg id="container">
    <!-- This circle overlaps the circle in <mask> -->
    <circle cx="25" cy="25" r="25" fill="none" stroke="black" />
  </svg>
</svg>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73612550

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档