Ways to eliminate the relationship between parent and child

I am attempting to create a design featuring a large circle surrounded by smaller circles. The parent element is the large circle, and the small circles are its children. My goal is for any circle to change color to red when hovered over, but if I hover over one of the child circles, the parent should also turn red.

Below is my current code snippet:

var parentDiv = document.getElementById('parentDiv');
var largeDiv = document.getElementById('large');

var div = 360 / 6;
var radius = 150;

var offsetToChildCenter = 50;

var offsetToParentCenter = parseInt(largeDiv.offsetWidth / 2); //assuming parent is square
var totalOffset = offsetToParentCenter - offsetToChildCenter;

for (var i = 1; i <= 6; ++i) {
  var childDiv = document.createElement('div');
  childDiv.className = 'small';
  childDiv.style.position = 'absolute';

  var y = Math.sin((div * i) * (Math.PI / 180)) * radius;
  var x = Math.cos((div * i) * (Math.PI / 180)) * radius;

  childDiv.style.top = (y + totalOffset).toString() + "px";
  childDiv.style.left = (x + totalOffset).toString() + "px";

  childDiv.style.width = `${offsetToChildCenter * 2}px`
  childDiv.style.height = `${offsetToChildCenter * 2}px`
  childDiv.innerHTML = "Example Text"

  parentDiv.appendChild(childDiv);
}
#large {
  position: relative;
  margin: 150px;
  width: 150px;
  height: 150px;
  border-radius: 150px;
  background-color: white;
  color: red;
  justify-content: center;
  align-items: center;
  border-radius: 100%;
  text-align: center;
  display: flex;
}

.small {
  position: absolute;
  border-radius: 150px;
  background-color: white;
  color: red;
  justify-content: center;
  align-items: center;
  border-radius: 100%;
  text-align: center;
  display: flex;
}

.small:hover {
  background-color: red;
  color: white;
}

.small:active {
  background-color: red;
  color: white;
  box-shadow: 0px 0px 20px red;
}


/* for centering */

html {
  display: flex;
  height: 100%;
}

body {
  margin: auto
}
<head>
  <title></title>
  <link rel='stylesheet' href='styles.css' />

</head>

<body>
  <div id="parentDiv">
    <div id="large"> Example Center Text</div>
  </div>

  <script src="calc.js"></script>

</body>

To address the issue, I attempted to create an additional empty parent div and made the large circle a sibling of the small circles. However, the small circles did not center themselves around the large circle as intended.

As a beginner in HTML/CSS/JS, any assistance or guidance would be greatly appreciated!

Answer №1

Hovering solely on the child element is not feasible, but a similar effect can be achieved using the sibling element

var parentDiv = document.getElementById('parentDiv');
var largeDiv = document.getElementById('sibling');

var div = 360 / 6;
var radius = 150;

var offsetToChildCenter = 50;

var offsetToParentCenter = parseInt(largeDiv.offsetWidth / 2); //assuming parent is square
var totalOffset = offsetToParentCenter - offsetToChildCenter;

for (var i = 1; i <= 6; ++i) {
  var childDiv = document.createElement('div');
  childDiv.className = 'small';
  childDiv.style.position = 'absolute';

  var y = Math.sin((div * i) * (Math.PI / 180)) * radius;
  var x = Math.cos((div * i) * (Math.PI / 180)) * radius;

  childDiv.style.top = (y + totalOffset).toString() + "px";
  childDiv.style.left = (x + totalOffset).toString() + "px";

  childDiv.style.width = `${offsetToChildCenter * 2}px`
  childDiv.style.height = `${offsetToChildCenter * 2}px`
  childDiv.innerHTML = "Example Text"

  //parentDiv.appendChild(childDiv);
  largeDiv.appendChild(childDiv);
}
#large {
  position: relative;
  margin-left: 150px;
  margin-top: -150px;
  width: 150px;
  height: 150px;
  border-radius: 150px;
  background-color: white;
  color: red;
  justify-content: center;
  align-items: center;
  border-radius: 100%;
  text-align: center;
  display: flex;
}

#sibling {
  width: 150px;
  height: 150px;
  position: relative;
  margin-top: 150px;
  margin-left: 150px;
  background-color: none;
  color: red;
  justify-content: center;
  align-items: center;
  border-radius: 100%;
  text-align: center;
  display: flex;
}

#large:hover {
  color: blue;
  background: pink;
}

.small {
  position: absolute;
  border-radius: 150px;
  background-color: white;
  color: red;
  justify-content: center;
  align-items: center;
  border-radius: 100%;
  text-align: center;
  display: flex;
  z-index: 99;
}

.small:hover {
  background-color: red;
  color: white;
}

.small:active {
  background-color: red;
  color: white;
  box-shadow: 0px 0px 20px red;
}


/* for centering */

html {
  display: flex;
  height: 100%;
}

body {
  margin: auto
}
<head>
  <title></title>
  <link rel='stylesheet' href='styles.css' />

</head>

<body>
  <div id="parentDiv">
    <div id="sibling"></div>
    <div id="large"> Example Center Text</div>

  </div>

  <script src="calc.js"></script>

</body>

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

Adding retrieved ejs elements

Aim: I am struggling with a button that triggers a function to fetch more items from my Mongoose DataBase and display them in a table row dynamically. I am using the get method to retrieve data from the server side. Despite referring to advice from this po ...

Responsive design not functioning properly for Bootstrap columns on website

If my display currently looks like this with three equally distanced images at the top and two images at the bottom, how can I achieve that design? I attempted using Bootstrap by setting the top 3 images as <div className="col-md-4"> and t ...

The constricted styles are causing the whole page to bounce (scroll) up and down

On my SPA frontend, I have a parent div with a height of 580 containing 9 smaller divs (about 190px in height each). The parent div has an overflow set to hidden so that only 3 elements are visible at one time. Every 5 seconds, I change the styles by addin ...

What is the best way to handle a single promise from a shared listener?

I am working on implementing an event listener that will receive events from a server whenever a specific task is completed. I want to structure each task as a promise to create a more organized and clean workflow. How can I resolve each task promise by i ...

The results from utilizing Mongoose's text search feature are inaccurate and not matching the expected

Currently, I am in the process of developing a recipe blog website using Mongoose, Node.js, and Express. However, I have encountered an issue with the text search functionality not producing the desired output. In my Recipe.js schema, I have included spec ...

Error message: Iframe chrome encountered a Uncaught DOMException when attempting to access the 'localStorage' property from 'Window': Document does not have permission

I recently developed a JavaScript widget that utilizes localstorage to set and retrieve properties of the window. Upon opening this widget in Chrome, I encountered an error message: Uncaught DOMException: Failed to read the 'localStorage' prop ...

Generating grid-style buttons dynamically using jQuery Mobile

I am in need of assistance to create a dynamic grid using jQuery Mobile. The grid should consist of buttons with either 'onclick' or 'href' functionality. The number of buttons should be generated dynamically at runtime. Specifically, I ...

Save information entered into dynamically created input boxes in an array

I am currently working on a project to develop a website that generates timetables. However, I have encountered a roadblock in my progress. My issue lies in dynamically generating user-selected text boxes for subjects and faculties using a for loop. Unfor ...

The function is missing from the object, leading to a script error with jQuery

When using different versions of jQuery, I encountered some issues with saving changes in my application. Initially, with jquery-1.4.4.min.js, everything worked except for the save function due to an error I made. However, when switching to jquery-1.7.1.mi ...

Ways to display title attributes when focused using jQuery?

Typically, title attributes in all browsers only appear when the mouse hovers over them. I am looking to also display them when users are focused on them via keyboard navigation. Unfortunately, without using JavaScript, this cannot be achieved solely throu ...

Angular and HTML calendar picker

Is there a way to display a date in an HTML input type="date based on a variable stored in my ts file? The variable ScreenDate is defined in my ts file. <input type="date" [(ngModel)]="ScreenDate"> I tried the above code but it did not work as exp ...

In search of grabbing an image from a list item and relocating it to sit before the division tagged with class "event-title"

I am struggling with selecting each list element within the ul and moving the image inside the div class="event-details" above the div with the class="event-title". This adjustment is necessary for achieving the desired styling since the wordpress Event pl ...

Assistance needed for iterating through JSON data

I need assistance retrieving specific data from a JSON response in my JavaScript code. Unfortunately, the current approach is not providing the desired results: This is my JavaScript code snippet: function retrieveArtists(response){ var artistList ...

SquirrelFish appears to be lacking "bind()", so how can one attach a JS callback to "this" in its absence?

Does anyone know a way to attach a JS callback to "this" without using "bind()"? Based on Samsung specifications: In 2013 with V8: everything functions as expected (refer to linked screenshot, too large to include here) In 2012 with SquirrelFish: encoun ...

Tips on assigning a value to a dynamically generated drop-down element

I used arrays of strings to populate drop-down menus. Is there a way to automatically set the value of each option to match the text content? el.value = opt; seems to be ineffective. var validCoursesKeys = ['opt 1','opt 2','opt ...

ion-grid featuring alternate columns

As someone who is not very familiar with Angular, I am trying to create a grid layout like the one shown here: https://i.stack.imgur.com/nBavk.png The desired layout includes alternating rows with one image followed by two images. My current attempt at a ...

Performance problem with React-Native for-looping through a large array size

I'm currently facing a performance issue with the search function in my react-native app. The search involves scanning through an array of 15,000 elements for specific keywords. Surprisingly, the search performs well and quickly on Android devices, bu ...

Is there a way to extract the visible text from an HTML TextNode without including the HTML tags?

My current challenge involves converting a DOM node and all of its children into plain text markup of a custom design. Utilizing node.childNodes allows me to retrieve a list of all content, which can then be recursively transformed into my desired string f ...

When working in Flask, I am experiencing an issue where my CSS is not linking to my

I am new to coding with HTML, CSS, and Flask and I have encountered an issue where my CSS styling only partially affects my HTML file. Here is the HTML code snippet: <!DOCTYPE html> <html> <head> <meta charset="UTF-8& ...

Rounded Corners on an HTML5 Canvas Triangle

Hi there, I'm relatively new to working with HTML5 Canvas and I'm currently attempting to create a triangle with rounded corners. So far, I've experimented with: ctx.lineJoin = "round"; ctx.lineWidth = 20; However, I haven't been suc ...