您的位置:首页 > Web前端 > BootStrap

4.5Bootstrap学习js插件篇之工具提示

2014-05-07 15:51 288 查看


案例

受到Jason Frame开发的jQuery.tipsy插件的启发,我们才把这个工具提示插件做的更好,而且此插件不依赖图片,只是使用CSS3来实现动画效果,并使用data属性存储标题。

将鼠标悬停到下面的链接上就可以看到工具提示了:

代码段:

<div class="bs-example tooltip-demo" id="tooltip">
<p class="muted" style="margin-bottom: 0;">Tight pants next level keffiyeh
<a href="#" data-toggle="tooltip" title="Default tooltip">you probably</a>
haven't heard of them. Photo booth beard raw denim letterpress vegan messenger bag stumptown.
Farm-to-table seitan, mcsweeney's fixie sustainable quinoa 8-bit american apparel
<a href="#" data-toggle="tooltip" title="Another tooltip">have a</a>
terry richardson vinyl chambray. Beard stumptown, cardigans banh mi lomo thundercats.
Tofu biodiesel williamsburg marfa, four loko mcsweeney's cleanse vegan chambray. A really ironic artisan
<a href="#" data-toggle="tooltip" title="Another one here too">whatever keytar</a>
, scenester farm-to-table banksy Austin
<a href="#" data-toggle="tooltip" title="The last tip!">twitter handle</a>
freegan cred raw denim single-origin coffee viral.
</p>
</div>


选择性加入的功能

出于性能方面的考虑,工具提示和弹框组件的data属性api是选择性加入的,也就是说你必须自己初始化他们。
现在初始化:

$(function() {
$('.tooltip-demo').tooltip({
selector: "[data-toggle=tooltip]",
container: "body"
})
})
</script>效果




工具提示与按钮组和输入框组联合使用时需要一些特殊设置

.btn-group
 或 
.input-group
内的元素上使用工具提示时,你需要指定
container:
'body'
选项以避免不需要的副作用(例如,当工具提示显示之后,与其合作的页面元素可能变得更宽或是去圆角)。


在禁止使用的页面元素上使用工具提示时需要额外增加一个元素将其包裹起来

为了给
disabled
 或
.disabled
元素添加工具提示,将需要增加工具提示的页面元素包裹在一个
<div>
中,然后对这个
<div>
元素应用工具提示。


用法

通过JavaScript激活工具提示:上面也已经使用过了
$('#example').tooltip(options)


选项

可以将选项通过data属性或JavaScript传递。对于data属性,需要将选项名称放到
data-
之后,例如
data-animation=""


名称类型默认值描述
animationbooleantrueapply a CSS fade transition to the tooltip
htmlbooleanfalseInsert HTML into the tooltip. If false, jQuery's 
text
 method will be
used to insert content into the DOM. Use text if you're worried about XSS attacks.
placementstring | function'top'how to position the tooltip - top | bottom | left | right | auto. 

When "auto" is specified, it will dynamically reorient the tooltip. For example, if placement is "auto left", the tooltip will display to the left when possible, otherwise it will display right.
selectorstringfalseIf a selector is provided, tooltip objects will be delegated to the specified targets.
titlestring | function''default title value if 
title
 attribute isn't present
triggerstring'hover focus'how tooltip is triggered - click | hover | focus | manual. You may pass multiple triggers; separate them with a space.
delaynumber | object0delay showing and hiding the tooltip (ms) - does not apply to manual trigger type
If a number is supplied, delay is applied to both hide/show
Object structure is: 
delay:
{ show: 500, hide: 100 }

containerstring | falsefalseAppends the tooltip to a specific element. Example: 
container:
'body'

以placement为例,代码段:
<div class="tool-demo">
<button type="button" class="btn btn-default" data-toggle="tooltip" data-placement="left" title="左侧的提示">左侧的提示</button>
<button type="button" class="btn btn-default" data-toggle="tooltip" data-placement="top" title="上面的提示">上面的提示</button>
<button type="button" class="btn btn-default" data-toggle="tooltip" data-placement="bottom" title="底部的提示">底部的提示</button>
<button type="button" class="btn btn-default" data-toggle="tooltip" data-placement="right" title="右侧的提示">右侧的提示</button>
</div>javascript启动:
$('.tool-demo').tooltip({
selector: "[data-toggle=tooltip]",
container: "body"
})

预览效果:



关于选项,
animation:false,//是否显示动画
selector: "[data-toggle=tooltip]",//选择器
container: "body",//
trigger:"hover",//指定以什么事件触发该提示框,一般都是悬浮
title:"文本",//如果要提示的元素没有title属性,该title指定其属性title,并显示在提示框中。
delay:0,//延迟(ms)
html:true(暂时没弄懂,官网是说插入html我试了试,不太管用)


方法


$().tooltip(options)

为一组元素应用工具提示。


.tooltip('show')

展示工具提示。

$('#element').tooltip('show')



.tooltip('hide')

隐藏工具提示。

$('#element').tooltip('hide')



.tooltip('toggle')

展示或隐藏工具提示。

$('#element').tooltip('toggle')



.tooltip('destroy')

隐藏并销毁工具提示。

$('#element').tooltip('destroy')



事件

事件类型描述
show.bs.tooltip
show
方法被调用之后,此事件将被立即触发。
shown.bs.tooltip当工具提示展示到用户面前之后(同时CSS过渡效果执行完之后)此事件被触发。
hide.bs.tooltip
hide
方法被调用之后,此事件被触发。
hidden.bs.tooltip当工具提示被隐藏之后(同时CSS过渡效果执行完之后),此事件被触发。
$('#myTooltip').on('hidden.bs.tooltip', function () {
// do something…
})


代码段:
$(function(){
$("#btn").on('hidden.bs.tooltip',function()
{
alert("ok");
})
})


预览效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: