What is the best way to select an element that is currently visible but hidden underneath another element?

I have developed a circular graphic using primarily HTML and CSS, with some JavaScript and JQuery functionalities incorporated for text curving and planned interactions in the future.

However, I've encountered an issue where clicking on the upper right element results in it being partially covered by the upper left element. This leads to confusion when determining which element was actually clicked, as sometimes the number of the upper left element is returned instead of the intended one.

https://i.sstatic.net/Pd8hz.png

How can I accurately target the specific elements that are clicked on? This precision is crucial for linking to different pages within our web project at a later stage.

To illustrate the problem, I have created a JSFiddle: https://jsfiddle.net/niklasbuschner/gj67md4u/4/

The code snippet is outlined below:

$(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('CLICK' + link_target);
  });
})
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);
}
// Additional CSS styles ...
<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">SAMPLE ENTRY +</span>
      </div>
    </div>
    // Nested pie segments ...
  </div>
  <div class="pie-body">
    <p>Main Point</p>
  </div>
</div>

Answer №1

Here is a demonstration showcasing the utilization of 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; //sic

  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 {
  /*outline: 2px solid lightgreen;*/
  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%"
    >EXAMPLE ENTRY+</textPath>
  </text>
    <text id="text2">
    <textPath 
    href="#textPath2"   
     link="Link2.html"
    startOffset="10%"
    >NEXT ENTRY+</textPath>
</text>
    <text id="text3">
    <textPath 
    href="#textPath3"
    link="Link3.html"
    startOffset="10%"
    >ANOTHER ENTRY+</textPath>
</text>
  </svg>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Immerse yourself in a world of virtual reality with the cutting-edge VR Vide

Currently, I am in the process of developing a virtual panoramic tour that features various panoramas linked together with hotspots. Each panorama contains a video hotspot that plays a video. However, I have encountered an issue where every time a new sce ...

"Enhance Your Phonegap Android App with Pinch Zoom Capability

I am currently working on an Android Application with the help of jQuery Mobile and Phonegap. My goal is to implement pinch zoom functionality on the App pages. I have come across several suggestions that involve using the following line, but unfortunatel ...

Adding HTML content using jQuery's document.ready feature

As a jQuery novice, I am attempting to incorporate a Facebook like button using the jQuery document.ready function. In my external Javascript file (loaded after the jQuery script), you will find the following code snippet: $(document).ready(function(){ ...

What is the best way to prevent jest.mock from being hoisted and only use it in a single jest unit test?

My goal is to create a mock import that will be used only in one specific jest unit test, but I am encountering some challenges. Below is the mock that I want to be restricted to just one test: jest.mock("@components/components-chat-dialog", () ...

Tips for creating a responsive Youtube embedded video

Check out this website for a good example: If you take a look, you'll notice that the header youtube video is responsive - it automatically resizes when the window size changes. Here are the <iframe> codes: <iframe id="bluetube-player-1" fr ...

Consolidate all data connected to the specified key from a JSON dataset

Looking at the json string presented below [{"_id":"9/17/2015","amt1":0,"amt2":13276.5},{"_id":"9/18/2015","amt1":8075,"amt2":6445.5}] The expected outcome is: [{"_id": ["9/17/2015", "9/18/2015"], "amt1": [0, 8075], "amt2": [13276.5, 6445.5]}] Is there ...

The code functions properly on a standalone device, however, it encounters issues when

I am facing an issue with my JavaScript code. It works perfectly fine when I run it on my local machine, but once I upload it to the server, it only functions properly after I refresh the page. Below is the code in question. Please do not hesitate to rea ...

Trouble with rendering inline images from markdown files in GatsbyJS

I've been trying to include inline images in my markdown file with the gatsby-remark-images plugin. However, I'm facing an issue where the image is not loading on my local host. I'm not sure if it's a syntax error or if I'm missing ...

unable to locate public folder in express.js

I recently created a basic server using Node.js with Express, and configured the public folder to serve static files. Within my main index.js file, I included the following code: const express = require('express'); const app = express(); const h ...

Issue: Undefined onClick function for HTML when using Node.js, NPM, and Parcel

I am currently working on a weather application using Node.js, NPM, and Parcel for development. To ensure OpenLayers functions properly, I need to incorporate these technologies, even though I'm not very familiar with them. One key aspect of my proje ...

Tips for automatically closing one element when another is clicked

My goal is to create a menu that displays when I click on a link, and if another menu is already open, it should close before displaying the new one. Essentially, I want to achieve an accordion menu where only one menu is open at a time. However, despite m ...

Transferring information to a view using ExpressJS and Reactjs

I have created an application that requires users to log in with Twitter credentials. Once logged in successfully, I typically pass the user data to the template using the following code: app.get('/', function(req, res, next) { log("The ...

Is there a way to prevent redundancy when exporting functions in Node.js?

Is there a way to avoid repeating module.exports or usuariosControllers when writing a small API in express for fun? module.exports.getUsuarios = (request, response) => {}; module.exports.crearUsuario = (request, response) => {}; module.exports. ...

Ways to verify whether the checkboxes within a gridview have been selected

enter code hereMy Gridview has five checkboxes in each row with options 'NO', 'YES', 'NA', 'NA/U', and 'REPEAT'. I am looking to enable a button if any of the checkboxes among 'NO', 'YES&apos ...

preventing the propagation of another event

Is it possible to listen for a change event on a checkbox without triggering a click event on its container? <label><input type="checkbox"> Hello world</label> I need the action triggered by the checkbox's change event, but I want ...

The two-word string is failing to pass through the query string

I've been working on passing multiple values through the query string. So far, I have successfully passed single-word values to a PHP page using jQuery. However, I encountered an issue when trying to pass a user's name like "Vishal Deb" as it onl ...

What is the optimal way for text selection to operate within a clickable component on a web application?

We're currently developing a web application with expandable/collapsible elements. Here's an example of one of the clickable items: https://i.stack.imgur.com/jdLOu.png The text selection behavior when clicking on the element is causing some an ...

Best event for Angular directive to bind on image onload

I'm currently working on incorporating this particular jQuery plugin into my Ionic application. Although I have a good grasp of creating an Angular directive to encapsulate the plugin, I am encountering difficulties in successfully implementing it. B ...

Is it possible for a while loop to display just one row?

<?php include 'db.php'; $sql_locations = "SELECT * FROM location"; $result = mysqli_query($conn,$sql_locations); $count = mysqli_num_rows($result); $markers = array(); while($row = mysqli_fetch_array ...

Showcasing two sets of data from an array using chart.js within a node.js environment

I am currently working on a project where I need to display two elements from an array - one as the label (e.g. "name of certain type of crop") and the other as the data itself (e.g. "quantity of the crop"). However, I am facing an issue where if the same ...