Bootstrap弹出框插件(popover.js)

为任意元素添加一小块浮层,就像 iPad 上一样,用于存放非主要信息。

弹出框的标题和内容的长度都是零的话将永远不会被显示出来。

插件依赖

弹出框依赖 工具提示插件 ,因此,如果你定制了 Bootstrap,一定要注意将依赖的插件编译进去。

初始化

由于性能的原因,工具提示和弹出框的 data 编程接口(data api)是必须要手动初始化的

在一个页面上一次性初始化所有弹出框的方式是通过 

data-toggle
 属性选中他们:

复制
$(function () {
  $('[data-toggle="popover"]').popover()
})
Popovers in button groups and input groups require special setting

When using popovers on elements within a 

.btn-group
 or an  
.input-group
, you'll have to specify the option
container: 'body'
 (documented below) to avoid unwanted side effects (such as the element growing wider and/or losing its rounded corners when the popover is triggered).

Don't try to show popovers on hidden elements

Invoking 

$(...).popover('show')
 when the target element is  
display: none;
 will cause the popover to be incorrectly positioned.

Popovers on disabled elements require wrapper elements

To add a popover to a 

disabled
 or  
.disabled
 element, put the element inside of a  
<div>
 and apply the popover to that  
<div>
 instead.

Sometimes you want to add a popover to a hyperlink that wraps multiple lines. The default behavior of the popover plugin is to center it horizontally and vertically. Add 

white-space: nowrap;
 to your anchors to avoid this.

实例

静态弹出框

4个可能的弹出方向:顶部、右侧、底部和左侧。

Popover 顶部

Sed posuere consectetur est at lobortis. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum.

Popover 右侧

Sed posuere consectetur est at lobortis. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum.

Popover 顶部

Sed posuere consectetur est at lobortis. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum.

Popover 左侧

Sed posuere consectetur est at lobortis. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum.

实例演示

复制
<button type="button" class="btn btn-lg btn-danger" data-toggle="popover" title="Popover title" data-content="And here's some amazing content. It's very engaging. Right?">点我弹出/隐藏弹出框</button>
4个弹出方向
     
复制
<button type="button" class="btn btn-default" data-container="body" data-toggle="popover" data-placement="left" data-content="Vivamus sagittis lacus vel augue laoreet rutrum faucibus.">
  Popover on 左侧
</button>

<button type="button" class="btn btn-default" data-container="body" data-toggle="popover" data-placement="top" data-content="Vivamus sagittis lacus vel augue laoreet rutrum faucibus.">
  Popover on 顶部
</button>

<button type="button" class="btn btn-default" data-container="body" data-toggle="popover" data-placement="bottom" data-content="Vivamus
sagittis lacus vel augue laoreet rutrum faucibus.">
  Popover on 底部
</button>

<button type="button" class="btn btn-default" data-container="body" data-toggle="popover" data-placement="right" data-content="Vivamus sagittis lacus vel augue laoreet rutrum faucibus.">
  Popover on 右侧
</button>
点击并让弹出框消失

通过使用 

focus
 触发器可以在用户点击弹出框是让其消失。

实现“点击并让弹出框消失”的效果需要一些额外的代码

为了更好的跨浏览器和跨平台效果,你必须使用 

<a>
 标签, 不能使用  
<button>
 标签,并且,还必须包含
role="button"
  
tabindex
 属性。

复制
<a tabindex="0" class="btn btn-lg btn-danger" role="button" data-toggle="popover" data-trigger="focus" title="Dismissible popover" data-content="And here's some amazing content. It's very engaging. Right?">可消失的弹出框</a>

用法

通过 JavaScript 代码启动弹出框:

复制
$('#example').popover(options)

参数

可以通过 data 属性或 JavaScript 传递参数。对于 data 属性,将参数名附着到 

data-
 后面,例如  
data-animation=""

名称类型默认值描述
animationbooleantrue为弹出框赋予淡出的 CSS 动画效果。
containerstring | falsefalse

Appends the popover to a specific element. Example: 

container: 'body'
. This option is particularly useful in that it allows you to position the popover in the flow of the document near the triggering element - which will prevent the popover from floating away from the triggering element during a window resize.

contentstring | function''

Default content value if 

data-content
 attribute isn't present.

If a function is given, it will be called with its 

this
 reference set to the element that the popover is attached to.

delaynumber | object0

Delay showing and hiding the popover (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 }

htmlbooleanfalseInsert HTML into the popover. 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'right'

How to position the popover - top | bottom | left | right | auto.
When "auto" is specified, it will dynamically reorient the popover. For example, if placement is "auto left", the popover will display to the left when possible, otherwise it will display right.

When a function is used to determine the placement, it is called with the popover DOM node as its first argument and the triggering element DOM node as its second. The 

this
 context is set to the popover instance.

selectorstringfalseIf a selector is provided, popover objects will be delegated to the specified targets. In practice, this is used to enable dynamic HTML content to have popovers added. See this and an informative example.
templatestring
'<div class="popover" role="tooltip"><div class="arrow"></div><h3 class="popover-title"></h3><div class="popover-content"></div></div>'

Base HTML to use when creating the popover.

The popover's 

title
 will be injected into the 
.popover-title
.

The popover's 

content
 will be injected into the 
.popover-content
.

.arrow
 will become the popover's arrow.

The outermost wrapper element should have the 

.popover
 class.

titlestring | function''

Default title value if 

title
 attribute isn't present.

If a function is given, it will be called with its 

this
 reference set to the element that the popover is attached to.

triggerstring'click'How popover is triggered - click | hover | focus | manual. You may pass multiple triggers; separate them with a space.
viewportstring | object{ selector: 'body', padding: 0 }

Keeps the popover within the bounds of this element. Example: 

viewport: '#viewport'
 or 
{ "selector": "#viewport", "padding": 0 }

Data attributes for individual popovers

Options for individual popovers can alternatively be specified through the use of data attributes, as explained above.

Methods

$().popover(options)

Initializes popovers for an element collection.

.popover('show')

Reveals an element's popover. Returns to the caller before the popover has actually been shown (i.e. before the

shown.bs.popover
 event occurs). This is considered a "manual" triggering of the popover. Popovers whose both title and content are zero-length are never displayed.

复制
$('#element').popover('show')
.popover('hide')

Hides an element's popover. Returns to the caller before the popover has actually been hidden (i.e. before the

hidden.bs.popover
 event occurs). This is considered a "manual" triggering of the popover.

复制
$('#element').popover('hide')
.popover('toggle')

Toggles an element's popover. Returns to the caller before the popover has actually been shown or hidden (i.e. before the 

shown.bs.popover
 or  
hidden.bs.popover
 event occurs). This is considered a "manual" triggering of the popover.

复制
$('#element').popover('toggle')
.popover('destroy')

Hides and destroys an element's popover. Popovers that use delegation (which are created using the 

selector
 option) cannot be individually destroyed on descendant trigger elements.

复制
$('#element').popover('destroy')

Events

Event TypeDescription
show.bs.popoverThis event fires immediately when the 
show
 instance method is called.
shown.bs.popoverThis event is fired when the popover has been made visible to the user (will wait for CSS transitions to complete).
hide.bs.popoverThis event is fired immediately when the 
hide
 instance method has been called.
hidden.bs.popoverThis event is fired when the popover has finished being hidden from the user (will wait for CSS transitions to complete).
复制
$('#myPopover').on('hidden.bs.popover', function () {
  // do something…
})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用和介绍了弹出popover)是与提示类似的一种组件,用于在鼠标点击元素后显示更多的内容。弹出的用法和提示类似,只需要将data-toggle属性的值从"tooltip"改为"popover",然后在相应的元素上调用popover()方法即可。需要注意的是,弹出的触发动作是单击,而不是鼠标移上。同时,弹出的初始化代码要写在jQuery的初始化代码中。提到了可以使用data-trigger="focus"属性来设置在鼠标点击元素外部区域来关闭弹出。这样,当再次点击指定元素外部区域时,弹出会关闭。下面是一个示例代码,展示了如何使用bootstrap弹出: ```html <div class="container"> <h3>弹出实例</h3> <br> <a href="#" title="取消弹出" data-toggle="popover" data-trigger="focus" data-content="点击文档的其他地方关闭我">点我</a> </div> <script> $(document).ready(function(){ $('[data-toggle="popover"]').popover(); }); </script> ``` 在上述代码中,我们创建了一个包含标题和一个链接的容器。链接元素上添加了data-toggle="popover"属性,使其成为一个弹出触发器。data-trigger="focus"属性用于设置点击元素外部区域来关闭弹出。最后,在jQuery的初始化代码中,调用popover()方法对所有标记了data-toggle="popover"属性的元素进行初始化。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Bootstrap弹出](https://blog.csdn.net/yang5726685/article/details/69220345)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Bootstrap 弹出](https://blog.csdn.net/qq_27870421/article/details/97260000)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值