JavaScript

让 Bootstrap 的组件栩栩如生——现在有 13 个自定义 jQuery 插件。

单独或编译

插件可以单独包含(尽管有些具有必需的依赖项),也可以一次全部包含。bootstrap.jsbootstrap.min.js都在一个文件中包含所有插件。

数据属性

您可以完全通过标记 API 使用所有 Bootstrap 插件,而无需编写任何 JavaScript 代码。这是 Bootstrap 的第一类 API,在使用插件时应该是您的首要考虑因素。

也就是说,在某些情况下,可能需要关闭此功能。因此,我们还提供了禁用数据属性 API 的能力,方法是取消绑定以“data-api”命名的 body 上的所有事件。这看起来像这样:

  1. $ “身体” )。关闭'.data-api'

或者,要针对特定​​插件,只需将插件的名称作为命名空间与 data-api 命名空间一起包含,如下所示:

  1. $ “身体” )。关闭'.alert.data-api'

程序化 API

我们也相信您应该能够纯粹通过 JavaScript API 使用所有 Bootstrap 插件。所有公共 API 都是单一的、可链接的方法,并返回所操作的集合。

  1. $ “.btn.danger” )。按钮“切换” )。addClass “胖”

所有方法都应该接受一个可选的选项对象,一个针对特定方法的字符串,或者什么都没有(它启动一个具有默认行为的插件):

  1. $ “#myModal” )。modal () // 用默认值初始化
  2. $ “#myModal” )。modal ({ keyboard : false }) // 没有键盘初始化
  3. $ “#myModal” )。modal ( 'show' ) // 立即初始化并调用 show

每个插件还在 `Constructor` 属性上公开其原始构造函数$.fn.popover.Constructor:如果您想获取特定的插件实例,请直接从元素中检索它:$('[rel=popover]').data('popover')

活动

Bootstrap 为大多数插件的独特操作提供自定义事件。通常,这些以不定式和过去分词形式出现 - 其中不定式(例如show)在事件开始时触发,其过去分词形式(例如shown)在动作完成时触发。

所有不定式事件都提供 preventDefault 功能。这提供了在动作开始之前停止执行的能力。

  1. $ '#myModal' )。'显示' 功能e {
  2. 如果(!数据返回e preventDefault () // 停止显示模态
  3. })

关于过渡

对于简单的过渡效果,将 bootstrap-transition.js 与其他 JS 文件一起包含一次。如果您使用的是已编译(或缩小)的 bootstrap.js,则无需包含它——它已经存在。

用例

过渡插件的几个例子:

  • 在模态中滑动或淡出
  • 淡出标签
  • 淡出警报
  • 滑动轮播窗格

例子

模态是精简但灵活的对话框提示,具有最低要求的功能和智能默认值。

静态示例

在页脚中带有页眉、正文和一组操作的渲染模式。

  1. <div class = "模态隐藏淡入淡出" >
  2. <div= “模态标题” >
  3. <button type = "button" class = "close" data-dismiss = "modal" aria-hidden = "true" > × </按钮>
  4. <h3>模态标题</h3>
  5. </div>
  6. <div= “模态体” >
  7. <p>一个很好的身体...... </p>
  8. </div>
  9. <div= “模态页脚” >
  10. <a href = "#" class = "btn" >关闭</a>
  11. <a href = "#" class = "btn btn-primary" >保存更改</a>
  12. </div>
  13. </div>

现场演示

单击下面的按钮,通过 JavaScript 切换模式。它将从页面顶部向下滑动并淡入。

  1. <!-- 触发模态的按钮 -->
  2. <a href = "#myModal" role = "button" class = "btn" data-toggle = "modal" >启动演示模态</a>
  3.  
  4. <!-- 模态 -->
  5. <div id = "myModal" class = "modal hide fade" tabindex = "-1" role = "dialog" aria- labelledby = "myModalLabel" aria-hidden = "true" >
  6. <div= “模态标题” >
  7. <button type = "button" class = "close" data-dismiss = "modal" aria-hidden = "true" > × </button>
  8. <h3 id = "myModalLabel" >模态标题</h3>
  9. </div>
  10. <div= “模态体” >
  11. <p>一个很好的身体...... </p>
  12. </div>
  13. <div= “模态页脚” >
  14. <button class = "btn" data-dismiss = "modal" aria-hidden = "true" >关闭</button>
  15. <button class = "btn btn-primary" >保存更改</button>
  16. </div>
  17. </div>

用法

通过数据属性

无需编写 JavaScript 即可激活模式。在控制器元素上设置data-toggle="modal",如按钮,以及一个data-target="#foo"href="#foo"以特定模式为目标进行切换。

  1. <button type = "button" data-toggle = "modal" data-target = "#myModal" >启动模态</button>

通过 JavaScript

myModal使用单行 JavaScript调用带有 id 的模式:

  1. $ '#myModal' )。模态选项

选项

选项可以通过数据属性或 JavaScript 传递。对于数据属性,将选项名称附加到 中data-,如data-backdrop="".

姓名 类型 默认 描述
背景 布尔值 真的 包括一个模态背景元素。或者,指定static在单击时不关闭模式的背景。
键盘 布尔值 真的 按下退出键时关闭模态
节目 布尔值 真的 初始化时显示模态。
偏僻的 小路 错误的

如果提供了远程 url,内容将通过 jQuery 的load方法加载并注入到.modal-body. 如果您使用的是数据 api,您也可以使用href标签来指定远程源。这方面的一个例子如下所示:

  1. <a data-toggle="modal" href="remote.html" data-target="#modal">click me</a>

方法

.modal(选项)

将您的内容激活为模式。接受一个可选选项object

  1. $ '#myModal' )。模态({
  2. 键盘
  3. })

.modal('切换')

手动切换模式。

  1. $ '#myModal' )。模态'切换'

.modal('显示')

手动打开一个模态。

  1. $ '#myModal' )。模态“显示”

.modal('隐藏')

手动隐藏模态。

  1. $ '#myModal' )。模态“隐藏”

活动

Bootstrap 的模态类公开了一些用于连接模态功能的事件。

事件 描述
节目 show调用实例方法时立即触发此事件。
显示 当模式对用户可见时触发此事件(将等待 css 转换完成)​​。
隐藏 hide调用实例方法时立即触发此事件。
当模式对用户完成隐藏时会触发此事件(将等待 css 转换完成)​​。
  1. $ '#myModal' )。on ( '隐藏' , function () {
  2. // 做一点事…
  3. })

导航栏中的示例

ScrollSpy 插件用于根据滚动位置自动更新导航目标。滚动导航栏下方的区域并观察活动类的变化。下拉子项目也将突出显示。

@胖的

广告紧身裤 keytar,早午餐 id 艺术派对 dolor labe。Pitchfork yr enim lo-fi 在售罄之前 qui。Tumblr 从农场到餐桌的自行车权利。Anim keffiyeh carles 开衫。Velit seitan mcsweeney 的照相亭 3 wolf moon irure。Cosby 毛衣 lomo jean 短裤,williamsburg 连帽衫 minim qui 你可能没听说过它们和开衫信托基金过失生物柴油韦斯安德森美学。Nihil 纹身 accusamus,具有讽刺意味的生物柴油 keffiyeh 工匠 ullamco 后果。

@mdo

Veniam marfa 小胡子滑板,adipisicing fugiat velit 干草叉胡须。Freegan 胡须 aliqua cupidatat mcsweeney 的 vero。Cupidatat 四个 loko nisi,ea helvetica nulla carles。纹身 cosby 毛衣食品卡车,mcsweeney 的非免费乙烯基。低保真韦斯安德森 +1 裁缝。卡尔斯非审美练习 quis 绅士化。布鲁克林 adipisicing 精酿啤酒副 keytar deserunt。

Occaecat commodo aliqua delectus。Fap 精酿啤酒 deserunt 滑板 ea。Lomo 自行车权利 adipisicing banh mi, velit ea sunt next level locavore single-origin coffee in magna veniam。高生活 id 乙烯基,回声公园 consequat quis aliquip banh mi 干草叉。Vero VHS est adipisicing。Consectetur nisi DIY 迷你邮差包。Cred ex in,可持续的 delectus consectetur 腰包 iphone。

In incididunt echo park, officia deserunt mcsweeney's proident master cleanse thundercats sapiente veniam. Excepteur VHS elit, proident shoreditch +1 biodiesel laborum craft beer. Single-origin coffee wayfarers irure four loko, cupidatat terry richardson master cleanse. Assumenda you probably haven't heard of them art party fanny pack, tattooed nulla cardigan tempor ad. Proident wolf nesciunt sartorial keffiyeh eu banh mi sustainable. Elit wolf voluptate, lo-fi ea portland before they sold out four loko. Locavore enim nostrud mlkshk brooklyn nesciunt.

three

Ad leggings keytar, brunch id art party dolor labore. Pitchfork yr enim lo-fi before they sold out qui. Tumblr farm-to-table bicycle rights whatever. Anim keffiyeh carles cardigan. Velit seitan mcsweeney's photo booth 3 wolf moon irure. Cosby sweater lomo jean shorts, williamsburg hoodie minim qui you probably haven't heard of them et cardigan trust fund culpa biodiesel wes anderson aesthetic. Nihil tattooed accusamus, cred irony biodiesel keffiyeh artisan ullamco consequat.

Keytar twee 博客,过错邮差包 marfa 不管是什么美味的食品卡车。Sapiente 合成器 id 假设。Locavore sed helvetica 陈词滥调具有讽刺意味,您可能没有听说过它们导致连帽衫无麸质 lo-fi fap aliquip。在售罄之前,Labore 就出现了,terry Richardson 早午餐 nesciunt quis cosby 毛衣 pariatur keffiyeh ut helvetica artisan。开衫精酿啤酒 seitan 现成的 velit。VHS chambray labouris tempor veniam。Anim mollit minim commodo ullamco 雷猫。


用法

通过数据属性

要轻松地将 scrollspy 行为添加到您的顶栏导航,只需添加data-spy="scroll"到您要监视的元素(通常是正文)并data-target=".navbar"选择要使用的导航。您需要将 scrollspy 与.nav组件一起使用。

  1. <body data-spy = "scroll" data-target = ".navbar" > ... </body>

通过 JavaScript

通过 JavaScript 调用 scrollspy:

  1. $ '#navbar' )。卷轴间谍()
小心!导航栏链接必须具有可解析的 id 目标。例如, a <a href="#home">home</a>必须对应 dom 中的某些东西 like <div id="home"></div>

方法

.scrollspy('刷新')

当使用 scrollspy 和从 DOM 中添加或删除元素时,你需要像这样调用 refresh 方法:

  1. $ ( '[data-spy="scroll"]' )。每个函数(){
  2. var $spy = $ (这个)。scrollspy ( '刷新' )
  3. });

选项

选项可以通过数据属性或 JavaScript 传递。对于数据属性,将选项名称附加到 中data-,如data-offset="".

姓名 类型 默认 描述
抵消 数字 10 计算滚动位置时从顶部偏移的像素。

活动

事件 描述
启用 每当滚动间谍激活新项目时,就会触发此事件。

示例选项卡

添加快速、动态的选项卡功能以通过本地内容窗格进行转换,甚至通过下拉菜单。

你可能没听说过奥斯汀牛仔短裤。Nesciunt tofu stumptown aliqua,复古合成大师净化。小胡子陈词滥调,威廉斯堡卡尔斯素食主义者 helvetica。Reprehenderit 屠夫复古 keffiyeh 捕梦网合成器。Cosby 毛衣 eu banh mi, qui irure terry richardson ex squid。Aliquip placeat 鼠尾草 iphone。Seitan aliquip quis 开衫美国服装,屠夫 voluptate nisi qui。

Food truck fixie locavore, accusamus mcsweeney's marfa nulla single-origin coffee squid. Exercitation +1 labore velit, blog sartorial PBR leggings next level wes anderson artisan four loko farm-to-table craft beer twee. Qui photo booth letterpress, commodo enim craft beer mlkshk aliquip jean shorts ullamco ad vinyl cillum PBR. Homo nostrud organic, assumenda labore aesthetic magna delectus mollit. Keytar helvetica VHS salvia yr, vero magna velit sapiente labore stumptown. Vegan fanny pack odio cillum wes anderson 8-bit, sustainable jean shorts beard ut DIY ethical culpa terry richardson biodiesel. Art party scenester stumptown, tumblr butcher vero sint qui sapiente accusamus tattooed echo park.


用法

通过 JavaScript 启用可选项卡(每个选项卡需要单独激活):

  1. $ '#myTab a' )。点击功能e {
  2. e . 防止默认();
  3. $ 这个)。选项卡“显示” );
  4. })

您可以通过多种方式激活单个选项卡:

  1. $ ( '#myTab a[href="#profile"]' )。选项卡“显示” );// 按名称选择标签
  2. $ ( '#myTab a:first' )。选项卡“显示” );// 选择第一个标签
  3. $ ( '#myTab a:last' )。选项卡“显示” );// 选择最后一个标签
  4. $ ( '#myTab li:eq(2) a' )。选项卡“显示” );// 选择第三个标签(0-indexed)

标记

您无需编写任何 JavaScript,只需在元素上指定data-toggle="tab"或即可激活选项卡或药丸导航。data-toggle="pill"navnav-tabs类添加到选项卡ul将应用 Bootstrap 选项卡样式。

  1. <ul class = "nav 导航标签" >
  2. <li><a href = "#home" data-toggle = "tab" >主页</a></li>
  3. <li><a href = "#profile" data-toggle = "tab" >个人资料</a></li>
  4. <li><a href = "#messages" data-toggle = "tab" >消息</a></li>
  5. <li><a href = "#settings" data-toggle = "tab" >设置</a></li>
  6. </ul>

方法

$().tab

激活选项卡元素和内容容器。Tab 应该在 DOM 中具有一个data-target或一个href目标容器节点。

  1. <ul= “导航导航标签” id = “我的标签” >
  2. <li class = "active" ><a href = "#home" >主页</a></li>
  3. <li><a href = "#profile" >个人资料</a></li>
  4. <li><a href = "#messages" >消息</a></li>
  5. <li><a href = "#settings" >设置</a></li>
  6. </ul>
  7.  
  8. <div= "标签内容" >
  9. <div class = "tab-pane active" id = "home" > ... </div>
  10. <div class = "tab-pane" id = "profile" > ... </div>
  11. <div class = "tab-pane" id = "messages" > ... </div>
  12. <div class = "tab-pane" id = "settings" > ... </div>
  13. </div>
  14.  
  15. <脚本>
  16. $ (函数() {
  17. $ ( '#myTab a:last' )。选项卡“显示” );
  18. })
  19. </脚本>

活动

事件 描述
节目 此事件在标签显示时触发,但在新标签显示之前。使用event.targetevent.relatedTarget分别定位活动选项卡和上一个活动选项卡(如果可用)。
显示 显示选项卡后,此事件在选项卡显示时触发。使用event.targetevent.relatedTarget分别定位活动选项卡和上一个活动选项卡(如果可用)。
  1. $ ( 'a[data-toggle="tab"]' )。on '显示' 函数e {
  2. e . 目标// 激活的选项卡
  3. e . relatedTarget // 上一个标签
  4. })

例子

受到 Jason Frame 编写的优秀 jQuery.tipsy 插件的启发;工具提示是一个更新版本,它不依赖图像,使用 CSS3 制作动画,使用数据属性存储本地标题。

将鼠标悬停在下面的链接上以查看工具提示:

紧身裤下一级 keffiyeh你可能没听说过。照相亭胡须生牛仔布凸版素食信使包 stumptown。从农场到餐桌的 seitan,mcsweeney 的 fixie 可持续藜麦 8 位美国服装采用特里·理查森乙烯基青年布。Beard stumptown,开襟羊毛衫 banh mi lomo 雷猫。Tofu biodiesel williamsburg marfa, 4 loko mcsweeney's cleanse vegan chambray。一个真正具有讽刺意味的工匠,无论 keytar,场景农场到餐桌的银行奥斯汀推特处理freegan cred raw denim single-origin 咖啡病毒。

四个方向


用法

通过 JavaScript 触发工具提示:

  1. $ '#example' )。工具提示选项

选项

选项可以通过数据属性或 JavaScript 传递。对于数据属性,将选项名称附加到 中data-,如data-animation="".

姓名 类型 默认 描述
动画 布尔值 真的 对工具提示应用 css 淡入淡出过渡
html 布尔值 错误的 将 html 插入到工具提示中。如果为 false,text将使用 jquery 的方法将内容插入到 dom 中。如果您担心 XSS 攻击,请使用文本。
放置 字符串|函数 '最佳' 如何定位工具提示 - 顶部 | 底部 | 离开 | 正确的
选择器 细绳 错误的 如果提供了选择器,则工具提示对象将被委托给指定的目标。
标题 字符串 | 功能 '' 如果 `title` 标签不存在,则默认标题值
扳机 细绳 '徘徊' 如何触发工具提示 - 点击 | 悬停 | 焦点 | 手动的
延迟 号码 | 目的 0

延迟显示和隐藏工具提示(毫秒) - 不适用于手动触发类型

如果提供了一个数字,延迟将应用于隐藏/显示

对象结构为:delay: { show: 500, hide: 100 }

小心!也可以通过使用数据属性来指定单个工具提示的选项。

标记

出于性能原因,选择加入 Tooltip 和 Popover data-apis。如果您想使用它们,只需指定一个选择器选项。

  1. <a href = "#" rel = "tooltip" title = "first tooltip" >悬停在我身上</a>

方法

$().tooltip(选项)

将工具提示处理程序附加到元素集合。

.tooltip('显示')

显示元素的工具提示。

  1. $ '#element' )。工具提示“显示”

.tooltip('隐藏')

隐藏元素的工具提示。

  1. $ '#element' )。工具提示“隐藏”

.tooltip('切换')

切换元素的工具提示。

  1. $ '#element' )。工具提示“切换”

.tooltip('销毁')

隐藏和破坏元素的工具提示。

  1. $ '#element' )。工具提示“销毁”

例子

向任何元素添加小的内容覆盖,如 iPad 上的内容,以容纳辅助信息。将鼠标悬停在按钮上以触发弹出窗口。需要包含工具提示

静态弹出框

有四个选项可用:上对齐、右对齐、下对齐和左对齐。

弹出顶部

Sed posuere consectetur est 在 lobortis。Aenean eu leo quam。Pellentesque ornare sem lacinia quam venenatis 前庭。

弹出框右

Sed posuere consectetur est 在 lobortis。Aenean eu leo quam。Pellentesque ornare sem lacinia quam venenatis 前庭。

弹出框底部

Sed posuere consectetur est 在 lobortis。Aenean eu leo quam。Pellentesque ornare sem lacinia quam venenatis 前庭。

弹出窗口左

Sed posuere consectetur est 在 lobortis。Aenean eu leo quam。Pellentesque ornare sem lacinia quam venenatis 前庭。

没有显示为弹出框的标记是从 JavaScript 和data属性中的内容生成的。

现场演示

四个方向


用法

通过 JavaScript 启用弹出框:

  1. $ '#example' )。弹出框选项

选项

选项可以通过数据属性或 JavaScript 传递。对于数据属性,将选项名称附加到 中data-,如data-animation="".

姓名 类型 默认 描述
动画 布尔值 真的 对工具提示应用 css 淡入淡出过渡
html 布尔值 错误的 将 html 插入弹出框。如果为 false,text将使用 jquery 的方法将内容插入到 dom 中。如果您担心 XSS 攻击,请使用文本。
放置 字符串|函数 '正确的' 如何定位popover - top | 底部 | 离开 | 正确的
选择器 细绳 错误的 如果提供了选择器,则工具提示对象将被委托给指定的目标
扳机 细绳 '点击' 弹出框是如何触发的 - 点击 | 悬停 | 焦点 | 手动的
标题 字符串 | 功能 '' 如果 `title` 属性不存在,则默认标题值
内容 字符串 | 功能 '' default content value if `data-content` attribute isn't present
delay number | object 0

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 }

Heads up! Options for individual popovers can alternatively be specified through the use of data attributes.

Markup

For performance reasons, the Tooltip and Popover data-apis are opt in. If you would like to use them just specify a selector option.

Methods

$().popover(options)

Initializes popovers for an element collection.

.popover('show')

Reveals an elements popover.

  1. $('#element').popover('show')

.popover('hide')

Hides an elements popover.

  1. $('#element').popover('hide')

.popover('toggle')

Toggles an elements popover.

  1. $('#element').popover('toggle')

.popover('destroy')

Hides and destroys an element's popover.

  1. $('#element').popover('destroy')

Example alerts

Add dismiss functionality to all alert messages with this plugin.

Holy guacamole! Best check yo self, you're not looking too good.

Oh snap! You got an error!

Change this and that and try again. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Cras mattis consectetur purus sit amet fermentum.

Take this action Or do this


Usage

Enable dismissal of an alert via JavaScript:

  1. $(".alert").alert()

Markup

Just add data-dismiss="alert" to your close button to automatically give an alert close functionality.

  1. <a class="close" data-dismiss="alert" href="#">&times;</a>

Methods

$().alert()

Wraps all alerts with close functionality. To have your alerts animate out when closed, make sure they have the .fade and .in class already applied to them.

.alert('close')

Closes an alert.

  1. $(".alert").alert('close')

Events

Bootstrap's alert class exposes a few events for hooking into alert functionality.

Event Description
close This event fires immediately when the close instance method is called.
closed This event is fired when the alert has been closed (will wait for css transitions to complete).
  1. $('#my-alert').bind('closed', function () {
  2. // do something…
  3. })

Example uses

Do more with buttons. Control button states or create groups of buttons for more components like toolbars.

Stateful

Add data-loading-text="Loading..." to use a loading state on a button.

  1. <button type="button" class="btn btn-primary" data-loading-text="Loading...">Loading state</button>

Single toggle

Add data-toggle="button" to activate toggling on a single button.

  1. <button type="button" class="btn" data-toggle="button">Single Toggle</button>

Checkbox

Add data-toggle="buttons-checkbox" for checkbox style toggling on btn-group.

  1. <div class="btn-group" data-toggle="buttons-checkbox">
  2. <button type="button" class="btn">Left</button>
  3. <button type="button" class="btn">Middle</button>
  4. <button type="button" class="btn">Right</button>
  5. </div>

Radio

Add data-toggle="buttons-radio" for radio style toggling on btn-group.

  1. <div class="btn-group" data-toggle="buttons-radio">
  2. <button type="button" class="btn">Left</button>
  3. <button type="button" class="btn">Middle</button>
  4. <button type="button" class="btn">Right</button>
  5. </div>

Usage

Enable buttons via JavaScript:

  1. $('.nav-tabs').button()

Markup

Data attributes are integral to the button plugin. Check out the example code below for the various markup types.

Options

None

Methods

$().button('toggle')

Toggles push state. Gives the button the appearance that it has been activated.

Heads up! You can enable auto toggling of a button by using the data-toggle attribute.
  1. <button type="button" class="btn" data-toggle="button" ></button>

$().button('loading')

Sets button state to loading - disables button and swaps text to loading text. Loading text should be defined on the button element using the data attribute data-loading-text.

  1. <button type="button" class="btn" data-loading-text="loading stuff..." >...</button>
Heads up! Firefox persists the disabled state across page loads. A workaround for this is to use autocomplete="off".

$().button('reset')

Resets button state - swaps text to original text.

$().button(string)

Resets button state - swaps text to any data defined text state.

  1. <button type="button" class="btn" data-complete-text="finished!" >...</button>
  2. <script>
  3. $('.btn').button('complete')
  4. </script>

About

Get base styles and flexible support for collapsible components like accordions and navigation.

* Requires the Transitions plugin to be included.

Example accordion

Using the collapse plugin, we built a simple accordion style widget:

Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
  1. <div class="accordion" id="accordion2">
  2. <div class="accordion-group">
  3. <div class="accordion-heading">
  4. <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseOne">
  5. Collapsible Group Item #1
  6. </a>
  7. </div>
  8. <div id="collapseOne" class="accordion-body collapse in">
  9. <div class="accordion-inner">
  10. Anim pariatur cliche...
  11. </div>
  12. </div>
  13. </div>
  14. <div class="accordion-group">
  15. <div class="accordion-heading">
  16. <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseTwo">
  17. Collapsible Group Item #2
  18. </a>
  19. </div>
  20. <div id="collapseTwo" class="accordion-body collapse">
  21. <div class="accordion-inner">
  22. Anim pariatur cliche...
  23. </div>
  24. </div>
  25. </div>
  26. </div>
  27. ...

You can also use the plugin without the accordion markup. Make a button toggle the expanding and collapsing of another element.

  1. <button type="button" class="btn btn-danger" data-toggle="collapse" data-target="#demo">
  2. simple collapsible
  3. </button>
  4.  
  5. <div id="demo" class="collapse in"></div>

Usage

Via data attributes

Just add data-toggle="collapse" and a data-target to element to automatically assign control of a collapsible element. The data-target attribute accepts a css selector to apply the collapse to. Be sure to add the class collapse to the collapsible element. If you'd like it to default open, add the additional class in.

To add accordion-like group management to a collapsible control, add the data attribute data-parent="#selector". Refer to the demo to see this in action.

Via JavaScript

Enable manually with:

  1. $(".collapse").collapse()

Options

Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-, as in data-parent="".

Name type default description
parent selector false If selector then all collapsible elements under the specified parent will be closed when this collapsible item is shown. (similar to traditional accordion behavior)
toggle boolean true Toggles the collapsible element on invocation

Methods

.collapse(options)

Activates your content as a collapsible element. Accepts an optional options object.

  1. $('#myCollapsible').collapse({
  2. toggle: false
  3. })

.collapse('toggle')

Toggles a collapsible element to shown or hidden.

.collapse('show')

Shows a collapsible element.

.collapse('hide')

Hides a collapsible element.

Events

Bootstrap's collapse class exposes a few events for hooking into collapse functionality.

Event Description
show This event fires immediately when the show instance method is called.
shown This event is fired when a collapse element has been made visible to the user (will wait for css transitions to complete).
hide This event is fired immediately when the hide method has been called.
hidden This event is fired when a collapse element has been hidden from the user (will wait for css transitions to complete).
  1. $('#myCollapsible').on('hidden', function () {
  2. // do something…
  3. })

Example

A basic, easily extended plugin for quickly creating elegant typeaheads with any form text input.

  1. <input type="text" data-provide="typeahead">

Usage

Via data attributes

Add data attributes to register an element with typeahead functionality as shown in the example above.

Via JavaScript

Call the typeahead manually with:

  1. $('.typeahead').typeahead()

Options

Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-, as in data-source="".

Name type default description
source array, function [ ] The data source to query against. May be an array of strings or a function. The function is passed two arguments, the query value in the input field and the process callback. The function may be used synchronously by returning the data source directly or asynchronously via the process callback's single argument.
items number 8 The max number of items to display in the dropdown.
minLength number 1 The minimum character length needed before triggering autocomplete suggestions
matcher function case insensitive The method used to determine if a query matches an item. Accepts a single argument, the item against which to test the query. Access the current query with this.query. Return a boolean true if query is a match.
sorter function exact match,
case sensitive,
case insensitive
Method used to sort autocomplete results. Accepts a single argument items and has the scope of the typeahead instance. Reference the current query with this.query.
updater function returns selected item The method used to return selected item. Accepts a single argument, the item and has the scope of the typeahead instance.
highlighter function highlights all default matches Method used to highlight autocomplete results. Accepts a single argument item and has the scope of the typeahead instance. Should return html.

Methods

.typeahead(options)

Initializes an input with a typeahead.

Example

The subnavigation on the left is a live demo of the affix plugin.


Usage

Via data attributes

To easily add affix behavior to any element, just add data-spy="affix" to the element you want to spy on. Then use offsets to define when to toggle the pinning of an element on and off.

  1. <div data-spy="affix" data-offset-top="200">...</div>
Heads up! You must manage the position of a pinned element and the behavior of its immediate parent. Position is controlled by affix, affix-top, and affix-bottom. Remember to check for a potentially collapsed parent when the affix kicks in as it's removing content from the normal flow of the page.

Via JavaScript

Call the affix plugin via JavaScript:

  1. $('#navbar').affix()

Methods

.affix('refresh')

When using affix in conjunction with adding or removing of elements from the DOM, you'll want to call the refresh method:

  1. $('[data-spy="affix"]').each(function () {
  2. $(this).affix('refresh')
  3. });

Options

Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-, as in data-offset-top="200".

Name type default description
offset number | function | object 10 计算滚动位置时从屏幕偏移的像素。如果提供单个数字,则将在顶部和左侧方向应用偏移量。要侦听单个方向或多个唯一偏移量,只需提供一个 object offset: { x: 10 }。当您需要动态提供偏移量时使用函数(对于某些响应式设计很有用)。