【问题标题】:Add tooltip to font awesome icon将工具提示添加到字体真棒图标
【发布时间】:2016-01-15 08:32:10
【问题描述】:

这里有没有人为字体很棒的图标添加工具提示?

我有following jsfiddle,但似乎找不到向图标添加工具提示的指南。

<header>
  <!-- icons for settings, change pwd and list of active sessions -->
  <div id="menuIcons">
    <i class="fa fa-cog"></i>
    <i class="fa fa-user"></i>
    <i class="fa fa-lock"></i>
  </div>

  <!-- display welcome text -->
  <div id="welcomeText">
    <p>Welcome Harriet</p>
  </div>

</header>

【问题讨论】:

  • 好的,所以我想有一个新功能需要与 jsfiddle 一起提交代码。我在这样做时遇到了麻烦,这就是为什么它在上面显示得很有趣。如果你们中的任何人对原始帖子有一些想法,我将不胜感激。

标签: icons tooltip font-awesome


【解决方案1】:

向任何 HTML 输出(不仅是 FontAwesome)添加工具提示的问题本身就是一整本书。 ;-)

默认方式是使用标题属性:

  <div id="welcomeText" title="So nice to see you!">
    <p>Welcome Harriet</p>
  </div>

<i class="fa fa-cog" title="Do you like my fa-coq icon?"></i>

但由于大多数人(包括我)不喜欢标准工具提示,因此有许多工具可以“美化”它们并提供各种增强功能。我个人最喜欢的是jBoxqtip2

【讨论】:

  • 但这在 IE 11 中不起作用是否有任何解决方案
  • 也许这会有所帮助:technet.microsoft.com/en-us/library/…
  • 我已经使用了 data-toggle=tooltip 它的工作,但在 chrome 中创建了两次工具提示
  • 那是一个不同的问题。我建议为此发布一个专门的问题。
【解决方案2】:

简单地在标签中使用标题

<i class="fa fa-edit" title="Edit Mode"></i>

悬停该图标时将显示“编辑模式”。

【讨论】:

【解决方案3】:

您应该使用“title”属性和“data-toogle”(引导程序)。

例如

<i class="fa fa-info" data-toggle="tooltip" title="Hooray!"></i>Hover over me

别忘了添加 javascript 来显示工具提示

<script>
    $(document).ready(function(){
        $('[data-toggle="tooltip"]').tooltip();   
    });
</script>

【讨论】:

  • 这会显示工具提示 - 和 title,因为浏览器会呈现它。因此,您最终会说“万岁!”在 2 个不同的地方,看起来很奇怪。
【解决方案4】:

关于这个问题,用几行SASS就可以轻松实现;

HTML:

<a href="https://www.urbandictionary.com/define.php?term=techninja" data-tool-tip="What's a tech ninja?" target="_blank"><i class="fas fa-2x fa-user-ninja" id="tech--ninja"></i></a>

CSS 输出为:

a[data-tool-tip]{
    position: relative;
    text-decoration: none;
    color: rgba(255,255,255,0.75);
}

a[data-tool-tip]::after{
    content: attr(data-tool-tip);
    display: block;
    position: absolute;
    background-color: dimgrey;
    padding: 1em 3em;
    color: white;
    border-radius: 5px;
    font-size: .5em;
    bottom: 0;
    left: -180%;
    white-space: nowrap;
    transform: scale(0);
    transition: 
    transform ease-out 150ms,
    bottom ease-out 150ms;
}

a[data-tool-tip]:hover::after{
    transform: scale(1);
    bottom: 200%;
}

基本上,属性选择器 [data-tool-tip] 选择里面的任何内容,并允许您根据需要对其进行动画处理。

【讨论】:

  • 这是title 的绝佳替代品,因为它可以设置样式。我个人避免添加更多模块来做像工具提示这样简单的事情。
【解决方案5】:

codepen 或许是一个有用的例子。

<div class="social-icons">
  <a class="social-icon social-icon--codepen">
    <i class="fa fa-codepen"></i>
    <div class="tooltip">Codepen</div>
</div>

body {  
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}

/* Color Variables */
$color-codepen: #000;

/* Social Icon Mixin */
@mixin social-icon($color) {
  background: $color;
  background: linear-gradient(tint($color, 5%), shade($color, 5%));
  border-bottom: 1px solid shade($color, 20%);
  color: tint($color, 50%);

  &:hover {
    color: tint($color, 80%);
    text-shadow: 0px 1px 0px shade($color, 20%);
  }

  .tooltip {
    background: $color;
    background: linear-gradient(tint($color, 15%), $color);
    color: tint($color, 80%);

    &:after {
      border-top-color: $color;
    }
  }
}

/* Social Icons */
.social-icons {
  display: flex;
}

.social-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  width: 80px;
  height: 80px;
  margin: 0 0.5rem;
  border-radius: 50%;
  cursor: pointer;
  font-family: "Helvetica Neue", "Helvetica", "Arial", sans-serif;
  font-size: 2.5rem;
  text-decoration: none;
  text-shadow: 0 1px 0 rgba(0,0,0,0.2);
  transition: all 0.15s ease;

  &:hover {
    color: #fff;

    .tooltip {
      visibility: visible;
      opacity: 1;
      transform: translate(-50%, -150%);
    }
  }

  &:active {
    box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.5) inset;
  }

  &--codepen { @include social-icon($color-codepen); }

  i {
    position: relative;
    top: 1px;
  }
}

/* Tooltips */
.tooltip {
  display: block;
  position: absolute;
  top: 0;
  left: 50%;
  padding: 0.8rem 1rem;
  border-radius: 3px;
  font-size: 0.8rem;
  font-weight: bold;
  opacity: 0;
  pointer-events: none;
  text-transform: uppercase;
  transform: translate(-50%, -100%);
  transition: all 0.3s ease;
  z-index: 1;

  &:after {
    display: block;
    position: absolute;
    bottom: 0;
    left: 50%;
    width: 0;
    height: 0;
    content: "";
    border: solid;
    border-width: 10px 10px 0 10px;
    border-color: transparent;
    transform: translate(-50%, 100%);
  }
}

【讨论】:

    【解决方案6】:

    我发现的最简单的解决方案是将您的 Font Awesome Icon 包装在 &lt;a&gt;&lt;/a&gt; 标签中:

       <Tooltip title="Node.js" >
         <a>
            <FontAwesomeIcon icon={faNode} size="2x" />
         </a>
       </Tooltip>
    

    【讨论】:

      【解决方案7】:

      只需使用原生 html 和 css:

      <div class="tooltip">Hover over me
        <span class="tooltiptext">Tooltip text</span>
      </div>
      
      /* Tooltip container */
      .tooltip {
        position: relative;
        display: inline-block;
        border-bottom: 1px dotted black; /* If you want dots under the hoverable text */
      }
      
      /* Tooltip text */
      .tooltip .tooltiptext {
        visibility: hidden;
        width: 120px;
        background-color: #555;
        color: #fff;
        text-align: center;
        padding: 5px 0;
        border-radius: 6px;
      
        /* Position the tooltip text */
        position: absolute;
        z-index: 1;
        bottom: 125%;
        left: 50%;
        margin-left: -60px;
      
        /* Fade in tooltip */
        opacity: 0;
        transition: opacity 0.3s;
      }
      
      /* Tooltip arrow */
      .tooltip .tooltiptext::after {
        content: "";
        position: absolute;
        top: 100%;
        left: 50%;
        margin-left: -5px;
        border-width: 5px;
        border-style: solid;
        border-color: #555 transparent transparent transparent;
      }
      
      /* Show the tooltip text when you mouse over the tooltip container */
      .tooltip:hover .tooltiptext {
        visibility: visible;
        opacity: 1;
      }
      

      这是来自 w3schools 的example 的来源

      【讨论】:

        猜你喜欢
        • 2019-01-11
        • 2020-03-26
        • 2021-04-30
        • 1970-01-01
        • 2015-01-23
        • 2013-08-05
        • 2016-02-29
        • 2012-07-10
        • 1970-01-01
        相关资源
        最近更新 更多