gpt4 book ai didi

javascript - 如何消除父/子依赖?

转载 作者:搜寻专家 更新时间:2023-10-31 08:49:44 25 4
gpt4 key购买 nike

我想画一个大圆圈,周围有小圆圈。目前父级是大圆圈,小圆圈是它的子级。当我将鼠标悬停在它上面时,我希望任何圆圈都变成红色,但是如果我将鼠标悬停在任何子圆圈上,父级也会变成红色。

这是我的代码:

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); //assumes 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: 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>

我试图通过创建另一个空的父 div 并使大圆成为小圆的同级来解决此问题,但小圆并未以大圆为中心。

我是 html/css/js 的新手,非常感谢任何帮助!

最佳答案

不可能只悬停子元素。但是,您可以使用同级元素创建类似的效果

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); //assumes 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>

关于javascript - 如何消除父/子依赖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57192704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com