【发布时间】:2021-05-06 19:50:53
【问题描述】:
我正在一个内部站点中制作一些小图表,并且想在<label> 悬停在上面时更改<circle> 元素的不透明度。
我最初想在 CSS 中完成这一切,但没有选择器可以让我定位 circle,并且没有匹配的类。
我希望能够在纯 JS 中做到这一点(as I've seen similar using jQuery)
因此,如果将任一项目悬停在(圆圈或标签)上方,则相应项目将变为 100% 不透明度。
从左到右的圆圈项目有一个固定的顺序是1到n从上到下匹配标签。
body{
min-height: 300px
}
.container {
display: flex;
flex-direction: row;
margin-bottom: 2em !important;
}
.container>* {
align-self: center;
}
.dataset {
max-width: 100%;
width: 100%;
}
.dataset svg {
overflow: visible;
max-width: 25em;
max-height: 25em;
}
fieldset {
order: 2;
margin-left: auto;
border-width: 5px 0 0 0;
border-color: #CCC;
border-style: solid;
font-size: 0.9em;
max-width: 30%;
width: 100%;
}
fieldset legend {
font-size: 1em;
font-family: sans-serif;
font-weight: bold;
padding: 0 1em;
margin: 0 0 0.5em -1em;
}
fieldset label {
display: block;
text-indent: -2.5em;
margin: 0.5em 0 0 2.5em;
line-height: 1.5em;
}
fieldset label span {
height: 1em;
width: 1em;
margin-right: 1em;
border-radius: 0.2em;
display: inline-block;
}
<figure>
<div class="container">
<div class="dataset">
<svg viewBox="0 0 100 100" preserveAspectRatio="xMidYMid meet">
<g class="data-container">
<g class="data-item">
<circle r="1.25" cx="5" cy="88.8" fill="hsl( 18, 98%, 0% )"></circle>
<circle r="1.25" cx="20.83" cy="0"fill="hsl( 18, 98%, 10% )"></circle>
<circle r="1.25" cx="36.6" cy="77.77" fill="hsl( 18, 98%, 20% )"></circle>
<circle r="1.25" cx="52.5" cy="11.1" fill="hsl( 18, 98%, 30% )"></circle>
<circle r="1.25" cx="68.3" cy="66.6" fill="hsl( 18, 98%, 40% )"></circle>
<circle r="1.25" cx="84.16" cy="22.2" fill="hsl( 18, 98%, 50% )"></circle>
</g>
</g>
</svg>
</div>
<fieldset>
<legend>Legend</legend>
<label><span style="background:hsl( 18, 98%, 0% );"></span>Dot 1</label>
<label><span style="background:hsl( 18, 98%, 10% );"></span>Dot 2</label>
<label><span style="background:hsl( 18, 98%, 20% );"></span>Dot 3</label>
<label><span style="background:hsl( 18, 98%, 30% );"></span>Dot 4</label>
<label><span style="background:hsl( 18, 98%, 40% );"></span>Dot 5</label>
<label><span style="background:hsl( 18, 98%, 50% );"></span>Dot 6</label>
</fieldset>
</div>
</figure>
【问题讨论】:
-
您尝试了哪些 JavaScript?为什么不自己将 jQuery 移植到 JS 中呢?你被困在哪里了?
标签: javascript html css