【问题标题】:Icon get disappeared after hover in css在css中悬停后图标消失
【发布时间】:2017-12-18 15:54:22
【问题描述】:

我正在研究 Angular JS (1.x),我的要求是我需要在将鼠标悬停在信息图标上时显示工具提示文本。 工具提示文本显示得非常好。但问题是当我将鼠标悬停在该图标上时该图标消失了。一旦我停止悬停,图标就会再次出现。

下面是我的 HTML 代码:

<div class="col-md-4 margin-left-26">            
    <span ng-show="{{ job.information }}" class="glyphicon glyphicon-info-sign
    spark-error-info pad-left-10" tooltip="{{job.information.sparkJob}}">
    </span>
</div>

我的 CSS 来了:

.spark-error-info:hover:after{
      content: attr(tooltip);
      padding: 4px 8px;
      color: white;
      position: absolute;
      left: 0;
      margin-top:10px;
      top: 100%;
      z-index: 20;
      white-space: nowrap;
      -moz-border-radius: 5px;
      -webkit-border-radius: 5px;
      border-radius: 5px;
      background-color: #000000;
}

.spark-error-info:hover:before{
    border: solid;
    border-color: #000 transparent;
    border-width: 0px 10px 10px 10px;
    top: 0px;
    content: "";
    left: 97%;
    position: absolute;
    z-index: 99;
}

谁能帮我解决这个问题?

这是相同的 JsFiddle:https://jsfiddle.net/gLt86eqe/4/

【问题讨论】:

  • 你能解决这个问题吗?
  • 你检查过这个小提琴工作了吗?它能够重现您的问题吗??
  • @SaurabhAgrawal:实际上我从未创建过小提琴。这是我第一次创建。它没有复制任何东西..这就是我想要弄清楚的..
  • 现在它正在工作@SaurabhAgrawal jsfiddle.net/gLt86eqe/4

标签: css angularjs twitter-bootstrap tooltip


【解决方案1】:

问题

.glyphicon 图标需要 伪元素 :before 以使用 content 属性呈现图标。

此content 属性值(用于.glyphicon 图标)在:hover 状态下被覆盖,工具提示content 属性旨在绘制箭头,见下文:

自然元素状态:

.glyphicon-info-sign:before {
    content: "\e086";
}

:hover元素状态:

.spark-error-info:hover:before {
    border: solid;
    border-color: #000 transparent;
    border-width: 0px 10px 10px 10px;
    top: 0px;
    /* refrain from re-declaring the `content` property value as an empty string */
    /* content: ""; */ 
    left: 97%;
    position: absolute;
    z-index: 99;
}

选择器不同,但两个规则匹配并将应用于同一个元素。

解决方案

考虑在.glyphicon 图标的span 元素中嵌套另一个元素。

另一个空的span 元素可以专门用于工具提示 - 使用这种方法可以分离您的关注点。

HTML 示例:

<span class="glyphicon glyphicon-info-sign
    spark-error-info pad-left-10">
   <span class="icon-tooltip" tooltip="Hello this is my fiddle"></span>
</span>

CSS 示例:

然后根据给定的状态更改相应地调整上下文 css 选择器...

.spark-error-info:hover .icon-tooltip:after { ... }

.spark-error-info:hover .icon-tooltip:before { ... }

代码片段演示:

.spark-error-info:hover .icon-tooltip:after {
  content: attr(tooltip);
  padding: 4px 8px;
  color: white;
  position: absolute;
  left: 0;
  margin-top: 10px;
  top: 100%;
  z-index: 20;
  white-space: nowrap;
  -moz-border-radius: 5px;
  -webkit-border-radius: 5px;
  border-radius: 5px;
  background-color: #000000;
}

.spark-error-info:hover .icon-tooltip:before {
  border: solid;
  border-color: #000 transparent;
  border-width: 0px 10px 10px 10px;
  top: 0px;
  content: ""; 
  left: 97%;
  position: absolute;
  z-index: 99;
}

.margin-left-26 {
  margin-left: 26px;
}
<link rel="stylesheet" type="text/css" href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<div class="row">
  <div class="col-md-4 margin-left-26">
    <span class="glyphicon glyphicon-info-sign
    spark-error-info pad-left-10">
      <span class="icon-tooltip" tooltip="Hello this is my fiddle"></span>
    </span>
  </div>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-03
    • 2015-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-27
    • 1970-01-01
    相关资源
    最近更新 更多