Bootstrap pop-up plug-in


May 04, 2021 09:00 Bootstrap


Table of contents


Bootstrap pop-up plug-in

This section describes the Bootstrap pop-up plug-in.

Popover, similar to Tooltip, provides an extended view. T o activate the pop-up, the user simply hovers over the element. T he contents of the pop-up can be populated entirely with the Bootstrap Data API. This method relies on tooltip.

If you want to refer to the features of the plug-in separately, you .js popover, which relies on the Tooltip plug-in. O r, as mentioned in the Bootstrap plug-in overview chapter, you can refer to bootstrap.js or the compressed version of Bootstrap.min .js.

Usage

Popover plug-ins generate content and tags on demand, and by default they put popovers behind their trigger elements. You can add pop-ups in two ways:

  • Via data property: To add a popover, simply add a data-toggle to an anchor/button label. T he title of the anchor is the text of the popover. B y default, the plug-in sets the popover at the top.
    <a href="#" data-toggle="popover" title="Example popover">
       请悬停在我的上面
    </a>
    
  • Enable popovers with JavaScript: Via JavaScript:
    $('#identifier').popover(options)
    

Unlike the drop-down menus and other plug-ins discussed earlier, the Popover plug-in is not a pure CSS plug-in. T o use the plug-in, you must activate it with jquery (read javascript). U se the following script to enable all pop-ups on the page:

$(function () { $("[data-toggle='popover']").popover(); });

Instance

The following example demonstrates the use of popover plug-ins through the data property.

<!DOCTYPE html>
<html>
<head>
   <title>Bootstrap 实例 - 弹出框(Popover)插件</title>
   <link href="//cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="stylesheet">
   <script src="//cdn.bootcss.com/jquery/2.1.1/jquery.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
   <script src="//cdn.bootcss.com/bootstrap/3.3.6/js/bootstrap.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
</head>
<body>

<div class="container" style="padding: 100px 50px 10px;" >
   <button type="button" class="btn btn-default" title="Popover title"         data-container="body" data-toggle="popover" data-placement="left"        data-content="左侧的 Popover 中的一些内容">
      左侧的 Popover
   </button>
   <button type="button" class="btn btn-primary" title="Popover title"         data-container="body" data-toggle="popover" data-placement="top"        data-content="顶部的 Popover 中的一些内容">
      顶部的 Popover
   </button>
   <button type="button" class="btn btn-success" title="Popover title"         data-container="body" data-toggle="popover" data-placement="bottom"        data-content="底部的 Popover 中的一些内容">
      底部的 Popover
   </button>
   <button type="button" class="btn btn-warning" title="Popover title"         data-container="body" data-toggle="popover" data-placement="right"        data-content="右侧的 Popover 中的一些内容">
      右侧的 Popover
   </button>
   </div>

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

</body>
</html>

The result is as follows:

Bootstrap pop-up plug-in

Options

Some options are added through the Bootstrap Data API or called through JavaScript. The following table lists these options:

选项名称 类型/默认值 Data 属性名称 描述
animation boolean
默认值:true
data-animation 向弹出框应用 CSS 褪色过渡效果。
html boolean
默认值:false
data-html 向弹出框插入 HTML。如果为 false,jQuery 的 text 方法将被用于向 dom 插入内容。如果您担心 XSS 攻击,请使用 text。
placement string|function
默认值:top
data-placement 规定如何定位弹出框(即 top|bottom|left|right|auto)。
当指定为 auto 时,会动态调整弹出框。例如,如果 placement 是 "auto left",弹出框将会尽可能显示在左边,在情况不允许的情况下它才会显示在右边。
selector string
默认值:false
data-selector 如果提供了一个选择器,弹出框对象将被委派到指定的目标。
title string | function
默认值:''
data-title 如果未指定 title 属性,则 title 选项是默认的 title 值。
trigger string
默认值:'hover focus'
data-trigger 定义如何触发弹出框: click| hover | focus | manual 。您可以传递多个触发器,每个触发器之间用空格分隔。
delay number | object
默认值:0
data-delay 延迟显示和隐藏弹出框的毫秒数 - 对 manual 手动触发类型不适用。如果提供的是一个数字,那么延迟将会应用于显示和隐藏。如果提供的是对象,结构如下所示:
delay:
{ show: 500, hide: 100 }
container string | false
默认值:false
data-container 向指定元素追加弹出框。
实例: container: 'body'

Method

Here are some useful ways to plug-in Popover:

方法 描述 实例
Options: .popover(options) 向元素集合附加弹出框句柄。
$().popover(options)
Toggle: .popover('toggle') 切换显示/隐藏元素的弹出框。
$('#element').popover('toggle')
Show: .popover('show') 显示元素的弹出框。
$('#element').popover('show')
Hide: .popover('hide') 隐藏元素的弹出框。
$('#element').popover('hide')
Destroy: .popover('destroy') 隐藏并销毁元素的弹出框。
$('#element').popover('destroy')

Instance

The following example demonstrates how pop-up plug-ins work:

<!DOCTYPE html>
<html>
<head>
   <title>Bootstrap 实例 - 弹出框(Popover)插件方法</title>
   <link href="//cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="stylesheet">
   <script src="//cdn.bootcss.com/jquery/2.1.1/jquery.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
   <script src="//cdn.bootcss.com/bootstrap/3.3.6/js/bootstrap.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
</head>
<body>

<div class="container" style="padding: 100px 50px 10px;" >
   <button type="button" class="btn btn-default popover-show"        title="Popover title" data-container="body"        data-toggle="popover" data-placement="left"        data-content="左侧的 Popover 中的一些内容 —— show 方法">
      左侧的 Popover
   </button>
   <button type="button" class="btn btn-primary popover-hide"        title="Popover title" data-container="body"        data-toggle="popover" data-placement="top"        data-content="顶部的 Popover 中的一些内容 —— hide 方法">
      顶部的 Popover
   </button>
   <button type="button" class="btn btn-success popover-destroy"        title="Popover title" data-container="body"        data-toggle="popover" data-placement="bottom"        data-content="底部的 Popover 中的一些内容 —— destroy 方法">
      底部的 Popover
   </button>
   <button type="button" class="btn btn-warning popover-toggle"        title="Popover title" data-container="body"        data-toggle="popover" data-placement="right"        data-content="右侧的 Popover 中的一些内容 —— toggle 方法">
      右侧的 Popover
   </button><br><br><br><br><br><br>
   <p class="popover-options">
      <a href="#" type="button" class="btn btn-warning" title="<h2>Title</h2>"  
         data-container="body" data-toggle="popover" data-content="
         <h4>Popover 中的一些内容 —— options 方法</h4>">
         Popover
      </a>
   </p>
   <script>
      $(function () { $('.popover-show').popover('show');});
      $(function () { $('.popover-hide').popover('hide');});
      $(function () { $('.popover-destroy').popover('destroy');});
      $(function () { $('.popover-toggle').popover('toggle');});
     $(function () { $(".popover-options a").popover({html : true });});
   </script>
</div>

</body>
</html>

The result is as follows:

Bootstrap pop-up plug-in

Event

The following table lists the events to use in the Popover plug-in. These events can be used as hooks in functions.

事件 描述 实例
show.bs.popover 当调用 show 实例方法时立即触发该事件。
$('#mypopover').on('show.bs.popover', function () {
  // 执行一些动作...
})
shown.bs.popover 当弹出框对用户可见时触发该事件(将等待 CSS 过渡效果完成)。
$('#mypopover').on('shown.bs.popover', function () {
  // 执行一些动作...
})
hide.bs.popover 当调用 hide 实例方法时立即触发该事件。
$('#mypopover').on('hide.bs.popover', function () {
  // 执行一些动作...
})
hidden.bs.popover 当工具提示对用户隐藏时触发该事件(将等待 CSS 过渡效果完成)。
$('#mypopover').on('hidden.bs.popover', function () {
  // 执行一些动作...
})

Instance

The following example demonstrates the events of the Popover plug-in:

<!DOCTYPE html>
<html>
<head>
   <title>Bootstrap 实例 - 弹出框(Popover)插件事件</title>
   <link href="//cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="stylesheet">
   <script src="//cdn.bootcss.com/jquery/2.1.1/jquery.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
   <script src="//cdn.bootcss.com/bootstrap/3.3.6/js/bootstrap.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
</head>
<body>

<div clas="container" style="padding: 100px 50px 10px;" >
   <button type="button" class="btn btn-primary popover-show"        title="Popover title" data-container="body"        data-toggle="popover"        data-content="左侧的 Popover 中的一些内容 —— show 方法">
      左侧的 Popover
   </button>

   </div>
   <script>
      $(function () { $('.popover-show').popover('show');});
      $(function () { $('.popover-show').on('shown.bs.popover', function () {
      alert("当显示时警告消息");
   })});
   </script>
</div>

</body>
</html>

The result is as follows:

Bootstrap pop-up plug-in