요소를 제외한 SVG 마우스 이벤트 통과 (SVG Mouse Event Passthrough except for elements)


문제 설명

요소를 제외한 SVG 마우스 이벤트 통과 (SVG Mouse Event Passthrough except for elements)

다른 요소에 대한 오버레이로 투명한 SVG 캔버스가 있습니다. 내가 원하는 것은 마우스 이벤트가 아래 요소로 전달되는 것을 허용하지만 보이는 svg 요소의 이벤트도 작동하도록 하는 것입니다. svg가 이벤트를 캡처하거나 svg 요소의 css 포인터 이벤트(none|auto)를 사용하여 이벤트를 캡처하도록 할 수 있지만 svg의 보이는 요소에서만 이벤트를 캡처하는 방법을 알 수 없습니다.

svg 요소를 동적으로 생성합니다(예:

C=document.createElementNS("http://www.w3.org/2000/svg","circle")
C.setAttributeNS(null,"r",curve_radius)
C.setAttributeNS(null,"cx",x)
C.setAttributeNS(null,"cy",y)
C.setAttributeNS(null,"fill","rgb(0,0,255)")
C.setAttributeNS(null,"stroke","rgb(0,0,255)")     
C.setAttributeNS(null,"stroke‑width","1")

svg.appendChild(C) ;
).

참조 솔루션

방법 1:

You found pointer‑events. I think it is just a matter of tweaking it in the right way. Here I "disable" pointer‑events on the <svg> and at the same time specifying that the stroke of the <circle> should take event.

document.getElementById('bg').addEventListener('click', e => {
  console.log(e.target.nodeName);
});
#bg {
  background‑color: orange;
  position: relative;
  width: 200px;
  height: 200px;
}

svg {
  pointer‑events: none;
  position: absolute;
}

circle {
  pointer‑events: stroke;
}

p {
  position: absolute;
  top: 100px;
  left: 40px;
}
<div id="bg">
  <p>Text</p>
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10" width="200">
    <circle cx="5" cy="5" r="4" stroke="red" fill="none"/>
  </svg>
</div>

(by user18504955chrwahl)

참조 문서

  1. SVG Mouse Event Passthrough except for elements (CC BY‑SA 2.5/3.0/4.0)

#svg #events #onmouseover #javascript #mouseevent






관련 질문

D3/SVG: 범위를 수정하여 시간 척도로 d3.svg.axis의 크기를 조정하는 방법은 무엇입니까? (D3/SVG: How to resize a d3.svg.axis with time scale by modifying range?)

d3 SVG의 드롭다운 메뉴 (Drop down menu over d3 SVG)

단색 배경의 SVG 투명도 (SVG transparency on a solid background)

그룹을 번역할 때 이미지가 포함된 SVG 채우기가 작동하지 않음 (SVG fill with image not working when translate the group)

프로그래밍 방식으로 SVG 파일의 변환을 병합하는 방법은 무엇입니까? (How to flatten transforms in an SVG file programmatically?)

Jquery/Javascript - SVG를 클릭할 때 IE에서 요소 외부 클릭 감지가 실패함 (Jquery/Javascript - Detecting a click outside an element fails on IE when clicking an SVG)

snap.svg에서 그룹의 요소/자식을 반복하는 방법은 무엇입니까? (How to iterate on a group's elements/children in snap.svg?)

svgwrite로 기존 svg를 가져오는 방법 - Python (How to import an existing svg with svgwrite - Python)

SVG로 사실적인(정현파) 플래그 애니메이션/플러터 만들기 (Making realistic (sinusoid) flag animation / flutter with SVG)

javascript/svg에서 호로 양면이 있는 상자 그리기 (Drawing a box with two sides as an Arc in javascript/svg)

요소를 제외한 SVG 마우스 이벤트 통과 (SVG Mouse Event Passthrough except for elements)

단일 svg 요소를 만들기 위해 여러 모양 병합 (Merging number of shapes to make a single svg element)







코멘트