如何在CSS3中修复工具提示上的过渡?

时间:2019-06-02 18:47:50

标签: css tooltip css-transitions

我试图在工具提示“容器”中添加过渡效果,因此当我将鼠标悬停在工具提示所显示的容器上时。但是当我将鼠标悬停在工具提示本身上时,它也会出现。如何停止工具提示上的悬停过渡效果,使其仅在将鼠标悬停在容器上方时显示?

/*Styles for the tooltip container*/

.tooltip {
  margin-top: 5rem;
  height: 100px;
  width: 300px;
  background-color: aquamarine;
  padding: 1rem;
  cursor: pointer;
  text-transform: capitalize;
  font-size: 18px;
  text-align: center;
  line-height: 100px;
  position: relative;
}


/* Styles for the tooltip*/

.tooltip::before {
  content: attr(title);
  width: 200px;
  height: auto;
  font-size: 15px;
  color: #fff;
  background: #000;
  border-radius: 5px;
  padding: 10px;
  text-align: center;
  line-height: 30px;
  z-index: 2;
  opacity: 0;
  position: absolute;
  left: 50%;
  bottom: calc(100% + 5px);
  transform: translateX(-50%);
  transition: opacity 0.4s ease-in-out;
}


/* Styles for the arrow*/

.tooltip::after {
  content: "";
  width: 0;
  height: 0;
  z-index: 2;
  opacity: 0;
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-top: 5px solid #000;
  transition: opacity 0.4s ease-in-out;
}

.tooltip:hover::after,
.tooltip:hover::before {
  opacity: 1;
}
<div class="tooltip" title="Lorem ipsum dolor sit amet consectetur 
     adipisicing elit. Minima, ipsa.">tooltip</div>

1 个答案:

答案 0 :(得分:0)

只需向伪元素添加pointer-events: none;即可取消指针事件。干杯,欢迎来到! :)

/*Styles for the tooltip container*/

.tooltip {
  margin-top: 5rem;
  height: 100px;
  width: 300px;
  background-color: aquamarine;
  padding: 1rem;
  cursor: pointer;
  text-transform: capitalize;
  font-size: 18px;
  text-align: center;
  line-height: 100px;
  position: relative;
}


/* Styles for the tooltip*/

.tooltip::before {
  content: attr(title);
  width: 200px;
  height: auto;
  font-size: 15px;
  color: #fff;
  background: #000;
  border-radius: 5px;
  padding: 10px;
  text-align: center;
  line-height: 30px;
  z-index: 2;
  opacity: 0;
  position: absolute;
  left: 50%;
  bottom: calc(100% + 5px);
  transform: translateX(-50%);
  transition: opacity 0.4s ease-in-out;
  pointer-events: none;
}


/* Styles for the arrow*/

.tooltip::after {
  content: "";
  width: 0;
  height: 0;
  z-index: 2;
  opacity: 0;
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-top: 5px solid #000;
  transition: opacity 0.4s ease-in-out;
}

.tooltip:hover::after,
.tooltip:hover::before {
  opacity: 1;
}
<div class="tooltip" title="Lorem ipsum dolor sit amet consectetur 
     adipisicing elit. Minima, ipsa.">tooltip</div>