代码之家  ›  专栏  ›  技术社区  ›  potato

CSS区分悬停在元素上和它的::before伪元素

  •  1
  • potato  · 技术社区  · 6 年前

    attr() 要实现自定义工具提示,但是当我将鼠标从元素移到工具提示本身上时,工具提示不会消失,有没有方法不使用包含工具提示文本的子元素来修复它?

    [data-tooltip] {
      position: relative;
    }
    
    [data-tooltip]:hover::after {
      content: attr(data-tooltip);
      text-align: center;
      white-space: nowrap;
      position: absolute;
      bottom: calc(100% + 8px);
      left: 50%;
      transform: translateX(-50%);
      background-color: #000;
      color: #fff;
      padding: 0.5em;
      line-height: 1.2em;
      max-height: 1.2em;
    }
    
    [data-tooltip]:hover::before {
      content: "";
      position: absolute;
      bottom: calc(100% - 8px);
      left: 50%;
      transform: translateX(-50%);
      border-width: 8px;
      border-style: solid;
      border-color: #000 transparent transparent transparent;
    }
    <br><br><br><br>
    <center>
        <span data-tooltip="tooltip text"> visible text </span>
    </center>
    2 回复  |  直到 6 年前
        1
  •  1
  •   imste    6 年前

    试用 pointer-events:none 在工具提示上,它将显示工具提示,但它将被光标忽略

    [data-tooltip]:hover::before {
        content: "";
        pointer-events: none;
        position: absolute;
        bottom: calc(100% - 8px);
        left: calc(50% - 8px);
        border-width: 8px;
        border-style: solid;
        border-color: #000 transparent transparent transparent;
    }