如何创建工具提示?

How to create a tooltip?

我想为工具提示定制一个 CSS class ,它将包含一个长度超过 25-30.Usually 的字符串,如此长的文本不适合 tootltip文本区域。

还有没有办法使用 Tooltip (ui.bootstrap.tooltip) 来做到这一点? 就像使用自定义 CSS class 来获得所需的输出。

这是简单的 css 工具提示 - Plunker DEMO

这是相同的代码片段:

.tooltip {
    position: relative;
    display: inline-block;
    border-bottom: 1px dotted black;
}

.tooltip .tooltiptext {
   
    max-width:100px;
    padding:15px;
    min-height:30px;
    background:#fff;
    visibility: hidden;
    border: 1px solid black;
    color: #000;
    text-align: center;
    border-radius: 6px;
    /* Position the tooltip */
    position: absolute;
    z-index: 1;
}

.tooltip:hover .tooltiptext {
    visibility: visible;
}
<body style="text-align:center;">

<p>Move the mouse over the text below:</p>

<div class="tooltip">Hover over me 1
  <span class="tooltiptext">Tooltip text</span>
</div>
<div class="tooltip">Hover over me 2
  <span class="tooltiptext">Array [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,,1,11,1,1,1,11,1,,1,1,1,1,1,1,1,1,1,1,1,1,1,1]</span>
</div>
</body>

您可以使用一些 css 技巧来根据内容调整工具提示的大小。

这里是一些假设宽度和高度的例子。

.tooltip{
    max-width:100px;
    padding:15px;
    min-height:30px;
    background:#000;/*change accordingly*/
}

以上 css 将创建一个 div 具有固定的最大宽度和可调整大小的高度,将适合您的内容。

The CSS Solution

手头的问题有一个非常简单的解决方案。我基本上添加的是以下 CSS 代码

word-wrap:break-word;

到工具提示文本周围的 class。

这是一个working demo

还有,这个不错read

What if I am using Angular UI?

为了样式化来自 angular ui 的工具提示,我在工具提示代码中添加了 tooltip-class="tooltip"

这里是working demo with angular

这是从 Angular UI documentation

获得的修改后的 plunkr

这里是 bootstrap

中自定义工具提示的完整代码
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <script>
   $(document).ready(function(){
    $("[data-toggle=tooltip]").tooltip();
});


  </script>
  <style>
  .tooltip.top .tooltip-inner {
    background-color:red;
  }
.tooltip.top .tooltip-arrow {
          border-top-color: red;
       }
.tooltip.right .tooltip-inner {
    background-color:blue;
  }
.tooltip.right .tooltip-arrow {
          border-right-color: blue;
       }
.tooltip.bottom .tooltip-inner {
    background-color:green;
    width:400px;
    height:50px;
    padding-top:10px;
  }
.tooltip.bottom .tooltip-arrow {
          border-bottom-color: green;
       }  
.tooltip.left .tooltip-inner {
    background-color:yellow;
  }
.tooltip.left .tooltip-arrow {
          border-left-color: yellow;
       }                    
  </style>
</head>
<body>

   <div class="container">
  <div class="row">
    <div class="col-sm-12">
        <h1>Colored Tooltip</h1>
    </div>
    <div class="col-sm-3">
      <h3>Top</h3>
        <a href="#" data-toggle="tooltip" title="This tooltip is on top!">Hover to see my tooltip</a>
    </div>
    <div class="col-sm-3">
      <h3>Right</h3>
        <a href="#" data-placement="right" data-toggle="tooltip" title="This tooltip is on the right!">Hover to see my tooltip</a>
    </div>
    <div class="col-sm-3">
      <h3>Bottom</h3>
        <a href="#" data-placement="bottom" data-toggle="tooltip" title="This tooltip is on the bottom!">Hover to see my tooltip</a>
    </div>
    <div class="col-sm-3">
      <h3>Left</h3>
        <a href="#" data-placement="left" data-toggle="tooltip" title="This tooltip is on the left!">Hover to see my tooltip</a>
    </div>
  </div>
</div>
</body>
</html>

.tooltip {
    position: relative;
    display: inline-block;
    border-bottom: 1px dotted black;
}

.tooltip .tooltiptext {
   
    max-width:100px;
    padding:15px;
    min-height:30px;
    background:red;
    visibility: hidden;
    border: 1px solid black;
    color: #000;
    text-align: center;
    border-radius: 6px;
    /* Position the tooltip */
    position: absolute;
    z-index: 1;
}

.tooltip:hover .tooltiptext {
    visibility: visible;
}
<body style="text-align:center;">

<p>Move the mouse over the text below:</p>

<div class="tooltip">Hover over me 1
  <span class="tooltiptext">Tooltip text</span>
</div>
<div class="tooltip">Hover over me 2
  <span class="tooltiptext">Array [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,,1,11,1,1,1,11,1,,1,1,1,1,1,1,1,1,1,1,1,1,1,1]</span>
</div>
</body>