Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

javascript - Make overlapping html and svg based elements respond to click event

Is there a way to make both the html based div and the SVG based line in the snippet below respond to javascript?

I know of the pointer-events css property. But it seems to be all or nothing to me - I want both elements to respond to events ...

#main {
  position: relative;
  width: 600px;
}

svg {
  border: 1px solid blue;
  red;
  position: absolute;
  top: 0;
  width: 100%;
  height: 100%;
  stroke: rgb(155, 155, 155);
  stroke-width: 10
}

#box-container {
  display: flex;
}

.box {
  width: 200px;
  background-color: pink;
  height: 100px;
}
<div>
  <p>
    Html based boxes should be clickable
  </p>
  <p>
    SVG based line should be clickable
  </p>
</div>
<div id="main">
  <svg>
    <line onClick="alert('line')" x1="0" y1="0" x2="600" y2="100" />
  </svg>
  <div style="display:flex;">
    <div onClick="alert('one')" class="box">
      One
    </div>
    <div onClick="alert('two')" style="margin-left:auto" class="box">
      Two
    </div>
  </div>
</div>
question from:https://stackoverflow.com/questions/65938030/make-overlapping-html-and-svg-based-elements-respond-to-click-event

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
#main {
  position: relative;
  width: 600px;
}

svg {
  border: 1px solid blue;
  red;
  position: absolute;
  top: 0;
  width: 100%;
  height: 100%;
  stroke: rgb(155, 155, 155);
  stroke-width: 10
}

#box-container {
  display: flex;
}

.box {
  width: 200px;
  background-color: pink;
  height: 100px;
}
<div>
  <p>
    Html based boxes should be clickable
  </p>
  <p>
    SVG based line should be clickable
  </p>
</div>
<div id="main">
  <svg>
    <line style="pointer-events:all;" onClick="alert('line')" x1="0" y1="0" x2="600" y2="100" />
  </svg>
  <div style="display:flex;">
    <div onClick="alert('one')" class="box">
      One
    </div>
    <div onClick="alert('two')" style="margin-left:auto" class="box">
      Two
    </div>
  </div>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...