在另一个元素的悬停事件上设置元素可见性
Set element visibility on hover event of another element
我正在为我正在进行的项目制定一个水平时间表。
为此,我一直在定制一个模板。
我希望给定日期的事件在时间轴上的适当位置 (event_bullet) 上显示为 'bullet',并且该事件的信息面板 (event1bubble) 在鼠标悬停在项目符号上时可见。
我正在使用的削减代码如下。
项目符号已显示,信息面板一开始是 'visibility: hidden',但在 event_bullet:hover 上它不可见。 :(
我开始时使用的是 'display: block' 和 'display: none',但后来转到可见性以查看我是否有更多运气。
.Timeline {
display: flex;
align-items: center;
height: 500px;
}
.event1 {
position: relative;
}
.event1Bubble {
visibility: hidden;
position: absolute;
background-color: rgba(158, 158, 158, 0.1);
width: 139px;
height: 60px;
top: -70px;
left: -15px;
border-radius: 5px;
box-shadow: inset 0 0 5px rgba(158, 158, 158, 0.64);
}
.event1Bubble:after,
.event1Bubble:before {
content: "";
position: absolute;
width: 0;
height: 0;
border-style: solid;
border-color: transparent;
border-bottom: 0;
}
.event1Bubble:before {
bottom: -10px;
left: 13px;
border-top-color: rgba(222, 222, 222, 0.66);
border-width: 12px;
}
.event1Bubble:after {
bottom: -8px;
left: 13px;
border-top-color: #F6F6F6;
border-width: 12px;
}
.eventTime {
display: flex;
}
.eventTitle {
font-family: "Arial Black", Gadget, sans-serif;
color: #a71930;
font-size: 11px;
text-transform: uppercase;
display: flex;
flex: 1;
align-items: center;
margin-left: 12px;
margin-top: 5px;
}
.time {
position: absolute;
font-family: Arial, Helvetica, sans-serif;
width: 50px;
font-size: 8px;
margin-top: 5px;
margin-left: -5px;
color: #9E9E9E;
}
.event_bullet {
display: block;
content: url(https://www.pngfind.com/pngs/m/16-164808_bullet-point-png-circle-transparent-png.png);
width: 15px;
height: 15px;
}
.event_bullet:hover .event1bubble {
visibility: visible;
}
<div class="Timeline">
<svg height="5" width="200">
<line x1="0" y1="0" x2="200" y2="0" style="stroke:#004165;stroke-width:5" />
</svg>
<div class="event1">
<div class="event1Bubble">
<div class="eventTitle">Event occured</div>
</div>
<div class="event_bullet"></div>
<div class="time">9:27 AM</div>
</div>
<svg height="5" width="600">
<line x1="0" y1="0" x2="600" y2="0" style="stroke:#223344;stroke-width:5" />
</svg>
<svg height="5" width="50">
<line x1="0" y1="0" x2="50" y2="0" style="stroke:#004165;stroke-width:5" />
</svg>
<svg height="5" width="50">
<line x1="0" y1="0" x2="50" y2="0" style="stroke:#004165;stroke-width:5" />
</svg>
<svg height="20" width="42">
<line x1="1" y1="0" x2="1" y2="20" style="stroke:#004165;stroke-width:2" />
<circle cx="11" cy="10" r="3" fill="#004165" />
<circle cx="21" cy="10" r="3" fill="#004165" />
<circle cx="31" cy="10" r="3" fill="#004165" />
<line x1="41" y1="0" x2="41" y2="20" style="stroke:#004165;stroke-width:2" />
</svg>
</div>
如果可以修改 HTML,可以将 <div class="event_bullet">
移到 <div class="event1Bubble">
之前,并使用 general sibling selector, ~
, or the adjacent sibling selector、+
.
<!-- Current code -->
<div class="event1">
<div class="event1Bubble">...</div>
<div class="event_bullet"></div>
...
</div>
<!-- Updated code -->
<div class="event1">
<div class="event_bullet"></div>
<div class="event1Bubble">...</div>
...
</div>
/* Current code */
.event_bullet:hover .event1bubble {
visibility: visible;
}
/* Updated code */
.event_bullet:hover ~ .event1bubble {
visibility: visible;
}
.Timeline {
display: flex;
align-items: center;
height: 500px;
}
.event1 {
position: relative;
}
.event1Bubble {
visibility: hidden;
position: absolute;
background-color: rgba(158, 158, 158, 0.1);
width: 139px;
height: 60px;
top: -70px;
left: -15px;
border-radius: 5px;
box-shadow: inset 0 0 5px rgba(158, 158, 158, 0.64);
}
.event1Bubble:after,
.event1Bubble:before {
content: "";
position: absolute;
width: 0;
height: 0;
border-style: solid;
border-color: transparent;
border-bottom: 0;
}
.event1Bubble:before {
bottom: -10px;
left: 13px;
border-top-color: rgba(222, 222, 222, 0.66);
border-width: 12px;
}
.event1Bubble:after {
bottom: -8px;
left: 13px;
border-top-color: #F6F6F6;
border-width: 12px;
}
.eventTime {
display: flex;
}
.eventTitle {
font-family: "Arial Black", Gadget, sans-serif;
color: #a71930;
font-size: 11px;
text-transform: uppercase;
display: flex;
flex: 1;
align-items: center;
margin-left: 12px;
margin-top: 5px;
}
.time {
position: absolute;
font-family: Arial, Helvetica, sans-serif;
width: 50px;
font-size: 8px;
margin-top: 5px;
margin-left: -5px;
color: #9E9E9E;
}
.event_bullet {
display: block;
content: url(https://www.pngfind.com/pngs/m/16-164808_bullet-point-png-circle-transparent-png.png);
width: 15px;
height: 15px;
}
.event_bullet:hover ~ .event1Bubble {
visibility: visible;
}
<div class="Timeline">
<svg height="5" width="200">
<line x1="0" y1="0" x2="200" y2="0" style="stroke:#004165;stroke-width:5" />
</svg>
<div class="event1">
<div class="event_bullet"></div>
<div class="event1Bubble">
<div class="eventTitle">Event occured</div>
</div>
<div class="time">9:27 AM</div>
</div>
<svg height="5" width="600">
<line x1="0" y1="0" x2="600" y2="0" style="stroke:#223344;stroke-width:5" />
</svg>
<svg height="5" width="50">
<line x1="0" y1="0" x2="50" y2="0" style="stroke:#004165;stroke-width:5" />
</svg>
<svg height="5" width="50">
<line x1="0" y1="0" x2="50" y2="0" style="stroke:#004165;stroke-width:5" />
</svg>
<svg height="20" width="42">
<line x1="1" y1="0" x2="1" y2="20" style="stroke:#004165;stroke-width:2" />
<circle cx="11" cy="10" r="3" fill="#004165" />
<circle cx="21" cy="10" r="3" fill="#004165" />
<circle cx="31" cy="10" r="3" fill="#004165" />
<line x1="41" y1="0" x2="41" y2="20" style="stroke:#004165;stroke-width:2" />
</svg>
</div>
在 CSS 中没有直接的方法可以在将鼠标悬停在其子项上时向父项添加效果,因此您可以改用 javascript
将它添加到您的 JS 文件中,它应该可以工作
const event_bullet = document.getElementsByClassName("event_bullet")[0];
const event1bubble = document.getElementsByClassName("event1Bubble")[0];
event_bullet.addEventListener("mouseover", () => {
event1bubble.style.visibility = "visible";
});
event_bullet.addEventListener("mouseout", () => {
event1bubble.style.visibility = "hidden";
});
我正在为我正在进行的项目制定一个水平时间表。 为此,我一直在定制一个模板。 我希望给定日期的事件在时间轴上的适当位置 (event_bullet) 上显示为 'bullet',并且该事件的信息面板 (event1bubble) 在鼠标悬停在项目符号上时可见。
我正在使用的削减代码如下。 项目符号已显示,信息面板一开始是 'visibility: hidden',但在 event_bullet:hover 上它不可见。 :(
我开始时使用的是 'display: block' 和 'display: none',但后来转到可见性以查看我是否有更多运气。
.Timeline {
display: flex;
align-items: center;
height: 500px;
}
.event1 {
position: relative;
}
.event1Bubble {
visibility: hidden;
position: absolute;
background-color: rgba(158, 158, 158, 0.1);
width: 139px;
height: 60px;
top: -70px;
left: -15px;
border-radius: 5px;
box-shadow: inset 0 0 5px rgba(158, 158, 158, 0.64);
}
.event1Bubble:after,
.event1Bubble:before {
content: "";
position: absolute;
width: 0;
height: 0;
border-style: solid;
border-color: transparent;
border-bottom: 0;
}
.event1Bubble:before {
bottom: -10px;
left: 13px;
border-top-color: rgba(222, 222, 222, 0.66);
border-width: 12px;
}
.event1Bubble:after {
bottom: -8px;
left: 13px;
border-top-color: #F6F6F6;
border-width: 12px;
}
.eventTime {
display: flex;
}
.eventTitle {
font-family: "Arial Black", Gadget, sans-serif;
color: #a71930;
font-size: 11px;
text-transform: uppercase;
display: flex;
flex: 1;
align-items: center;
margin-left: 12px;
margin-top: 5px;
}
.time {
position: absolute;
font-family: Arial, Helvetica, sans-serif;
width: 50px;
font-size: 8px;
margin-top: 5px;
margin-left: -5px;
color: #9E9E9E;
}
.event_bullet {
display: block;
content: url(https://www.pngfind.com/pngs/m/16-164808_bullet-point-png-circle-transparent-png.png);
width: 15px;
height: 15px;
}
.event_bullet:hover .event1bubble {
visibility: visible;
}
<div class="Timeline">
<svg height="5" width="200">
<line x1="0" y1="0" x2="200" y2="0" style="stroke:#004165;stroke-width:5" />
</svg>
<div class="event1">
<div class="event1Bubble">
<div class="eventTitle">Event occured</div>
</div>
<div class="event_bullet"></div>
<div class="time">9:27 AM</div>
</div>
<svg height="5" width="600">
<line x1="0" y1="0" x2="600" y2="0" style="stroke:#223344;stroke-width:5" />
</svg>
<svg height="5" width="50">
<line x1="0" y1="0" x2="50" y2="0" style="stroke:#004165;stroke-width:5" />
</svg>
<svg height="5" width="50">
<line x1="0" y1="0" x2="50" y2="0" style="stroke:#004165;stroke-width:5" />
</svg>
<svg height="20" width="42">
<line x1="1" y1="0" x2="1" y2="20" style="stroke:#004165;stroke-width:2" />
<circle cx="11" cy="10" r="3" fill="#004165" />
<circle cx="21" cy="10" r="3" fill="#004165" />
<circle cx="31" cy="10" r="3" fill="#004165" />
<line x1="41" y1="0" x2="41" y2="20" style="stroke:#004165;stroke-width:2" />
</svg>
</div>
如果可以修改 HTML,可以将 <div class="event_bullet">
移到 <div class="event1Bubble">
之前,并使用 general sibling selector, ~
, or the adjacent sibling selector、+
.
<!-- Current code -->
<div class="event1">
<div class="event1Bubble">...</div>
<div class="event_bullet"></div>
...
</div>
<!-- Updated code -->
<div class="event1">
<div class="event_bullet"></div>
<div class="event1Bubble">...</div>
...
</div>
/* Current code */
.event_bullet:hover .event1bubble {
visibility: visible;
}
/* Updated code */
.event_bullet:hover ~ .event1bubble {
visibility: visible;
}
.Timeline {
display: flex;
align-items: center;
height: 500px;
}
.event1 {
position: relative;
}
.event1Bubble {
visibility: hidden;
position: absolute;
background-color: rgba(158, 158, 158, 0.1);
width: 139px;
height: 60px;
top: -70px;
left: -15px;
border-radius: 5px;
box-shadow: inset 0 0 5px rgba(158, 158, 158, 0.64);
}
.event1Bubble:after,
.event1Bubble:before {
content: "";
position: absolute;
width: 0;
height: 0;
border-style: solid;
border-color: transparent;
border-bottom: 0;
}
.event1Bubble:before {
bottom: -10px;
left: 13px;
border-top-color: rgba(222, 222, 222, 0.66);
border-width: 12px;
}
.event1Bubble:after {
bottom: -8px;
left: 13px;
border-top-color: #F6F6F6;
border-width: 12px;
}
.eventTime {
display: flex;
}
.eventTitle {
font-family: "Arial Black", Gadget, sans-serif;
color: #a71930;
font-size: 11px;
text-transform: uppercase;
display: flex;
flex: 1;
align-items: center;
margin-left: 12px;
margin-top: 5px;
}
.time {
position: absolute;
font-family: Arial, Helvetica, sans-serif;
width: 50px;
font-size: 8px;
margin-top: 5px;
margin-left: -5px;
color: #9E9E9E;
}
.event_bullet {
display: block;
content: url(https://www.pngfind.com/pngs/m/16-164808_bullet-point-png-circle-transparent-png.png);
width: 15px;
height: 15px;
}
.event_bullet:hover ~ .event1Bubble {
visibility: visible;
}
<div class="Timeline">
<svg height="5" width="200">
<line x1="0" y1="0" x2="200" y2="0" style="stroke:#004165;stroke-width:5" />
</svg>
<div class="event1">
<div class="event_bullet"></div>
<div class="event1Bubble">
<div class="eventTitle">Event occured</div>
</div>
<div class="time">9:27 AM</div>
</div>
<svg height="5" width="600">
<line x1="0" y1="0" x2="600" y2="0" style="stroke:#223344;stroke-width:5" />
</svg>
<svg height="5" width="50">
<line x1="0" y1="0" x2="50" y2="0" style="stroke:#004165;stroke-width:5" />
</svg>
<svg height="5" width="50">
<line x1="0" y1="0" x2="50" y2="0" style="stroke:#004165;stroke-width:5" />
</svg>
<svg height="20" width="42">
<line x1="1" y1="0" x2="1" y2="20" style="stroke:#004165;stroke-width:2" />
<circle cx="11" cy="10" r="3" fill="#004165" />
<circle cx="21" cy="10" r="3" fill="#004165" />
<circle cx="31" cy="10" r="3" fill="#004165" />
<line x1="41" y1="0" x2="41" y2="20" style="stroke:#004165;stroke-width:2" />
</svg>
</div>
在 CSS 中没有直接的方法可以在将鼠标悬停在其子项上时向父项添加效果,因此您可以改用 javascript
将它添加到您的 JS 文件中,它应该可以工作
const event_bullet = document.getElementsByClassName("event_bullet")[0];
const event1bubble = document.getElementsByClassName("event1Bubble")[0];
event_bullet.addEventListener("mouseover", () => {
event1bubble.style.visibility = "visible";
});
event_bullet.addEventListener("mouseout", () => {
event1bubble.style.visibility = "hidden";
});