如何定位一个在另一个元素下面但可见的元素?

huangapple go评论81阅读模式
英文:

How would I target an element that is visible but beneath another element?

问题

I created a circular graphic that is mainly based on pure HTML and CSS. A little JavaScript and JQuery is added for curving text and interaction that is planned for later on.

The problem I have is, that when I click on the upper right element, it is covered in party by the upper left element. So when I check which element is clicked through an alert, I see that for 50% of the upper right element's area, the number of the upper left element is returned.

How would I target precisely the elements that I click on? This is needed for linking to different pages of our web project later on.

I created a JSFiddle to show the problem: https://jsfiddle.net/niklasbuschner/gj67md4u/4/

The code looks like this:

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).ready(function() {
  function textRotation() {
    new CircleType(document.getElementById('demo1')).radius(185);
    new CircleType(document.getElementById('demo2')).radius(185);
    new CircleType(document.getElementById('demo3')).radius(185);
  }
  textRotation();
  $('#demo1').children('div').addClass('pie__segment__path-text__rotation1');
  $('#demo3').children('div').addClass('pie__segment__path-text__rotation3');
  $('.pie__segment').on('click', function() {
    var link_target = $(this).data('href');
    alert('KLICK' + link_target);
  });
})
<!-- language: lang-css -->
html {
  font-family: Arial;
  font-size: 14px;
}

.pie {
  border-radius: 100%;
  height: calc(var(--size, 400) * 1px);
  overflow: hidden;
  position: absolute;
  top: 0;
  left: 0;
  width: calc(var(--size, 400) * 1px);
}

.pie__segment {
  --a: calc(var(--over50, 0) * -100%);
  --b: calc((1 + var(--over50, 0)) * 100%);
  --degrees: calc((var(--offset, 0) / 100) * 360);
  -webkit-clip-path: polygon(var(--a) var(--a), var(--b) var(--a), var(--b) var(--b), var(--a) var(--b));
  clip-path: polygon(var(--a) var(--a), var(--b) var(--a), var(--b) var(--b), var(--a) var(--b));
  height: 100%;
  position: absolute;
  -webkit-transform: translate(0, -50%) rotate(90deg) rotate(calc(var(--degrees) * 1deg));
  transform: translate(0, -50%) rotate(90deg) rotate(calc(var(--degrees) * 1deg));
  -webkit-transform-origin: 50% 100%;
  transform-origin: 50% 100%;
  width: 100%;
  z-index: calc(1 + var(--over50));
  cursor: pointer;
}

.pie__segment:after,
.pie__segment:before {
  background: var(--bg, #e74c3c);
  content: '';
  height: 100%;
  position: absolute;
  width: 100%;
}

.pie__segment:before {
  --degrees: calc((var(--value, 45) / 100) * 360);
  -webkit-transform: translate(0, 100%) rotate(calc(var(--degrees) * 1deg));
  transform: translate(0, 100%) rotate(calc(var(--degrees) * 1deg));
  -webkit-transform-origin: 50% 0%;
  transform-origin: 50% 0%;
}

.pie__segment:after {
  opacity: var(--over50, 0);
}

.pie__segment .path-text {
  position: absolute;
  left: -82px;
  bottom: 122px;
  color: #fff;
  font-weight: 700;
  z-index: 2;
  width: 100%;
  text-align: center;
}

.pie__segment .path-text span div {
  height: 2.5em !important;
}

.pie__segment .path-text span div span:last-child {
  color: rgba(255, 255, 255, 0.75);
}

.pie__segment .path-text.demo1 {
  transform: rotate(-90deg);
}

.pie__segment__path-text__rotation1 {
  transform: rotate(60deg);
}

.pie__segment .path-text.demo2 {
  transform: rotate(-30deg);
}

.pie__segment .path-text.demo3 {
  transform: rotate(30deg);
}

.pie__segment__path-text__rotation3 {
  transform: rotate(-60deg);
}

.pie-body {
  border-radius: 100%;
  height: 300px;
  width: 300px;
  position: absolute;
  top: 50px;
  left: 50px;
  background-color: #73c6be;
  text-align: center;
  overflow: hidden;
}

.pie-body p {
  line-height: 260px;
  font-size: 1.75em;
  font-weight: 700;
  color: #0896A5;
}
<!-- language: lang-html -->
<div class="pie-container" style="position: relative; top: 100px; left: 100px;">
  <div class="pie">
    <div class="pie__segment" data-href="1" style="--offset: 0; --value: 33.33333; --bg: #089baa">
      <div class="path-text demo1">
        <span id="demo1">BEISPIEL EINTRAG +</span>
      </div>
    </div>
    <div class="pie__segment" data-href="2" style="--offset: 33.33333; --value: 33.33333; --bg: #066f7a;">
      <div class="path-text demo2">
        <span id="demo2">NÄCHSTER EINTRAG +</span>
      </div>
    </div>
    <div class="pie__segment" data-href="3" style="--offset: 66.66666; --value: 33.33333; --bg: #044249;">
      <div class="path-text demo3">
        <span id="demo3">WEITERER EINTRAG +</span>
      </div>
    </div>
  </div>
  <div class="pie-body">
    <p>Kernaussage</p>
  </div>
</div>
<!-- end snippet -->

(Note: I've provided the code you shared without additional translation.)

英文:

I created a circular graphic that is mainly based on pure HTML and CSS. A little JavaScript and JQuery is added for curving text and interaction that is planned for later on.

The problem I have is, that when I click on the upper right element, it is covered in party by the upper left element. So when I check which element is clicked through an alert, I see that for 50% of the upper right element's area, the number of the upper left element is returned.

如何定位一个在另一个元素下面但可见的元素?

How would I target precisely the elements that I click on? This is needed for linking to different pages of our web project later on.

I created a JSFiddle to show the problem: https://jsfiddle.net/niklasbuschner/gj67md4u/4/

The code looks like this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(document).ready(function() {
function textRotation() {
new CircleType(document.getElementById(&#39;demo1&#39;)).radius(185);
new CircleType(document.getElementById(&#39;demo2&#39;)).radius(185);
new CircleType(document.getElementById(&#39;demo3&#39;)).radius(185);
}
textRotation();
$(&#39;#demo1&#39;).children(&#39;div&#39;).addClass(&#39;pie__segment__path-text__rotation1&#39;);
$(&#39;#demo3&#39;).children(&#39;div&#39;).addClass(&#39;pie__segment__path-text__rotation3&#39;);
$(&#39;.pie__segment&#39;).on(&#39;click&#39;, function() {
var link_target = $(this).data(&#39;href&#39;);
alert(&#39;KLICK&#39; + link_target);
});
})

<!-- language: lang-css -->

html {
font-family: Arial;
font-size: 14px;
}
.pie {
border-radius: 100%;
height: calc(var(--size, 400) * 1px);
overflow: hidden;
position: absolute;
top: 0;
left: 0;
width: calc(var(--size, 400) * 1px);
}
.pie__segment {
--a: calc(var(--over50, 0) * -100%);
--b: calc((1 + var(--over50, 0)) * 100%);
--degrees: calc((var(--offset, 0) / 100) * 360);
-webkit-clip-path: polygon(var(--a) var(--a), var(--b) var(--a), var(--b) var(--b), var(--a) var(--b));
clip-path: polygon(var(--a) var(--a), var(--b) var(--a), var(--b) var(--b), var(--a) var(--b));
height: 100%;
position: absolute;
-webkit-transform: translate(0, -50%) rotate(90deg) rotate(calc(var(--degrees) * 1deg));
transform: translate(0, -50%) rotate(90deg) rotate(calc(var(--degrees) * 1deg));
-webkit-transform-origin: 50% 100%;
transform-origin: 50% 100%;
width: 100%;
z-index: calc(1 + var(--over50));
cursor: pointer;
}
.pie__segment:after,
.pie__segment:before {
background: var(--bg, #e74c3c);
content: &#39;&#39;;
height: 100%;
position: absolute;
width: 100%;
}
.pie__segment:before {
--degrees: calc((var(--value, 45) / 100) * 360);
-webkit-transform: translate(0, 100%) rotate(calc(var(--degrees) * 1deg));
transform: translate(0, 100%) rotate(calc(var(--degrees) * 1deg));
-webkit-transform-origin: 50% 0%;
transform-origin: 50% 0%;
}
.pie__segment:after {
opacity: var(--over50, 0);
}
.pie__segment .path-text {
position: absolute;
left: -82px;
bottom: 122px;
color: #fff;
font-weight: 700;
z-index: 2;
width: 100%;
text-align: center;
}
.pie__segment .path-text span div {
height: 2.5em !important;
}
.pie__segment .path-text span div span:last-child {
color: rgba(255, 255, 255, 0.75);
}
.pie__segment .path-text.demo1 {
transform: rotate(-90deg);
}
.pie__segment__path-text__rotation1 {
transform: rotate(60deg);
}
.pie__segment .path-text.demo2 {
transform: rotate(-30deg);
}
.pie__segment .path-text.demo3 {
transform: rotate(30deg);
}
.pie__segment__path-text__rotation3 {
transform: rotate(-60deg);
}
.pie-body {
border-radius: 100%;
height: 300px;
width: 300px;
position: absolute;
top: 50px;
left: 50px;
background-color: #73c6be;
text-align: center;
overflow: hidden;
}
.pie-body p {
line-height: 260px;
font-size: 1.75em;
font-weight: 700;
color: #0896A5;
}

<!-- language: lang-html -->

&lt;div class=&quot;pie-container&quot; style=&quot;position: relative; top: 100px; left: 100px;&quot;&gt;
&lt;div class=&quot;pie&quot;&gt;
&lt;div class=&quot;pie__segment&quot; data-href=&quot;1&quot; style=&quot;--offset: 0; --value: 33.33333; --bg: #089baa&quot;&gt;
&lt;div class=&quot;path-text demo1&quot;&gt;
&lt;span id=&quot;demo1&quot;&gt;BEISPIEL EINTRAG +&lt;/span&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;pie__segment&quot; data-href=&quot;2&quot; style=&quot;--offset: 33.33333; --value: 33.33333; --bg: #066f7a;&quot;&gt;
&lt;div class=&quot;path-text demo2&quot;&gt;
&lt;span id=&quot;demo2&quot;&gt;N&#196;CHSTER EINTRAG +&lt;/span&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;pie__segment&quot; data-href=&quot;3&quot; style=&quot;--offset: 66.66666; --value: 33.33333; --bg: #044249;&quot;&gt;
&lt;div class=&quot;path-text demo3&quot;&gt;
&lt;span id=&quot;demo3&quot;&gt;WEITERER EINTRAG +&lt;/span&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;pie-body&quot;&gt;
&lt;p&gt;Kernaussage&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

这是一个关于如何使用 svg 的示例。以下是代码的翻译部分:

function polarToCartesian(centerX, centerY, radius, angleInDegrees) {
  var angleInRadians = (angleInDegrees - 90) * Math.PI / 180.0;

  return {
    x: centerX + (radius * Math.cos(angleInRadians)),
    y: centerY + (radius * Math.sin(angleInRadians))
  };
}

function describeArc(x, y, radius, startAngle, endAngle) {

  var start = polarToCartesian(x, y, radius, endAngle);
  var end = polarToCartesian(x, y, radius, startAngle);

  var largeArcFlag = endAngle - startAngle <= 180 ? "0" : "1";
  var sweepFlag = endAngle > startAngle ? 0 : 1;

  var d = [
    "M", start.x, start.y,
    "A", radius, radius, 0, largeArcFlag, sweepFlag, end.x, end.y
  ].join(" ");

  return d;
}

window.onload = function() {
  let arc1 = document.getElementById("arc1")
  let arc2 = document.getElementById("arc2")
  let arc3 = document.getElementById("arc3")

  arc1.setAttribute("d", describeArc(200, 200, 100, 120, 0));
  arc2.setAttribute("d", describeArc(200, 200, 100, 240, 120));
  arc3.setAttribute("d", describeArc(200, 200, 100, 360, 240));

  let text1 = document.getElementById("text1")
  let text2 = document.getElementById("text2")
  let text3 = document.getElementById("text3")

  let textPath1 = document.getElementById("textPath1")
  textPath1.setAttribute("d", describeArc(200, 200, 95, 120, 0));

  let textPath2 = document.getElementById("textPath2")
  textPath2.setAttribute("d", describeArc(200, 200, 95, 240, 120));

  let textPath3 = document.getElementById("textPath3")
  textPath3.setAttribute("d", describeArc(200, 200, 95, 360, 240));


  [arc1, arc2, arc3, text1, text2, text3].forEach(el => {
    el.addEventListener("click", e => {
      console.log(e.target.getAttribute("link"))
    })
  })
};
* {
  margin: 0;
  padding: 0;
}

html,
body {
  height: 100%;
  width: 100%;
}

body {
  background-color: rgb(36, 41, 46);
  display: flex;
  align-items: center;
  justify-content: center;
}

svg {
  height: 400px;
  width: 400px;
  transform: scale(1);
}

path,
text {
  cursor: pointer;
}

text {
  font-family: arial;
  font-size: 14px;
  fill: #fff;
}
<svg viewBox="0 0 400 400">
  <circle shape-rendering="geometricPrecision" cx="200" cy="200" r="100" fill="#73c6be" stroke="none" />
  <path shape-rendering="geometricPrecision" id="arc1" fill="none" stroke="#089baa" stroke-width="30" link="Link1.html" />
  <path shape-rendering="geometricPrecision" id="arc2" fill="none" stroke="#066f7a" stroke-width="30" link="Link2.html" />
  <path shape-rendering="geometricPrecision" id="arc3" fill="none" stroke="#044249" stroke-width="30" link="Link3.html" />

  <path id="textPath1" fill="none" stroke="none" />
  <path id="textPath2" fill="none" stroke="none" />
  <path id="textPath3" fill="none" stroke="none" />

  <text id="text1">
    <textPath 
      href="#textPath1"
      link="Link1.html"     
      startOffset="15%"
    >BEISPIEL EINTRAG+</textPath>
  </text>
  <text id="text2">
    <textPath 
      href="#textPath2"     
      link="Link2.html"
      startOffset="10%"
    >NACHSTER EINTRAG+</textPath>
  </text>
  <text id="text3">
    <textPath 
      href="#textPath3"
      link="Link3.html"
      startOffset="10%"
    >WEITERER EINTRAG+</textPath>
  </text>
</svg>

这些是您提供的代码的翻译部分。

英文:

Here is an example of how you can use svg

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function polarToCartesian(centerX, centerY, radius, angleInDegrees) {
var angleInRadians = (angleInDegrees - 90) * Math.PI / 180.0;
return {
x: centerX + (radius * Math.cos(angleInRadians)),
y: centerY + (radius * Math.sin(angleInRadians))
};
}
function describeArc(x, y, radius, startAngle, endAngle) {
var start = polarToCartesian(x, y, radius, endAngle);
var end = polarToCartesian(x, y, radius, startAngle);
var largeArcFlag = endAngle - startAngle &lt;= 180 ? &quot;0&quot; : &quot;1&quot;;
var sweepFlag = endAngle &gt; startAngle ? 0 : 1; //sic
var d = [
&quot;M&quot;, start.x, start.y,
&quot;A&quot;, radius, radius, 0, largeArcFlag, sweepFlag, end.x, end.y
].join(&quot; &quot;);
return d;
}
window.onload = function() {
let arc1 = document.getElementById(&quot;arc1&quot;)
let arc2 = document.getElementById(&quot;arc2&quot;)
let arc3 = document.getElementById(&quot;arc3&quot;)
arc1.setAttribute(&quot;d&quot;, describeArc(200, 200, 100, 120, 0));
arc2.setAttribute(&quot;d&quot;, describeArc(200, 200, 100, 240, 120));
arc3.setAttribute(&quot;d&quot;, describeArc(200, 200, 100, 360, 240));
let text1 = document.getElementById(&quot;text1&quot;)
let text2 = document.getElementById(&quot;text2&quot;)
let text3 = document.getElementById(&quot;text3&quot;)
let textPath1 = document.getElementById(&quot;textPath1&quot;)
textPath1.setAttribute(&quot;d&quot;, describeArc(200, 200, 95, 120, 0));
let textPath2 = document.getElementById(&quot;textPath2&quot;)
textPath2.setAttribute(&quot;d&quot;, describeArc(200, 200, 95, 240, 120));
let textPath3 = document.getElementById(&quot;textPath3&quot;)
textPath3.setAttribute(&quot;d&quot;, describeArc(200, 200, 95, 360, 240));
[arc1, arc2, arc3, text1, text2, text3].forEach(el =&gt; {
el.addEventListener(&quot;click&quot;, e =&gt; {
console.log(e.target.getAttribute(&quot;link&quot;))
})
})
};

<!-- language: lang-css -->

* {
margin: 0;
padding: 0;
}
html,
body {
height: 100%;
width: 100%;
}
body {
background-color: rgb(36, 41, 46);
display: flex;
align-items: center;
justify-content: center;
}
svg {
/*outline: 2px solid lightgreen;*/
height: 400px;
width: 400px;
transform: scale(1);
}
path,
text {
cursor: pointer;
}
text {
font-family: arial;
font-size: 14px;
fill: #fff;
}

<!-- language: lang-html -->

&lt;svg viewBox=&quot;0 0 400 400&quot;&gt;
&lt;circle shape-rendering=&quot;geometricPrecision&quot; cx=&quot;200&quot; cy=&quot;200&quot; r=&quot;100&quot; fill=&quot;#73c6be&quot; stroke=&quot;none&quot; /&gt;
&lt;path shape-rendering=&quot;geometricPrecision&quot; id=&quot;arc1&quot; fill=&quot;none&quot; stroke=&quot;#089baa&quot; stroke-width=&quot;30&quot; link=&quot;Link1.html&quot; /&gt;
&lt;path shape-rendering=&quot;geometricPrecision&quot; id=&quot;arc2&quot; fill=&quot;none&quot; stroke=&quot;#066f7a&quot; stroke-width=&quot;30&quot; link=&quot;Link2.html&quot; /&gt;
&lt;path shape-rendering=&quot;geometricPrecision&quot; id=&quot;arc3&quot; fill=&quot;none&quot; stroke=&quot;#044249&quot; stroke-width=&quot;30&quot; link=&quot;Link3.html&quot; /&gt;
&lt;path id=&quot;textPath1&quot; fill=&quot;none&quot; stroke=&quot;none&quot; /&gt;
&lt;path id=&quot;textPath2&quot; fill=&quot;none&quot; stroke=&quot;none&quot; /&gt;
&lt;path id=&quot;textPath3&quot; fill=&quot;none&quot; stroke=&quot;none&quot; /&gt;
&lt;text id=&quot;text1&quot;&gt;
&lt;textPath 
href=&quot;#textPath1&quot;
link=&quot;Link1.html&quot; 	    	
startOffset=&quot;15%&quot;
&gt;BEISPIEL EINTRAG+&lt;/textPath&gt;
&lt;/text&gt;
&lt;text id=&quot;text2&quot;&gt;
&lt;textPath 
href=&quot;#textPath2&quot;   	
link=&quot;Link2.html&quot;
startOffset=&quot;10%&quot;
&gt;NACHSTER EINTRAG+&lt;/textPath&gt;
&lt;/text&gt;
&lt;text id=&quot;text3&quot;&gt;
&lt;textPath 
href=&quot;#textPath3&quot;
link=&quot;Link3.html&quot;
startOffset=&quot;10%&quot;
&gt;WEITERER EINTRAG+&lt;/textPath&gt;
&lt;/text&gt;
&lt;/svg&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月7日 00:28:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615618.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定