Triggering modal activation upon clicking on the SVG map

Seeking advice on the best way to implement a new feature.

I have an SVG map with tiny blue icons that users can click on. You can refer to the image below for clarity:

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

Whenever a user clicks on any of these blue icons, I want a small modal to pop up showing information about that specific region. Take a look at the screenshot below as an example:

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

For instance, in the screenshot above, clicking on the blue circle at the bottom brings up a modal displaying details about that particular region.

In my svg, I've assigned ids to each blue icon (

<g class="mapLocations__icon" id="uk" ..."
>. However, I'm currently contemplating two options:

  1. Absolutely positioning the modals for each region, but this could be challenging to make responsive.
  2. Repeating markup for the modal within the svg itself.

Both approaches don't seem ideal to me.

Any suggestions on how to proceed?

Here is the link to view my svg code: https://jsfiddle.net/6squyc7d/ (Shared via jsfiddle due to character limit when pasted here)

Answer №1

When the onclick event is triggered, you have the ability to leverage the x and y coordinates in order to dynamically position the modal. This means that you can actually create and place the modal directly within the onclick function.

function openModal( evt ) {
 var intCoordX = evt.clientX;    
 var intCoordY = evt.clientY;
 // Adjust modal properties
 // Display modal
}

Answer №2

Each dot is assigned a unique Id. Using a forEach loop, you can add an onclick listener to each dot by iterating through all the Ids in an array. The code snippet for adding this functionality within the forEach loop is as follows:

const dot = document.getElementById('canada')
dot.addEventListener('click', event => {
  console.log(event.x, event.y)
});

This code snippet retrieves the dot with the Id 'canada'. Upon clicking on the dot, the x and y coordinates of the click event are logged, indicating where the user clicked on their screen.

To display a popup modal at the clicked location, you can utilize an absolute positioned div with specified top and left values:

.position {
  position: absolute;
  opacity: 0;
}

Using JavaScript, you can dynamically position the modal by setting the appropriate top and left values, while changing the opacity to 1 to make it visible at the correct position upon click.

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

Transform your Angular Framework Application into a desktop application with the power of Electron

Project Description: I currently have a fully functional front-end angularjs application that is connected to a back-end Java application deployed on Tomcat, running on a different port (8443). Up until now, I have been accessing the application through a ...

D3 bar chart displaying identical values but with varying data sets

<!DOCTYPE html> <html lang='en'> <head> <meta charset="UTF-8"/> <title>Interactive Bar Chart using D3</title> <script src="https://d3js.org/d3.v4.min.js"></script> </head> <b ...

Utilizing JavaScript to eliminate objects from a collection and showcase a refreshed arrangement

On a simple webpage using bootstrap and js/jquery, data is fetched via ajax to create entries displayed to the viewer. The process involves: - making a request - parsing XML to create an array of objects with all the data - sorting by one property - cr ...

Is there an issue with hover not working for MUI styled component when focused?

Having trouble getting both focus and hover effects to work on the same MUI styled component in React. Check out this code sandbox for a demonstration. When clicking on the Select element, the background and border colors do not change: https://i.sstatic. ...

Loop through each item in order to modify the text within the pop-up using a for loop

Goal Upon clicking a .player, a popup should display with relevant data about that player fetched from players.js Issue: When clicking on a player, the event seems to trigger multiple times instead of just once. This results in retrievi ...

Struggling to eliminate the remaining vertical paddings within the <div> element while utilizing Bootstrap 5

I want to eliminate all padding (top + bottom) for text inside a block using Bootstrap 5. When I use HTML without Bootstrap, setting padding: 0px works as expected: <div style="padding: 0px; background-color: gray;">no bootstrap</div&g ...

How can I retrieve the page title in a React app when using jest and enzyme for testing purposes?

After creating a react app using create-react-app, I found the barebones to be pretty good! Now, I am looking to modify the title of the page in index.html and test it. Although enzyme is useful for testing individual components and producing HTML, I am cu ...

The setState function in React.js fails to properly assign data

Recently, I've been using axios.get() to retrieve data from my database. The response is coming back correctly, but for some reason, when I attempt to update the state with this data, nothing seems to change. import React, { Component, useState, useE ...

pressing the switch will adjust the size of the container

I am looking to implement a feature where clicking on an icon will resize a div to full screen in the browser. Below is the HTML code I have set up for this functionality, and I am open to suggestions on how to achieve this. <div> <a (click)= ...

Struggling to alter Material-UI theme styles

I have been working on a project using Material-UI and I am trying to change the default theme style of textTransform:"uppercase" to textTransform:"capitalize." After checking out the documentation on custom styling, I learned that I should use inline sty ...

The clearInterval function seems to be malfunctioning in this particular piece of code

Can someone help me troubleshoot this code? I can't seem to stop the setInterval function. Issue: changedHandler_BB is not defined. ... var changedHandler_BB = setInterval(function() { $("#wc_room_container_BB").addClass('active_chat_bl ...

Customize the Vuetify 2.0 sass variable $heading-font-family

I'm currently trying to customize the default variable $heading-font-family in Vuetify 2.0.0-beta.0, but I'm encountering issues with this particular override. Oddly enough, I have been successful in overriding other variables such as $body-font- ...

Using React Native to dynamically change color based on API response

I'm currently working on a React Native project and I have a requirement to dynamically change the background color of a styled component based on the value retrieved from an API. However, I'm facing some challenges in implementing this feature. ...

Error message: "Unable to locate module for split-pane-react in Jest"

Attempting to run Jest tests on my component with [email protected] in a Next.js project using typescript. Encountering the following error: Cannot locate module 'split-pane-react' from 'my component path' when running the test ca ...

Avoid using the Router with the Search component in React JS

Having trouble rendering my Search component within the main Header component using react-router-dom. I suspect there's an issue with this line of code <Route render={({ history }) => } /> I've been stuck on this for two days now... T ...

The outcome of Contains function is coming back as negative

Recently, I was working on a project that I cloned from GIT, which involved a bot. However, I encountered an issue where the 'contains' function used by the bot was not functioning properly. After conducting some research using Google, I discove ...

Issue detected in rxjs-compat operator's shareReplay file at line 2, column 10:

I've encountered an issue with the angular material spinner I'm using in my project. The error message is as follows: ERROR in node_modules/rxjs-compat/operator/shareReplay.d.ts(2,10): error TS2305: Module '"D:/ControlCenter/ofservices ...

Adjusting image size to accommodate responsive column cell using CSS

I've been working on optimizing my mobile website using a responsive column layout. The challenge I'm facing is getting the images within each column to stretch to 100% width while automatically adjusting their height. I experimented with settin ...

Node.js MongoDB Error: db.open is undefined

I recently started experimenting with MongoDB and encountered an error message stating TypeError: db.open is not a function. Although new mongodb.Server and new mongodb.Db seem to be functioning correctly. The issue arises while using [email protecte ...

Generating progress bar in Javascript while exporting CSV fileCan JavaScript export CSV and

Looking for a way to add a progress bar while generating and serving a CSV file via ajax? The database-heavy process is causing a delay, so I need a loader on the screen that disappears once the task is complete. It should be done with ajax or stay on th ...