如何创建工具提示?
Posted
技术标签:
【中文标题】如何创建工具提示?【英文标题】:How to create a tooltip? 【发布时间】:2016-07-16 11:59:26 【问题描述】:我想为 tooltip 制作一个自定义 CSS 类,它将包裹长度超过 25-30 的字符串。通常这么长的文本不适合 tootltip 文本区域。
还有没有办法使用 Tooltip (ui.bootstrap.tooltip) 做到这一点? 就像使用自定义 CSS 类来获得所需的输出一样。
这是简单的 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>
【问题讨论】:
你需要向我们展示你到目前为止所做的事情。 @Satej 这里是演示plnkr.co/edit/qhnkkTNfl9i7zu7aMXFJ?p=preview 你想要这样的东西吗?plnkr.co/edit/4xoSrItVinIvPTy89ldO?p=preview 是的,正确。 @satej 已将其作为答案发布,如果满足您的要求,请选择作为答案。 【参考方案1】:.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>
【讨论】:
【参考方案2】:这是引导程序中自定义工具提示的完整代码
<!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>
【讨论】:
【参考方案3】:CSS 解决方案
手头的问题有一个非常简单的解决方案。我本质上添加的是以下 CSS 代码
word-wrap:break-word;
到围绕您的工具提示文本的类。
这是working demo
另外,这是一个很好的read
如果我使用 Angular UI 会怎样?
为了从 angular ui 设置工具提示的样式,我在工具提示代码中添加了 tooltip-class="tooltip"
。
这是working demo with angular
这是从Angular UI documentation获得的修改plunkr
【讨论】:
我们可以用这个angular-ui.github.io/bootstrap/#/tooltip Tooltip (ui.bootstrap.tooltip) 来做吗 是的,给你。你必须尝试相同的样式,但它确实有效。这是link。还编辑了答案【参考方案4】:您可以创建一个使用一些 css 技巧来根据内容调整工具提示的大小。
下面是一些假设宽度和高度的示例。
.tooltip
max-width:100px;
padding:15px;
min-height:30px;
background:#000;/*change accordingly*/
在 css 之上将创建一个 div,它具有固定的最大宽度和可调整大小的高度以适合您的内容。
【讨论】:
请看这个演示plnkr.co/edit/qhnkkTNfl9i7zu7aMXFJ?p=preview 看第二个所有的文本都移出区域,即工具提示区域。我该怎么办? 这个问题是因为工作之间没有空格...你可以使用一个css属性word-break:break-work
休息就好了以上是关于如何创建工具提示?的主要内容,如果未能解决你的问题,请参考以下文章