在另一个元素的悬停事件上设置元素可见性

问题描述

我正在为我正在处理的项目制定一个水平时间线。 为此,我一直在定制模板。 我希望某一天的事件在时间轴的适当位置 (event_bullet) 和信息面板 (event1bubble) 上显示为“项目符号”,以便该事件在鼠标悬停在项目符号上时可见。

我正在使用的缩减代码如下。 显示项目符号并且信息面板开始时为“可见性:隐藏”,但在 event_bullet:hover 上它不会变得可见。 :(

我开始使用“显示:块”和“显示:无”,但后来转向可见性,看看我是否会有更多的运气。

.Timeline {
  display: flex;
  align-items: center;
  height: 500px;
}

.event1 {
  position: relative;
}

.event1Bubble {
  visibility: hidden;
  position: absolute;
  background-color: rgba(158,158,0.1);
  width: 139px;
  height: 60px;
  top: -70px;
  left: -15px;
  border-radius: 5px;
  Box-shadow: inset 0 0 5px rgba(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,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~adjacent sibling selector、{{1 }}。

+
<!-- 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,0.1);
  width: 139px;
  height: 60px;
  top: -70px;
  left: -15px;
  border-radius: 5px;
  box-shadow: inset 0 0 5px rgba(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,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;
}

,

有在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";
});