Bootstrap 简明教程
Bootstrap - Overview
Why Use Bootstrap?
-
Mobile first approach -Bootstrap 3框架包含整个库中的移动优先样式,而不是包含在不同的文件中。
-
Browser Support − 所有流行的浏览器都支持它。
-
Easy to get started − 只要有 HTML 和 CSS 知识,任何人都可以开始使用 Bootstrap。而且,Bootstrap 官方网站有很好的文档。
-
Responsive design − Bootstrap 的响应式 CSS 适用于台式机、平板电脑和移动设备。关于响应式设计的更多信息请参见章节 Bootstrap Responsive Design.
-
为开发人员提供构建界面的简洁统一的解决方案。
-
它包含漂亮且实用的内建组件,易于自定义。
-
它还提供基于 Web 的自定义功能。
-
最棒的是它是一个开源。
What Bootstrap Package Includes?
-
Scaffolding − Bootstrap 通过网格系统、链接样式和背景提供基本结构。本节 Bootstrap Basic Structure 中对此进行了详细介绍
-
CSS − Bootstrap 具有全局 CSS 设置、带有可扩展类的基本 HTML 元素样式和高级网格系统等功能。本节 Bootstrap with CSS 中对此进行了详细介绍。
-
Components − Bootstrap 包含十几个可重复使用的组件,用于提供图标、下拉列表、导航、提醒、弹出窗口等等。本节 Layout Components 中对此进行了详细介绍。
-
JavaScript Plugins − Bootstrap 包含十几个自定义 jQuery 插件。您可以轻松全部包含它们,或者逐个包含。本节 Bootstrap Plugins 中对此进行了详细介绍。
-
Customize − 您可以自定义 Bootstrap 的组件、LESS 变量和 jQuery 插件以获取您的专属版本。
Bootstrap - Environment Setup
配置和开始使用 Bootstrap 非常容易。本章将说明如何下载和配置 Bootstrap。我们还将讨论 Bootstrap 文件结构,并通过示例演示其用法。
Download Bootstrap
您可以从 https://getbootstrap.com/ 下载 Bootstrap 的最新版本。当您单击此链接时,您将看到如下屏幕 −
在该屏幕中,您会看到两个按钮 −
-
Download Bootstrap − 单击它,您可以下载 Bootstrap CSS、JavaScript 和字体的预编译和精简版本。这里不包含文档或原始源代码文件。
-
Download Source − 单击它,您可以直接从 GitHub 获取最新的 Bootstrap LESS 和 JavaScript 源代码。
出于更好的理解和易用性的考虑,我们将在整个教程中使用 Bootstrap 的预编译版本。由于文件是已编译和精简的,因此您不必每次都为各个功能包含单独的文件。在撰写本教程时,下载的是最新版本(Bootstrap 3)。
File structure
HTML Template
使用 Bootstrap 的基本 HTML 模板如下所示 −
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap 101 Template</title>
<meta name = "viewport" content = "width = device-width, initial-scale = 1.0">
<!-- Bootstrap -->
<link href = "css/bootstrap.min.css" rel = "stylesheet">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src = "https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src = "https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
</head>
<body>
<h1>Hello, world!</h1>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src = "https://code.jquery.com/jquery.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src = "js/bootstrap.min.js"></script>
</body>
</html>
此处您可以看到 jquery.js 、 bootstrap.min.js 和 bootstrap.min.css 文件,这些文件用于将普通 HTM 文件制作成 Bootstrap 模板。确保在包含 Bootstrap 库之前先包含 jQuery 库。
将在章节 Bootstrap CSS Overview 中讨论此代码片断中每个元素的更多详细信息。
Bootstrap - Grid System
在本节中,我们将讨论 Bootstrap 网格系统。
What is Bootstrap Grid System?
正如 Bootstrap 网格系统的官方文档所述 −
让我们理解上述陈述。Bootstrap 3 在意义上是移动设备优先的,因为 Bootstrap 的代码现在从针对移动设备和平板电脑等较小屏幕开始,然后针对笔记本电脑和台式机等较大屏幕“扩展”组件和网格。
Working of Bootstrap Grid System
网格系统用于通过包含您内容的一系列行和列创建页面布局。以下是如何使用 Bootstrap 网格系统 −
-
必须将行放入 .container 类中以进行适当的对齐和填充。
-
使用行创建列的水平组。
-
内容应置于列内,并且只有列才能成为行的直接子元素。
-
.row and .col-xs-4 等预定义网格类可用于快速生成网格布局。LESS 混入也可用于更语义化的布局。
-
列通过填充创建间隔(列内容之间的间隙)。该填充通过 .rows 对第一列和最后一列有负的利润来对齐的。
-
网格列是通过指定希望跨越的 12 个可用列的数量来创建的。例如,三列相等将使用三个 .col-xs-4 。
Media Queries
媒体查询是一个非常时尚的术语,表示“条件 CSS 规则”。它只是在满足某些规定的条件下应用一些 CSS。如果满足那些条件,则应用样式。
Bootstrap 中的媒体查询允许您基于视口大小移动、显示和隐藏内容。在 LESS 文件中使用以下媒体查询来创建 Bootstrap 网格系统中的关键断点。
/* Extra small devices (phones, less than 768px) */
/* No media query since this is the default in Bootstrap */
/* Small devices (tablets, 768px and up) */
@media (min-width: @screen-sm-min) { ... }
/* Medium devices (desktops, 992px and up) */
@media (min-width: @screen-md-min) { ... }
/* Large devices (large desktops, 1200px and up) */
@media (min-width: @screen-lg-min) { ... }
偶尔将这些扩展为包括 max-width 以将 CSS 限制到较窄的设备集。
@media (max-width: @screen-xs-max) { ... }
@media (min-width: @screen-sm-min) and (max-width: @screen-sm-max) { ... }
@media (min-width: @screen-md-min) and (max-width: @screen-md-max) { ... }
@media (min-width: @screen-lg-min) { ... }
媒体查询有两部分,设备规范,然后是大小规则。在以上情况下,设置了以下规则:
让我们考虑这一行:
@media (min-width: @screen-sm-min) and (max-width: @screen-sm-max) { ... }
对于任何类型的设备,无论如何,使用 min-width:@screen-sm-min,如果屏幕宽度小于 @screen-sm-max,则执行某些操作。
Grid options
下表总结了 Bootstrap 网格系统如何跨多个设备工作的各个方面:
超小设备手机(<768px) |
Small devices Tablets (≥768px) |
Medium devices Desktops (≥992px) |
Large devices Desktops (≥1200px) |
|
Grid behavior |
Horizontal at all times |
从断点开始折叠,高于断点时水平 |
从断点开始折叠,高于断点时水平 |
从断点开始折叠,高于断点时水平 |
Max container width |
None (auto) |
750px |
970px |
1170px |
Class prefix |
.col-xs- |
.col-sm- |
.col-md- |
.col-lg- |
# of columns |
12 |
12 |
12 |
12 |
Max column width |
Auto |
60px |
78px |
95px |
Gutter width |
30px(列两侧各 15px) |
30px(列两侧各 15px) |
30px(列两侧各 15px) |
30px(列两侧各 15px) |
Nestable |
Yes |
Yes |
Yes |
Yes |
Offsets |
Yes |
Yes |
Yes |
Yes |
Column ordering |
Yes |
Yes |
Yes |
Yes |
Responsive column resets
借助四个层级的网格,您很可能会遇到在某些断点处列不完全对齐的问题,因为其中的一个比另一个高。要解决此问题,可以结合使用类 .clearfix 和 responsive utility classes ,如下例所示:
<div class = "container">
<div class = "row" >
<div class = "col-xs-6 col-sm-3" style = "background-color: #dedef8;
box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
<div class = "col-xs-6 col-sm-3" style = "background-color: #dedef8;
box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut.</p>
</div>
<div class = "clearfix visible-xs"></div>
<div class = "col-xs-6 col-sm-3" style = "background-color: #dedef8;
box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
<p>Ut enim ad minim veniam, quis nostrud exercitation ullamco
laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
<div class = "col-xs-6 col-sm-3" style = "background-color: #dedef8;
box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
enim ad minim</p>
</div>
</div>
</div>
这会产生以下结果 −
Offset Columns
偏移量对于更专业化的布局非常有用。它们可用于将列推开以增加间距(例如)。 .col-xs = * 类不支持偏移量,但它们很容易通过使用一个空单元格来复制。
要在大型显示器上使用偏移量,请使用 .col-md-offset- 类。这些类通过 * columns where * 的范围将列的左外边距从 *1 增大到 11 。
在以下示例中,我们有 <div class = "col-md-6">..</div>,我们将使用 .col-md-offset-3 类将其居中。
<div class = "container">
<h1>Hello, world!</h1>
<div class = "row" >
<div class = "col-xs-6 col-md-offset-3" style = "background-color: #dedef8;
box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
</div>
</div>
这会产生以下结果 −
Nesting columns
要在默认网格中嵌套内容,请在现有的 .col-md- 列内添加新的 .row 和 .col-md- 列集。嵌套行应包括总和为 12 的一组列。
在以下示例中,布局有两列,第二列在两行中被分成四个框。
<div class = "container">
<h1>Hello, world!</h1>
<div class = "row">
<div class = "col-md-3" style = "background-color: #dedef8;
box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
<h4>First Column</h4>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
<div class = "col-md-9" style = "background-color: #dedef8;
box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
<h4>Second Column- Split into 4 boxes</h4>
<div class = "row">
<div class = "col-md-6" style = "background-color: #B18904;
box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
<p>Consectetur art party Tonx culpa semiotics.
Pinterest assumenda minim organic quis.</p>
</div>
<div class = "col-md-6" style = "background-color: #B18904;
box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
<p>sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
</div>
<div class = "row">
<div class = "col-md-6" style = "background-color: #B18904;
box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
<p>quis nostrud exercitation ullamco laboris nisi ut aliquip
ex ea commodo consequat.</p>
</div>
<div class = "col-md-6" style = "background-color: #B18904;
box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim.</p>
</div>
</div>
</div>
</div>
</div>
这会产生以下结果 −
Column Ordering
Bootstrap 网格系统的另一个优点是,你可以轻松地按顺序编写列,并在其他列中显示它们。你可以使用 .col-md-push- 和 .col-md-pull- 修饰类轻松更改内置网格列的顺序,其中 * 范围从 1 到 11 。
在以下示例中,我们有两个列布局,左侧列是最窄的,充当侧边栏。我们将使用 .col-md-push- 和 .col-md-pull- 类交换这些列的顺序。
<div class = "container">
<h1>Hello, world!</h1>
<div class = "row">
<p>Before Ordering</p>
<div class = "col-md-4" style = "background-color: #dedef8;
box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
I am on left
</div>
<div class = "col-md-8" style = "background-color: #dedef8;
box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
I am on right
</div>
</div>
<br>
<div class = "row">
<p>After Ordering</p>
<div class = "col-md-4 col-md-push-8" style = "background-color: #dedef8;
box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
I was on left
</div>
<div class = "col-md-8 col-md-pull-4" style = "background-color: #dedef8;
box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
I was on right
</div>
</div>
</div>
这会产生以下结果 −
Bootstrap - CSS Overview
本章概述了 Bootstrap 基础设施的关键部分,包括 Bootstrap 更好的、更快的、更强大的 Web 开发方法。
HTML5 doctype
Bootstrap 使用某些需要使用 HTML5 文档类型的 HTML 元素和 CSS 属性。因此,在使用 Bootstrap 的所有项目开头包含以下 HTML5 文档类型代码段。
<!DOCTYPE html>
<html>
....
</html>
Mobile First
自 Bootstrap 3 发布以来,Bootstrap 已成为移动优先。这意味着可以在整个库中找到“移动优先”样式,而不是将它们放在单独的文件中。你需要将 viewport meta tag 添加到 <head> 元素中,以确保在移动设备上正确渲染和触摸缩放。
<meta name = "viewport" content = "width = device-width, initial-scale = 1.0">
-
width 属性控制设备的宽度。将它设置为 device-width 可确保它在各种设备(移动设备、台式机、平板电脑…)上正确渲染。
-
initial-scale = 1.0 确保在加载时,你的网页将按 1:1 比例呈现,并且开箱即用时不会应用缩放。
将 user-scalable = no 添加到 content 属性以禁用移动设备上的缩放功能,如下所示。使用此更改,用户只能滚动而不缩放,并且会让你的网站感觉更像一个原生应用程序。
<meta name = "viewport" content = "width = device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable = no">
通常 maximum-scale = 1.0 与 user-scalable = no 一起使用。如上所述, user-scalable = no 可能会让用户体验更像原生应用,因此 Bootstrap 不建议使用此属性。
Responsive Images
Bootstrap 3 允许你通过向 <img> 标记添加类 .img-responsive 来使图像响应。此类将 max-width: 100%; 和 height: auto; 应用于图像,以便它很好地缩放为父元素。
<img src = "..." class = "img-responsive" alt = "Responsive image">
Typography and Links
Bootstrap 设置了基本全局显示(背景)、字体和链接样式 −
-
Basic Global display − 设置 <body> 元素上的 background-color: #fff;。
-
Typography − 使用 @font-family-base、@font-size-base 和 @line-height-base 属性作为排版基础。
-
Link styles − 通过属性 @link-color 设置全局链接颜色,仅对 :hover 应用链接下划线。
Normalize
Bootstrap 使用 Normalize 来建立跨浏览器的一致性。
Normalize.css 是一个现代的、适用于 HTML5 的 CSS 重置替代品。它是一个较小的 CSS 文件,可在 HTML 元素的默认样式中提供更好的跨浏览器一致性。
Containers
使用 class .container 来包装页面的内容,并轻松像下面展示那样将内容居中。
<div class = "container">
...
</div>
查看 bootstrap.css 文件中的 .container 类文件 −
.container {
padding-right: 15px;
padding-left: 15px;
margin-right: auto;
margin-left: auto;
}
请注意,由于填充和固定宽度的原因,容器在默认情况下不可嵌套。
查看 bootstrap.css 文件 −
@media (min-width: 768px) {
.container {
width: 750px;
}
}
在其中你可以看到 CSS 具有使用 width 的带有容器的媒体查询。这有助于使用响应能力并且在这些容器类中已相应地形成了网格系统。
Bootstrap - Typography
Bootstrap 在其默认字体栈中使用 Helvetica Neue、Helvetica、Arial 和 sans-serif。使用 Bootstrap 的 typography 特点能创建标题、段落、列表和其他内联元素。让我们在以下部分了解一下每个元素。
Headings
所有 HTML 标题(h1 到 h6)在 Bootstrap 中都具有特定样式。以下显示一个示例 −
<h1>I'm Heading1 h1</h1>
<h2>I'm Heading2 h2</h2>
<h3>I'm Heading3 h3</h3>
<h4>I'm Heading4 h4</h4>
<h5>I'm Heading5 h5</h5>
<h6>I'm Heading6 h6</h6>
使用 Bootstrap 的上述代码段会产生以下结果 −
Inline Subheadings
要在任何标题中添加一个内联副标题,只需在任何元素周围添加 <small> 或添加 .small 类,你就会获得一个浅色的小一些的文本,如下面的示例所示 −
<h1>I'm Heading1 h1. <small>I'm secondary Heading1 h1</small></h1>
<h2>I'm Heading2 h2. <small>I'm secondary Heading2 h2</small></h2>
<h3>I'm Heading3 h3. <small>I'm secondary Heading3 h3</small></h3>
<h4>I'm Heading4 h4. <small>I'm secondary Heading4 h4</small></h4>
<h5>I'm Heading5 h5. <small>I'm secondary Heading5 h5</small></h5>
<h6>I'm Heading6 h6. <small>I'm secondary Heading1 h6</small></h6>
使用 Bootstrap 的上述代码段会产生以下结果 −
Lead Body Copy
要在段落中添加一些强调内容,添加 class = "lead"。这会给你一个更大字体大小、更轻字体和更高行高,如下面的示例 −
<h2>Lead Example</h2>
<p class = "lead">This is an example paragraph demonstrating
the use of lead body copy. This is an example paragraph
demonstrating the use of lead body copy.This is an example
paragraph demonstrating the use of lead body copy.This is an
example paragraph demonstrating the use of lead body copy.
This is an example paragraph demonstrating the use of lead body copy.</p>
Emphasis
HTML 的默认强调标签,如 <small> 将文本设置为父代大小的 85%,<strong> 用较重的字体加粗文本,并且 <em> 让文本变成斜体。
Bootstrap 提供一些可以用来突出强调文本的类,如下面的示例 −
<small>This content is within tag</small><br>
<strong>This content is within tag</strong><br>
<em>This content is within tag and is rendered as italics</em><br>
<p class = "text-left">Left aligned text.</p>
<p class = "text-center">Center aligned text.</p>
<p class = "text-right">Right aligned text.</p>
<p class = "text-muted">This content is muted</p>
<p class = "text-primary">This content carries a primary class</p>
<p class = "text-success">This content carries a success class</p>
<p class = "text-info">This content carries a info class</p>
<p class = "text-warning">This content carries a warning class</p>
<p class = "text-danger">This content carries a danger class</p>
Abbreviations
HTML <abbr> 元素提供缩写或首字母缩写词标记,如 WWW 或 HTTP。Bootstrap 用淡蓝虚线边框在底部样式化 <abbr> 元素,并在悬停时显示完整文本(只要你将此文本添加到 <abbr> title 属性)。要获得略微小的字体大小,则在 <abbr> 中添加 .initialism。
<abbr title = "World Wide Web">WWW</abbr><br>
<abbr title = "Real Simple Syndication" class = "initialism">RSS</abbr>
Addresses
使用 <address> 标签,你可以在网页上显示联系信息。由于 <address> 的默认显示是:block;你需要使用
标签在封闭地址文本中添加换行符。
<address>
<strong>Some Company, Inc.</strong><br>
007 street<br>
Some City, State XXXXX<br>
<abbr title = "Phone">P:</abbr> (123) 456-7890
</address>
<address>
<strong>Full Name</strong><br>
<a href = "mailto:#">mailto@somedomain.com</a>
</address>
Blockquotes
你可以使用默认的 <blockquote> 在任何 HTML 文本周围。其他选项包括,添加 <small> 标签以识别引文来源,以及使用类 .pull-right 右对齐引用块。以下示例演示了所有这些功能 −
<blockquote>
<p>This is a default blockquote example. This is a default
blockquote example. This is a default blockquote
example.This is a default blockquote example. This is a
default blockquote example.</p>
</blockquote>
<blockquote>
This is a blockquote with a source title.
<small>Someone famous in <cite title = "Source Title">Source Title</cite></small>
</blockquote>
<blockquote class = "pull-right">This is a blockquote aligned to the right.
<small>Someone famous in <cite title = "Source Title">Source Title</cite></small>
</blockquote>
Lists
Bootstrap 支持有序列表、无序列表和定义列表。
-
Ordered lists − 有序列表是按照某种顺序排列的列表,并以数字开头。
-
Unordered lists − 无序列表是没有特定顺序的列表,并且通常用项目符号表示。如果你不想显示项目符号,则可以使用类 .list-unstyled 来删除样式。你还可以使用类 .list-inline 将所有列表项放在一行上。
-
Definition lists 在此类列表中,每个列表项均可以由 <dt> 和 <dd> 元素组成。<dt> 代表定义项,与字典类似,此项是要进行定义的术语(或短语)。随后,<dd> 是 <dt> 的定义。可以使用 dl-horizontal 类让 <dl> 中的术语和描述并排排列。
以下示例演示这些类型的每个示例:
<h4>Example of Ordered List</h4>
<ol>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
</ol>
<h4>Example of UnOrdered List</h4>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
</ul>
<h4>Example of Unstyled List</h4>
<ul class = "list-unstyled">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
</ul>
<h4>Example of Inline List</h4>
<ul class = "list-inline">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
</ul>
<h4>Example of Definition List</h4>
<dl>
<dt>Description 1</dt>
<dd>Item 1</dd>
<dt>Description 2</dt>
<dd>Item 2</dd>
</dl>
<h4>Example of Horizontal Definition List</h4>
<dl class = "dl-horizontal">
<dt>Description 1</dt>
<dd>Item 1</dd>
<dt>Description 2</dt>
<dd>Item 2</dd>
</dl>
Bootstrap - Code
Bootstrap 允许您使用两种不同的关键方式显示代码:
-
第一个是 <code> 标记。如果您将显示代码内联,应使用 <code> 标记。
-
第二个是 <pre> 标记。如果需要将代码显示为独立的块元素,或者代码包含多行,则应使用 <pre> 标记。
我们来看下面的示例:
<p><code><header></code> is wrapped as an inline element.</p>
<p>To display code as a standalone block element use <pre> tag as:</p>
<pre>
<article>
<h1>Article Heading</h1>
</article>
</pre>
Bootstrap - Tables
Bootstrap 提供了构建表的简洁布局。Bootstrap 支持的部分表格元素包括:
Sr.No. |
Tag & Description |
1 |
<table> 用于以表格格式显示数据的包装元素 |
2 |
<thead> 表头行(<tr>)的容器元素,用于标记表格列。 |
3 |
<tbody> 表格主体中表行(<tr>)的容器元素。 |
4 |
<tr> 出现在单行中的一组表单元格(<td>或<th>)的容器元素。 |
5 |
<td> Default table cell. |
6 |
<th> 专用于列(或行,具体取决于范围和放置)标签的特殊表单元格。必须在 <thead> 中使用 |
7 |
<caption> 表所包含内容的描述或摘要。 |
Basic Table
如果您希望获得具有浅色填充和水平分隔符的基本表格样式,请将基本类 .table 添加到任意表中,如下面的示例所示:
<table class = "table">
<caption>Basic Table Layout</caption>
<thead>
<tr>
<th>Name</th>
<th>City</th>
</tr>
</thead>
<tbody>
<tr>
<td>Tanmay</td>
<td>Bangalore</td>
</tr>
<tr>
<td>Sachin</td>
<td>Mumbai</td>
</tr>
</tbody>
</table>
Optional Table Classes
除了基本表标记和 .table 类之外,还可以使用一些其他类来设置标记样式。以下部分将向您展示所有这些类。
Striped Table
通过添加 .table-striped 类,您将获得 <tbody> 中的行条纹,如下面的示例所示:
<table class = "table table-striped">
<caption>Striped Table Layout</caption>
<thead>
<tr>
<th>Name</th>
<th>City</th>
<th>Pincode</th>
</tr>
</thead>
<tbody>
<tr>
<td>Tanmay</td>
<td>Bangalore</td>
<td>560001</td>
</tr>
<tr>
<td>Sachin</td>
<td>Mumbai</td>
<td>400003</td>
</tr>
<tr>
<td>Uma</td>
<td>Pune</td>
<td>411027</td>
</tr>
</tbody>
</table>
Bordered Table
通过添加 .table-bordered 类,您将获得包围每个元素的边框,并在整个表格周围形成圆角,如下面的示例所示:
<table class = "table table-bordered">
<caption>Bordered Table Layout</caption>
<thead>
<tr>
<th>Name</th>
<th>City</th>
<th>Pincode</th>
</tr>
</thead>
<tbody>
<tr>
<td>Tanmay</td>
<td>Bangalore</td>
<td>560001</td>
</tr>
<tr>
<td>Sachin</td>
<td>Mumbai</td>
<td>400003</td>
</tr>
<tr>
<td>Uma</td>
<td>Pune</td>
<td>411027</td>
</tr>
</tbody>
</table>
Hover Table
通过添加 .table-hover 类,当光标悬停在行上时,行上将添加浅灰色背景,如下面的示例所示:
<table class = "table table-hover">
<caption>Hover Table Layout</caption>
<thead>
<tr>
<th>Name</th>
<th>City</th>
<th>Pincode</th>
</tr>
</thead>
<tbody>
<tr>
<td>Tanmay</td>
<td>Bangalore</td>
<td>560001</td>
</tr>
<tr>
<td>Sachin</td>
<td>Mumbai</td>
<td>400003</td>
</tr>
<tr>
<td>Uma</td>
<td>Pune</td>
<td>411027</td>
</tr>
</tbody>
</table>
Condensed Table
通过添加 .table-condensed 类,行填充将减少一半以使表浓缩。如下面的示例所示。如果您想要更密集的信息,这将非常有用。
<table class = "table table-condensed">
<caption>Condensed Table Layout</caption>
<thead>
<tr>
<th>Name</th>
<th>City</th>
<th>Pincode</th>
</tr>
</thead>
<tbody>
<tr>
<td>Tanmay</td>
<td>Bangalore</td>
<td>560001</td>
</tr>
<tr>
<td>Sachin</td>
<td>Mumbai</td>
<td>400003</td>
</tr>
<tr>
<td>Uma</td>
<td>Pune</td>
<td>411027</td>
</tr>
</tbody>
</table>
Contextual classes
下表中显示的上下文类可让您更改表格行或单元格的背景颜色。
Sr.No. |
Class & Description |
1 |
.active 将悬停颜色应用到特定行或单元格 |
2 |
.success 表示成功或积极的操作 |
3 |
.warning 表示可能需要关注的警告 |
4 |
.danger 表示危险或可能产生负面影响的操作 |
这些类可应用于 <tr>、<td> 或 <th>。
<table class = "table">
<caption>Contextual Table Layout</caption>
<thead>
<tr>
<th>Product</th>
<th>Payment Date</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr class = "active">
<td>Product1</td>
<td>23/11/2013</td>
<td>Pending</td>
</tr>
<tr class = "success">
<td>Product2</td>
<td>10/11/2013</td>
<td>Delivered</td>
</tr>
<tr class = "warning">
<td>Product3</td>
<td>20/10/2013</td>
<td>In Call to confirm</td>
</tr>
<tr class = "danger">
<td>Product4</td>
<td>20/10/2013</td>
<td>Declined</td>
</tr>
</tbody>
</table>
Responsive Tables
通过将任意 .table 包裹在 .table-responsive 类中,您将使表格在小设备(小于 768px)上水平滚动。在查看宽度大于 768px 的任何内容时,您将不会看到这些表格有任何差异。
<div class = "table-responsive">
<table class = "table">
<caption>Responsive Table Layout</caption>
<thead>
<tr>
<th>Product</th>
<th>Payment Date</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr>
<td>Product1</td>
<td>23/11/2013</td>
<td>Pending</td>
</tr>
<tr>
<td>Product2</td>
<td>10/11/2013</td>
<td>Delivered</td>
</tr>
<tr>
<td>Product3</td>
<td>20/10/2013</td>
<td>In Call to confirm</td>
</tr>
<tr>
<td>Product4</td>
<td>20/10/2013</td>
<td>Declined</td>
</tr>
</tbody>
</table>
</div>
Bootstrap - Forms
在本章中,我们将学习如何使用 Bootstrap 轻松创建表单。Bootstrap 通过针对不同样式的表单提供简单的 HTML 标记和扩展类,使其变得容易。在本章中,我们将学习如何使用 Bootstrap 轻松创建表单。
Form Layout
Bootstrap 为您提供了以下类型的表单布局 -
-
Vertical (default) form
-
In-line form
-
Horizontal form
Vertical or Basic Form
基本表单结构附带 Bootstrap;各个表单控件自动接收一些全局样式。若要创建基本表单,请执行以下操作 -
-
将角色表单添加到父级 <form>元素。
-
使用类 .form-group 将标签和控件包裹在 <div>中。这对最佳间距来说是必需的。
-
向所有文本 <input>、<textarea> 和 <select> 元素添加 .form-control 类。
<form role = "form">
<div class = "form-group">
<label for = "name">Name</label>
<input type = "text" class = "form-control" id = "name" placeholder = "Enter Name">
</div>
<div class = "form-group">
<label for = "inputfile">File input</label>
<input type = "file" id = "inputfile">
<p class = "help-block">Example block-level help text here.</p>
</div>
<div class = "checkbox">
<label><input type = "checkbox"> Check me out</label>
</div>
<button type = "submit" class = "btn btn-default">Submit</button>
</form>
Inline Form
若要创建所有元素均呈内联、左对齐且标签随附的表单,请向 <form> 标签添加类 .form-inline。
<form class = "form-inline" role = "form">
<div class = "form-group">
<label class = "sr-only" for = "name">Name</label>
<input type = "text" class = "form-control" id = "name" placeholder = "Enter Name">
</div>
<div class = "form-group">
<label class = "sr-only" for = "inputfile">File input</label>
<input type = "file" id = "inputfile">
</div>
<div class = "checkbox">
<label><input type = "checkbox"> Check me out</label>
</div>
<button type = "submit" class = "btn btn-default">Submit</button>
</form>
-
默认情况下,Bootstrap 中的输入、选择和文本区具有 100% 宽度。使用内联表单时,您需要在表单控件上设置宽度。
-
使用类 .sr-only 可以隐藏内联表单的标签。
Horizontal Form
水平表单不仅在标记数量上与其他表单有所区别,而且在表单的呈现方式上也有所不同。若要创建使用水平布局的表单,请执行以下操作 -
-
向父级 <form> 元素添加 .form-horizontal 类。
-
使用类 .form-group 将标签和控件包裹在 <div> 中。
-
向标签添加 .control-label 类。
<form class = "form-horizontal" role = "form">
<div class = "form-group">
<label for = "firstname" class = "col-sm-2 control-label">First Name</label>
<div class = "col-sm-10">
<input type = "text" class = "form-control" id = "firstname" placeholder = "Enter First Name">
</div>
</div>
<div class = "form-group">
<label for = "lastname" class = "col-sm-2 control-label">Last Name</label>
<div class = "col-sm-10">
<input type = "text" class = "form-control" id = "lastname" placeholder = "Enter Last Name">
</div>
</div>
<div class = "form-group">
<div class = "col-sm-offset-2 col-sm-10">
<div class = "checkbox">
<label><input type = "checkbox"> Remember me</label>
</div>
</div>
</div>
<div class = "form-group">
<div class = "col-sm-offset-2 col-sm-10">
<button type = "submit" class = "btn btn-default">Sign in</button>
</div>
</div>
</form>
Supported Form Controls
Bootstrap 本机支持最常见的表单控件,主要包括输入、文本区、复选框、单选按钮和选择。
Inputs
最常见的表单文本字段是输入字段。这是用户输入大多数基本表单数据的地方。Bootstrap 支持所有原生 HTML5 输入类型:文本、密码、日期时间、日期时间局部、日期、月份、时间、星期、数字、电子邮件、网址、搜索、电话和颜色。需要正确的类型声明才能使输入完全设置样式。
<form role = "form">
<div class = "form-group">
<label for = "name">Label</label>
<input type = "text" class = "form-control" placeholder = "Text input">
</div>
</form>
Textarea
当需要多行输入时,使用文本区。根据需要更改行属性(行数较少 = 较小的框,行数较多 = 较大的框)。
<form role = "form">
<div class = "form-group">
<label for = "name">Text Area</label>
<textarea class = "form-control" rows = "3"></textarea>
</div>
</form>
CheckBoxes and Radio Buttons
当希望用户从预设选项列表中进行选择时,复选框和单选按钮非常合适。
-
在构建表单时,如果希望用户从列表中勾选任意数量的选项,则需使用复选框。如果您希望限制用户只能进行一项选择,则使用单选按钮。
-
对一系列复选框或单选按钮使用 .checkbox-inline 或 .radio-inline 类,以便控件显示在同一行上。
以下示例展示了两种(默认和内联)类型:
<label for = "name">Example of Default Checkbox and radio button </label>
<div class = "checkbox">
<label>
<input type = "checkbox" value = "">Option 1
</label>
</div>
<div class = "checkbox">
<label>
<input type = "checkbox" value = "">Option 2
</label>
</div>
<div class = "radio">
<label>
<input type = "radio" name = "optionsRadios" id = "optionsRadios1" value = "option1" checked> Option 1
</label>
</div>
<div class = "radio">
<label>
<input type = "radio" name = "optionsRadios" id = "optionsRadios2" value = "option2">
Option 2 - selecting it will deselect option 1
</label>
</div>
<label for = "name">Example of Inline Checkbox and radio button </label>
<div>
<label class = "checkbox-inline">
<input type = "checkbox" id = "inlineCheckbox1" value = "option1"> Option 1
</label>
<label class = "checkbox-inline">
<input type = "checkbox" id = "inlineCheckbox2" value = "option2"> Option 2
</label>
<label class = "checkbox-inline">
<input type = "checkbox" id = "inlineCheckbox3" value = "option3"> Option 3
</label>
<label class = "checkbox-inline">
<input type = "radio" name = "optionsRadiosinline" id = "optionsRadios3" value = "option1" checked> Option 1
</label>
<label class = "checkbox-inline">
<input type = "radio" name = "optionsRadiosinline" id = "optionsRadios4" value = "option2"> Option 2
</label>
</div>
Selects
当您希望允许用户从多个选项中进行选择时,但默认情况下仅允许选择一项时,可以使用选择器。
-
对于用户熟悉的列表选项,例如状态或数字,请使用 <select>。
-
使用 multiple =“multiple” 允许用户选择多个选项。
以下示例展示了两种(选择和多选)类型:
<form role = "form">
<div class = "form-group">
<label for = "name">Select list</label>
<select class = "form-control">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
<label for = "name">Mutiple Select list</label>
<select multiple class = "form-control">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div>
</form>
Static Control
当您需要在水平表单中将纯文本放在表单标签旁边时,对 <p> 使用 .form-control-static 类。
<form class = "form-horizontal" role = "form">
<div class = "form-group">
<label class = "col-sm-2 control-label">Email</label>
<div class = "col-sm-10">
<p class = "form-control-static">email@example.com</p>
</div>
</div>
<div class = "form-group">
<label for = "inputPassword" class = "col-sm-2 control-label">Password</label>
<div class = "col-sm-10">
<input type = "password" class = "form-control" id = "inputPassword" placeholder = "Password">
</div>
</div>
</form>
Form Control States
除了 :focus(即用户点击输入或在其上使用制表符)状态外,Bootstrap 还针对禁用输入和表单验证提供了样式。
Validation States
Bootstrap 包括错误、警告和成功消息的验证样式。要使用,只需向父元素添加适当的类(.has-warning、.has-error 或 .has-success)即可。
以下示例演示了所有表单控件状态:
<form class = "form-horizontal" role = "form">
<div class = "form-group">
<label class = "col-sm-2 control-label">Focused</label>
<div class = "col-sm-10">
<input class = "form-control" id = "focusedInput" type = "text" value = "This is focused...">
</div>
</div>
<div class = "form-group">
<label for = "inputPassword" class = "col-sm-2 control-label">
Disabled
</label>
<div class = "col-sm-10">
<input class = "form-control" id = "disabledInput" type = "text" placeholder = "Disabled input here..." disabled>
</div>
</div>
<fieldset disabled>
<div class = "form-group">
<label for = "disabledTextInput" class = "col-sm-2 control-label">
Disabled input (Fieldset disabled)
</label>
<div class = "col-sm-10">
<input type = "text" id = "disabledTextInput" class = "form-control" placeholder = "Disabled input">
</div>
</div>
<div class = "form-group">
<label for = "disabledSelect" class = "col-sm-2 control-label">
Disabled select menu (Fieldset disabled)
</label>
<div class = "col-sm-10">
<select id = "disabledSelect" class = "form-control">
<option>Disabled select</option>
</select>
</div>
</div>
</fieldset>
<div class = "form-group has-success">
<label class = "col-sm-2 control-label" for = "inputSuccess">
Input with success
</label>
<div class = "col-sm-10">
<input type = "text" class = "form-control" id = "inputSuccess">
</div>
</div>
<div class = "form-group has-warning">
<label class = "col-sm-2 control-label" for = "inputWarning">
Input with warning
</label>
<div class = "col-sm-10">
<input type = "text" class = "form-control" id = "inputWarning">
</div>
</div>
<div class = "form-group has-error">
<label class = "col-sm-2 control-label" for = "inputError">
Input with error
</label>
<div class = "col-sm-10">
<input type = "text" class = "form-control" id = "inputError">
</div>
</div>
</form>
Form Control Sizing
您可以分别使用 .input-lg 和 .col-lg-* 等类设置表单的高度和宽度。以下示例演示了这一点:
<form role = "form">
<div class = "form-group">
<input class = "form-control input-lg" type = "text" placeholder =".input-lg">
</div>
<div class = "form-group">
<input class = "form-control" type = "text" placeholder = "Default input">
</div>
<div class = "form-group">
<input class = "form-control input-sm" type = "text" placeholder = ".input-sm">
</div>
<div class = "form-group"></div>
<div class = "form-group">
<select class = "form-control input-lg">
<option value = "">.input-lg</option>
</select>
</div>
<div class = "form-group">
<select class = "form-control">
<option value = "">Default select</option>
</select>
</div>
<div class = "form-group">
<select class = "form-control input-sm">
<option value = "">.input-sm</option>
</select>
</div>
<div class = "row">
<div class = "col-lg-2">
<input type = "text" class = "form-control" placeholder = ".col-lg-2">
</div>
<div class = "col-lg-3">
<input type = "text" class = "form-control" placeholder = ".col-lg-3">
</div>
<div class = "col-lg-4">
<input type = "text" class = "form-control" placeholder = ".col-lg-4">
</div>
</div>
</form>
Help Text
Bootstrap 表单控件可以具有随输入一起显示的块级帮助文本。要添加一个全宽文本块,请在 <input> 之后使用 .help-block。以下示例演示了这一点:
<form role = "form">
<span>Example of Help Text</span>
<input class = "form-control" type = "text" placeholder = "">
<span class = "help-block">
A longer block of help text that breaks onto a new line and may extend beyond one line.
</span>
</form>
Bootstrap - Buttons
本章涵盖 Bootstrap 按钮的使用方法并附带示例。任何被赋予 .btn 类的对象都将继承圆角灰色按钮的默认外观。但是,Bootstrap 提供了一些用于设置按钮样式的选项,这些选项总结在以下表格中:
Sr.No. |
Class & Description |
1 |
btn Default/ Standard button. |
2 |
btn-primary 提供额外的视觉权重,并标识一系列按钮中的主要操作。 |
3 |
btn-success 表示成功或积极的操作。 |
4 |
btn-info 用于信息警报消息的上下文按钮。 |
5 |
btn-warning 表示此操作应谨慎执行。 |
6 |
btn-danger 表示危险或潜在消极的操作。 |
7 |
btn-link 通过让按钮看起来像链接但保持按钮行为来降低按钮的重要性。 |
以下示例展示了上述所有按钮类:
<!-- Standard button -->
<button type = "button" class = "btn btn-default">Default Button</button>
<!-- Provides extra visual weight and identifies the primary action in a set of buttons -->
<button type = "button" class = "btn btn-primary">Primary Button</button>
<!-- Indicates a successful or positive action -->
<button type = "button" class = "btn btn-success">Success Button</button>
<!-- Contextual button for informational alert messages -->
<button type = "button" class = "btn btn-info">Info Button</button>
<!-- Indicates caution should be taken with this action -->
<button type = "button" class = "btn btn-warning">Warning Button</button>
<!-- Indicates a dangerous or potentially negative action -->
<button type = "button" class = "btn btn-danger">Danger Button</button>
<!-- Deemphasize a button by making it look like a link while maintaining button behavior -->
<button type = "button" class = "btn btn-link">Link Button</button>
Button Size
下表总结了用于获取各种大小按钮的类:
Sr.No. |
Class & Description |
1 |
.btn-lg 这会让按钮大小变大。 |
2 |
.btn-sm 这会让按钮大小变小。 |
3 |
.btn-xs 这会让按钮大小变超小。 |
4 |
.btn-block 这会创建块级按钮,这些按钮会跨越父级的整个宽度。 |
以下示例演示了这一点−
<p>
<button type = "button" class = "btn btn-primary btn-lg">
Large Primary button
</button>
<button type = "button" class = "btn btn-default btn-lg">
Large button
</button>
</p>
<p>
<button type = "button" class = "btn btn-primary">
Default size Primary button
</button>
<button type = "button" class = "btn btn-default">
Default size button
</button>
</p>
<p>
<button type = "button" class = "btn btn-primary btn-sm">
Small Primary button
</button>
<button type = "button" class = "btn btn-default btn-sm">
Small button
</button>
</p>
<p>
<button type = "button" class = "btn btn-primary btn-xs">
Extra small Primary button
</button>
<button type = "button" class = "btn btn-default btn-xs">
Extra small button
</button>
</p>
<p>
<button type = "button" class = "btn btn-primary btn-lg btn-block">
Block level Primary button
</button>
<button type = "button" class = "btn btn-default btn-lg btn-block">
Block level button
</button>
</p>
Button State
Bootstrap 提供了可让您将按钮的状态更改为活跃、禁用等状态的类,以下几节中将讨论每个类。
Active State
处于活跃状态时,按钮会显示为已按压(背景更深、边框更深、内嵌阴影)。下表汇总了用于使按钮元素和锚元素处于活跃状态的类:
Element |
Class |
Button element |
使用 .active 类以显示其已被激活。 |
Anchor element |
将 .active 类添加到 <a> 按钮中以显示其已被激活。 |
以下示例演示了这一点−
<p>
<button type = "button" class = "btn btn-default btn-lg ">
Default Button
</button>
<button type = "button" class = "btn btn-default btn-lg active">
Active Button
</button>
</p>
<p>
<button type = "button" class = "btn btn-primary btn-lg">
Primary button
</button>
<button type = "button" class = "btn btn-primary btn-lg active">
Active Primary button
</button>
</p>
Disabled State
当您禁用按钮时,其颜色会淡去 50%,并且失去渐变效果。
下表汇总了用于使按钮元素和锚元素处于禁用状态的类:
Element |
Class |
Button element |
将 disabled 属性添加到 <button> 按钮中。 |
Anchor element |
将 disabled 类添加到 <a> 按钮中。 Note - 该类只会改变 <a> 的外观,不会改变其功能。您需要使用自定义 JavaScript 来禁用此处的链接。 |
以下示例演示了这一点−
<p>
<button type = "button" class = "btn btn-default btn-lg">
Default Button
</button>
<button type = "button" class = "btn btn-default btn-lg" disabled = "disabled">
Disabled Button
</button>
</p>
<p>
<button type = "button" class = "btn btn-primary btn-lg">
Primary button
</button>
<button type = "button" class = "btn btn-primary btn-lg" disabled = "disabled">
Disabled Primary button
</button>
</p>
<p>
<a href = "#" class = "btn btn-default btn-lg" role = "button">
Link
</a>
<a href = "#" class = "btn btn-default btn-lg disabled" role = "button">
Disabled Link
</a>
</p>
<p>
<a href = "#" class = "btn btn-primary btn-lg" role = "button">
Primary link
</a>
<a href = "#" class = "btn btn-primary btn-lg disabled" role = "button">
Disabled Primary link
</a>
</p>
Button Tags
您可以对 <a>、<button> 或 <input> 元素使用按钮类。不过建议您主要使用 <button> 元素对这些类,以避免出现浏览器不一致的问题。
以下示例演示了这一点−
<a class = "btn btn-default" href = "#" role = "button">Link</a>
<button class = "btn btn-default" type = "submit">Button</button>
<input class = "btn btn-default" type = "button" value = "Input">
<input class = "btn btn-default" type = "submit" value = "Submit">
Bootstrap - Images
本章涵盖 Bootstrap 对图像的支持。Bootstrap 提供了可用于对图像应用一些简单样式的三种类:
-
.img-rounded - 添加 border-radius:6px 以使图像具有圆角。
-
.img-circle - 通过添加 border-radius:500px 使整个图像呈圆形。
-
.img-thumbnail - 添加一些填充和灰色边框:
以下示例演示了这一点−
<img src = "/bootstrap/images/download.png" class = "img-rounded">
<img src = "/bootstrap/images/download.png" class = "img-circle">
<img src = "/bootstrap/images/download.png" class = "img-thumbnail">
Bootstrap - Helper Classes
本章讨论 Bootstrap 中的一些辅助类,这些类可能非常有用。
Close icon
使用通用关闭图标来关闭模态框和提醒等内容。使用类 close 来获取关闭图标。
<p>Close Icon Example
<button type = "button" class = "close" aria-hidden = "true">
×
</button>
</p>
Quick Floats
你可以将元素 float 到左边或右边,使用类 pull-left ,或 pull-right ,以下示例予以演示。
<div class = "pull-left">Quick Float to left</div>
<div class = "pull-right">Quick Float to right</div>
Center Content Blocks
将元素设置为居中的,使用 center-block 类。
<div class = "row">
<div class = "center-block" style = "width:200px; background-color:#ccc;">
This is an example for center-block
</div>
</div>
Clearfix
清除任何元素的 float,使用 .clearfix 类。
<div class = "clearfix" style = "background: #D8D8D8;border: 1px solid #000; padding: 10px;">
<div class = "pull-left" style = "background:#58D3F7;">
Quick Float to left
</div>
<div class = "pull-right" style = "background: #DA81F5;">
Quick Float to right
</div>
</div>
Showing and Hiding Content
你可以强制显示或隐藏一个元素(包括对屏幕阅读器),使用类 .show 和 .hidden 。
<div class = "row" style = "padding: 91px 100px 19px 50px;">
<div class = "show" style = "left-margin:10px; width:300px; background-color:#ccc;">
This is an example for show class
</div>
<div class = "hidden" style = "width:200px; background-color:#ccc;">
This is an example for hide class
</div>
</div>
Screen Reader Content
你可以隐藏一个元素对除屏幕阅读器以外的所有设备,使用 .sr-only 类。
<div class = "row" style = "padding: 91px 100px 19px 50px;">
<form class = "form-inline" role = "form">
<div class = "form-group">
<label class = "sr-only" for = "email">Email address</label>
<input type = "email" class = "form-control" placeholder = "Enter email">
</div>
<div class = "form-group">
<label class = "sr-only" for = "pass">Password</label>
<input type = "password" class = "form-control" placeholder = "Password">
</div>
</form>
</div>
在这里,我们可以看到,两种输入类型的 label 都被赋予 sr-only 类,因此标签仅对屏幕阅读器可见。
Bootstrap - Responsive utilities
Bootstrap 提供了一些有用的辅助类,用于快速移动端友好开发。这些辅助类可以通过媒体查询,结合大型、小型和中型设备,用于通过设备显示和隐藏内容。
谨慎使用这些辅助类,避免创建完全不同的网站版本。 Responsive utilities are currently only available for block and table toggling 。
Classes |
Devices |
.visible-xs |
超小(小于 768px)可见 |
.visible-sm |
小(高达 768 px)可见 |
.visible-md |
中(768 px 到 991 px)可见 |
.visible-lg |
大(992 px 及以上)可见 |
.hidden-xs |
超小(小于 768px)隐藏 |
.hidden-sm |
小(高达 768 px)隐藏 |
.hidden-md |
中(768 px 到 991 px)隐藏 |
.hidden-lg |
大(992 px 及以上)隐藏 |
Print Classes
下表列出了打印类。用于切换打印的内容。
Classes |
|
.visible-print |
Yes Visible |
.hidden-print |
仅对浏览器可见,不对打印可见。 |
Example
以下范例展示了上述辅助类别的用法。调整浏览器的大小或在不同的设备上载入范例,以测试响应式辅助类别。
<div class = "container" style = "padding: 40px;">
<div class = "row visible-on">
<div class = "col-xs-6 col-sm-3" style = "background-color: #dedef8;
box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
<span class = "hidden-xs">Extra small</span>
<span class = "visible-xs">✔ Visible on x-small</span>
</div>
<div class = "col-xs-6 col-sm-3" style = "background-color: #dedef8;
box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
<span class = "hidden-sm">Small</span>
<span class = "visible-sm">✔ Visible on small</span>
</div>
<div class = "clearfix visible-xs"></div>
<div class = "col-xs-6 col-sm-3" style = "background-color: #dedef8;
box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
<span class = "hidden-md">Medium</span>
<span class = "visible-md">✔ Visible on medium</span>
</div>
<div class = "col-xs-6 col-sm-3" style = "background-color: #dedef8;
box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
<span class = "hidden-lg">Large</span>
<span class = "visible-lg">✔ Visible on large</span>
</div>
</div>
</div>
Checkmarks 指示元素在当前视窗中可见。
Bootstrap - Glyphicons
本章将说明 Glyphicons、其用法以及一些范例。Bootstrap 汇整 200 个字形符号(以字体格式)。现在让我们了解什么是 Glyphicons。
What are Glyphicons?
Glyphicons 是您可以用在网页专案中的图示字体。 Glyphicons Halflings 不是免费的且需要授权,但是它们的创作者已免费提供给 Bootstrap 专案使用。
Where to find Glyphicons?
现在我们已经下载了 Bootstrap 3.x 版本,也从章节 Environment Setup 了解其目录结构。Glyphicons 可以从 fonts 资料夹中找到。这项包含以下文件:
-
glyphicons-halflings-regular.eot
-
glyphicons-halflings-regular.svg
-
glyphicons-halflings-regular.ttf
-
glyphicons-halflings-regular.woff
相关 CSS 规则存在于 dist 资料夹中的 css 资料夹内 bootstrap.css 和 bootstrap-min.css 文件中。您可以在 GLYPHICONS 这个连结看到可用的 Glyphicons。
Usage
要使用图示,只要在代码中的任何地方使用以下代码即可。在图示与文字之间留下空格,以适当填充。
<span class = "glyphicon glyphicon-search"></span>
以下示例演示了这一点−
<p>
<button type = "button" class = "btn btn-default">
<span class = "glyphicon glyphicon-sort-by-attributes"></span>
</button>
<button type = "button" class = "btn btn-default">
<span class = "glyphicon glyphicon-sort-by-attributes-alt"></span>
</button>
<button type = "button" class = "btn btn-default">
<span class = "glyphicon glyphicon-sort-by-order"></span>
</button>
<button type = "button" class = "btn btn-default">
<span class = "glyphicon glyphicon-sort-by-order-alt"></span>
</button>
</p>
<button type = "button" class = "btn btn-default btn-lg">
<span class = "glyphicon glyphicon-user"></span>
User
</button>
<button type = "button" class = "btn btn-default btn-sm">
<span class = "glyphicon glyphicon-user"></span>
User
</button>
<button type ="button" class = "btn btn-default btn-xs">
<span class = "glyphicon glyphicon-user"></span>
User
</button>
Bootstrap - Dropdowns
本章将说明 Bootstrap 下拉式菜单。下拉式菜单是可以切换的,用以清单格式显示连结的情境式菜单。这可以使用 dropdown JavaScript plugin 产生互动性。
要使用下拉式菜单,只要将下拉式菜单包装在类别 .dropdown 中。以下范例展示基本下拉式菜单:
<div class = "dropdown">
<button type = "button" class = "btn dropdown-toggle" id = "dropdownMenu1" data-toggle = "dropdown">
Topics
<span class = "caret"></span>
</button>
<ul class = "dropdown-menu" role = "menu" aria-labelledby = "dropdownMenu1">
<li role = "presentation">
<a role = "menuitem" tabindex = "-1" href = "#">Java</a>
</li>
<li role = "presentation">
<a role = "menuitem" tabindex = "-1" href = "#">Data Mining</a>
</li>
<li role = "presentation">
<a role = "menuitem" tabindex = "-1" href = "#">
Data Communication/Networking
</a>
</li>
<li role = "presentation" class = "divider"></li>
<li role = "presentation">
<a role = "menuitem" tabindex = "-1" href = "#">Separated link</a>
</li>
</ul>
</div>
Options
Alignment
透过在 .dropdown-menu 中加入类别 .pull-right ,将下拉式菜单对齐到右边。以下范例展示这样的作法:
<div class = "dropdown">
<button type = "button" class = "btn dropdown-toggle" id = "dropdownMenu1" data-toggle = "dropdown">
Topics
<span class = "caret"></span>
</button>
<ul class = "dropdown-menu pull-right" role = "menu" aria-labelledby = "dropdownMenu1">
<li role = "presentation">
<a role = "menuitem" tabindex = "-1" href = "#">Java</a>
</li>
<li role = "presentation">
<a role = "menuitem" tabindex = "-1" href = "#">Data Mining</a>
</li>
<li role = "presentation">
<a role = "menuitem" tabindex = "-1" href = "#">
Data Communication/Networking
</a>
</li>
<li role = "presentation" class = "divider"></li>
<li role = "presentation">
<a role = "menuitem" tabindex = "-1" href = "#">Separated link</a>
</li>
</ul>
</div>
Headers
您可以在任何下拉式菜单中,使用类别 dropdown-header 为动作的部分加上标签。以下范例展示这样的作法:
<div class = "dropdown">
<button type = "button" class = "btn dropdown-toggle" id = "dropdownMenu1" data-toggle = "dropdown">
Topics
<span class = "caret"></span>
</button>
<ul class = "dropdown-menu" role = "menu" aria-labelledby = "dropdownMenu1">
<li role = "presentation" class = "dropdown-header">Dropdown header</li>
<li role = "presentation" >
<a role = "menuitem" tabindex = "-1" href = "#">Java</a>
</li>
<li role = "presentation">
<a role = "menuitem" tabindex = "-1" href = "#">Data Mining</a>
</li>
<li role = "presentation">
<a role = "menuitem" tabindex = "-1" href = "#">
Data Communication/Networking
</a>
</li>
<li role = "presentation" class = "divider"></li>
<li role = "presentation" class = "dropdown-header">Dropdown header</li>
<li role = "presentation">
<a role = "menuitem" tabindex = "-1" href = "#">Separated link</a>
</li>
</ul>
</div>
Bootstrap - Button Groups
按钮群组允许多个按钮堆叠在单一行上。当您想将诸如对齐按钮之类的项目置在一起时,这会有用。您可以使用 Bootstrap Button Plugin 加入选择性的 JavaScript 单选按钮和核取方块样式的行为。
以下表格总结了 Bootstrap 提供来供按钮群组使用的重要类别:
Class |
Description |
Code Sample |
.btn-group |
此类别用于基本按钮群组。将一系列按钮与类别 .btn 包装在 .btn-group 中。 |
<div class = "btn-group"> <button type = "button" class = "btn btn-default">Button1</button> <button type = "button" class = "btn btn-default">Button2</button></div> |
.btn-toolbar |
这有助于将 <div class = "btn-group"> 集合并成一个 <div class = "btn-toolbar">,以提供更复杂的组件。 |
<div class = "btn-toolbar" role = "toolbar"> <div class = "btn-group">…</div> <div class = "btn-group">…</div></div> |
.btn-group-lg, .btn-group-sm, .btn-group-xs |
这些类别可以套用在按钮群组,而不是调整每个按钮的大小。 |
<div class = "btn-group btn-group-lg">…</div><div class = "btn-group btn-group-sm">…</div><div class = "btn-group btn-group-xs">…</div> |
.btn-group-vertical |
此类使一组按钮垂直堆叠显示,而不是水平显示。 |
<div class = "btn-group-vertical"> …</div> |
Basic Button Group
以下示例演示了上述表中讨论的类 .btn-group 的用法 −
<div class = "btn-group">
<button type = "button" class = "btn btn-default">Button 1</button>
<button type = "button" class = "btn btn-default">Button 2</button>
<button type = "button" class = "btn btn-default">Button 3</button>
</div>
Button Toolbar
以下示例演示了上述表中讨论的类 .btn-toolbar 的用法 −
<div class = "btn-toolbar" role = "toolbar">
<div class = "btn-group">
<button type = "button" class = "btn btn-default">Button 1</button>
<button type = "button" class = "btn btn-default">Button 2</button>
<button type = "button" class = "btn btn-default">Button 3</button>
</div>
<div class = "btn-group">
<button type = "button" class = "btn btn-default">Button 4</button>
<button type = "button" class = "btn btn-default">Button 5</button>
<button type = "button" class = "btn btn-default">Button 6</button>
</div>
<div class = "btn-group">
<button type = "button" class = "btn btn-default">Button 7</button>
<button type = "button" class = "btn btn-default">Button 8</button>
<button type = "button" class = "btn btn-default">Button 9</button>
</div>
</div>
Button Size
以下示例演示了上述表中讨论的类 .btn-group- * 的用法 −
<div class = "btn-group btn-group-lg">
<button type = "button" class = "btn btn-default">Button 1</button>
<button type = "button" class = "btn btn-default">Button 2</button>
<button type = "button" class = "btn btn-default">Button 3</button>
</div>
<div class = "btn-group btn-group-sm">
<button type = "button" class = "btn btn-default">Button 4</button>
<button type = "button" class = "btn btn-default">Button 5</button>
<button type = "button" class = "btn btn-default">Button 6</button>
</div>
<div class = "btn-group btn-group-xs">
<button type = "button" class = "btn btn-default">Button 7</button>
<button type = "button" class = "btn btn-default">Button 8</button>
<button type = "button" class = "btn btn-default">Button 9</button>
</div>
Nesting
可以在另一个按钮组中嵌套按钮组,即在另一个 .btn-group * 中放置 *.btn-group 。这样做时要将下拉菜单与一系列按钮混合在一起。
<div class = "btn-group">
<button type = "button" class = "btn btn-default">Button 1</button>
<button type = "button" class = "btn btn-default">Button 2</button>
<div class = "btn-group">
<button type = "button" class = "btn btn-default dropdown-toggle" data-toggle = "dropdown">
Dropdown
<span class = "caret"></span>
</button>
<ul class = "dropdown-menu">
<li><a href = "#">Dropdown link 1</a></li>
<li><a href = "#">Dropdown link 2</a></li>
</ul>
</div>
</div>
Vertical Buttongroup
以下示例演示了上述表中讨论的类 .btn-group-vertical 的用法 −
<div class = "btn-group-vertical">
<button type = "button" class = "btn btn-default">Button 1</button>
<button type = "button" class = "btn btn-default">Button 2</button>
<div class = "btn-group-vertical">
<button type = "button" class = "btn btn-default dropdown-toggle" data-toggle = "dropdown">
Dropdown
<span class = "caret"></span>
</button>
<ul class = "dropdown-menu">
<li><a href = "#">Dropdown link 1</a></li>
<li><a href = "#">Dropdown link 2</a></li>
</ul>
</div>
</div>
Bootstrap - Button Dropdowns
本章将讨论如何使用 Bootstrap 类为按钮添加下拉菜单。要为按钮添加下拉菜单,只需将按钮和下拉菜单包装在 .btn-group 中。你也可以使用 <span class = "caret"></span> 作为指示器,表示该按钮是一个下拉菜单。
以下示例演示了基本单按钮下拉菜单 −
<div class = "btn-group">
<button type = "button" class = "btn btn-default dropdown-toggle" data-toggle = "dropdown">
Default
<span class = "caret"></span>
</button>
<ul class = "dropdown-menu" role = "menu">
<li><a href = "#">Action</a></li>
<li><a href = "#">Another action</a></li>
<li><a href = "#">Something else here</a></li>
<li class = "divider"></li>
<li><a href = "#">Separated link</a></li>
</ul>
</div>
<div class = "btn-group">
<button type = "button" class = "btn btn-primary dropdown-toggle" data-toggle = "dropdown">
Primary
<span class = "caret"></span>
</button>
<ul class = "dropdown-menu" role = "menu">
<li><a href = "#">Action</a></li>
<li><a href = "#">Another action</a></li>
<li><a href = "#">Something else here</a></li>
<li class = "divider"></li>
<li><a href = "#">Separated link</a></li>
</ul>
</div>
Split Button Dropdowns
拆分按钮下拉菜单使用与下拉按钮相同的一般样式,但在下拉菜单中添加了一个主要操作。拆分按钮左侧有主要操作,右侧有显示下拉菜单的切换按钮。
<div class = "btn-group">
<button type = "button" class = "btn btn-default">Default</button>
<button type = "button" class = "btn btn-default dropdown-toggle" data-toggle = "dropdown">
<span class = "caret"></span>
<span class = "sr-only">Toggle Dropdown</span>
</button>
<ul class = "dropdown-menu" role = "menu">
<li><a href = "#">Action</a></li>
<li><a href = "#">Another action</a></li>
<li><a href = "#">Something else here</a></li>
<li class = "divider"></li>
<li><a href = "#">Separated link</a></li>
</ul>
</div>
<div class = "btn-group">
<button type = "button" class = "btn btn-primary">Primary</button>
<button type = "button" class = "btn btn-primary dropdown-toggle" data-toggle = "dropdown">
<span class = "caret"></span>
<span class = "sr-only">Toggle Dropdown</span>
</button>
<ul class = "dropdown-menu" role = "menu">
<li><a href = "#">Action</a></li>
<li><a href = "#">Another action</a></li>
<li><a href = "#">Something else here</a></li>
<li class = "divider"></li>
<li><a href = "#">Separated link</a></li>
</ul>
</div>
Button Dropdown Size
你可以对任何按钮大小使用下拉菜单 − .btn-large, .btn-sm, *or *.btn-xs 。
<div class = "btn-group">
<button type = "button" class = "btn btn-default dropdown-toggle btn-lg" data-toggle = "dropdown">
Default
<span class = "caret"></span>
</button>
<ul class = "dropdown-menu" role = "menu">
<li><a href = "#">Action</a></li>
<li><a href = "#">Another action</a></li>
<li><a href = "#">Something else here</a></li>
<li class = "divider"></li>
<li><a href = "#">Separated link</a></li>
</ul>
</div>
<div class = "btn-group">
<button type = "button" class = "btn btn-primary dropdown-toggle btn-sm" data-toggle = "dropdown">
Primary
<span class = "caret"></span>
</button>
<ul class = "dropdown-menu" role = "menu">
<li><a href = "#">Action</a></li>
<li><a href = "#">Another action</a></li>
<li><a href = "#">Something else here</a></li>
<li class = "divider"></li>
<li><a href = "#">Separated link</a></li>
</ul>
</div>
<div class = "btn-group">
<button type = "button" class = "btn btn-success dropdown-toggle btn-xs" data-toggle = "dropdown">
Success
<span class = "caret"></span>
</button>
<ul class = "dropdown-menu" role = "menu">
<li><a href = "#">Action</a></li>
<li><a href = "#">Another action</a></li>
<li><a href = "#">Something else here</a></li>
<li class = "divider"></li>
<li><a href = "#">Separated link</a></li>
</ul>
</div>
Dropup Variation
可以构建向上而不是向下拉开的菜单。要实现此目的,只需将 .dropup 添加到父 .btn-group 容器。
<div class = "row" style = "margin-left:50px; margin-top:200px">
<div class = "btn-group dropup">
<button type = "button" class = "btn btn-default dropdown-toggle" data-toggle = "dropdown">
Default
<span class = "caret"></span>
</button>
<ul class = "dropdown-menu" role = "menu">
<li><a href = "#">Action</a></li>
<li><a href = "#">Another action</a></li>
<li><a href = "#">Something else here</a></li>
<li class = "divider"></li>
<li><a href = "#">Separated link</a></li>
</ul>
</div>
<div class = "btn-group dropup">
<button type = "button" class = "btn btn-primary dropdown-toggle" data-toggle = "dropdown">
Primary
<span class = "caret"></span>
</button>
<ul class = "dropdown-menu" role = "menu">
<li><a href = "#">Action</a></li>
<li><a href = "#">Another action</a></li>
<li><a href = "#">Something else here</a></li>
<li class = "divider"></li>
<li><a href = "#">Separated link</a></li>
</ul>
</div>
</div>
Bootstrap - Input Groups
本章介绍 Bootstrap 支持的另一项功能,即输入组。输入组扩展了 Form Controls 。使用输入组,你可以轻松地在基于文本的输入前面或后面添加文本或按钮。
通过在输入字段中添加前置和后置内容,可以将通用元素添加到用户输入的内容中。例如,你可以添加美元符号、用于 Twitter 用户名的@符号,或任何其他应用程序界面中常见的元素。
要在 .form-control 中添加前置或后置元素,请执行以下操作:
-
将其包装在 class 为 .input-group 的 <div> 元素中
-
作为下一步,在同一个 <div> 元素中,将你的额外内容放置在 class 为 .input-group-addon 的 <span> 元素中。
-
现在将此 <span> 元素放置在 <input> 元素之前或之后。
Basic Input Group
以下示例演示了基本输入组 −
<div style = "padding: 100px 100px 10px;">
<form class = "bs-example bs-example-form" role = "form">
<div class = "input-group">
<span class = "input-group-addon">@</span>
<input type = "text" class = "form-control" placeholder = "twitterhandle">
</div>
<br>
<div class = "input-group">
<input type = "text" class = "form-control">
<span class = "input-group-addon">.00</span>
</div>
<br>
<div class = "input-group">
<span class = "input-group-addon">$</span>
<input type = "text" class =" form-control">
<span class = "input-group-addon">.00</span>
</div>
</form>
</div>
Input Group Sizing
可以通过为 .input-group 本身添加相对大小类的尺寸类,例如 .input-group-lg, input-group-sm, input-group-xs ,来更改输入组的大小。其中内容会自动调整大小。
以下示例演示了此操作:
<div style = "padding: 100px 100px 10px;">
<form class = "bs-example bs-example-form" role = "form">
<div class = "input-group input-group-lg">
<span class = "input-group-addon">@</span>
<input type = "text" class = "form-control" placeholder = "Twitterhandle">
</div>
<br>
<div class = "input-group">
<span class = "input-group-addon">@</span>
<input type = "text" class = "form-control" placeholder = "Twitterhandle">
</div>
<br>
<div class = "input-group input-group-sm">
<span class = "input-group-addon">@</span>
<input type = "text" class = "form-control" placeholder = "Twitterhandle">
</div>
</form>
</div>
Checkboxes and Radio Addons
可以添加或附加单选按钮和复选框,而不是文本,如下例所示:
<div style = "padding: 100px 100px 10px;">
<form class = "bs-example bs-example-form" role = "form">
<div class = "row">
<div class = "col-lg-6">
<div class = "input-group">
<span class = "input-group-addon">
<input type = "checkbox">
</span>
<input type = "text" class = "form-control">
</div><!-- /input-group -->
</div><!-- /.col-lg-6 --><br>
<div class = "col-lg-6">
<div class = "input-group">
<span class = "input-group-addon">
<input type = "radio">
</span>
<input type = "text" class = "form-control">
</div><!-- /input-group -->
</div><!-- /.col-lg-6 -->
</div><!-- /.row -->
</form>
</div>
Button Addons
你甚至可以在输入组中添加或附加按钮。除了 .input-group-addon 类,还需要使用 .input-group-btn 类来包装按钮。这是由于默认的浏览器样式无法覆盖。以下示例演示了此操作:
<div style = "padding: 100px 100px 10px;">
<form class = "bs-example bs-example-form" role = "form">
<div class = "row">
<div class = "col-lg-6">
<div class = "input-group">
<span class = "input-group-btn">
<button class = "btn btn-default" type = "button">
Go!
</button>
</span>
<input type = "text" class = "form-control">
</div><!-- /input-group -->
</div><!-- /.col-lg-6 --><br>
<div class = "col-lg-6">
<div class = "input-group">
<input type = "text" class = "form-control">
<span class = "input-group-btn">
<button class = "btn btn-default" type = "button">
Go!
</button>
</span>
</div><!-- /input-group -->
</div><!-- /.col-lg-6 -->
</div><!-- /.row -->
</form>
</div>
Buttons with Dropdowns
可以通过简单地将按钮和下拉菜单包装在 .input-group-btn 类中,来在输入组中添加带下拉菜单的按钮,如下例所示:
<div style = "padding: 100px 100px 10px;">
<form class = "bs-example bs-example-form" role = "form">
<div class = "row">
<div class = "col-lg-6">
<div class = "input-group">
<div class = "input-group-btn">
<button type = "button" class = "btn btn-default dropdown-toggle"
data-toggle = "dropdown">
DropdownMenu
<span class = "caret"></span>
</button>
<ul class = "dropdown-menu">
<li><a href = "#">Action</a></li>
<li><a href = "#">Another action</a></li>
<li><a href = "#">Something else here</a></li>
<li class = "divider"></li>
<li><a href = "#">Separated link</a></li>
</ul>
</div><!-- /btn-group -->
<input type = "text" class = "form-control">
</div><!-- /input-group -->
</div><!-- /.col-lg-6 --><br>
<div class = "col-lg-6">
<div class = "input-group">
<input type = "text" class = "form-control">
<div class = "input-group-btn">
<button type = "button" class = "btn btn-default dropdown-toggle"
data-toggle = "dropdown">
DropdownMenu
<span class = "caret"></span>
</button>
<ul class = "dropdown-menu pull-right">
<li><a href = "#">Action</a></li>
<li><a href = "#">Another action</a></li>
<li><a href = "#">Something else here</a></li>
<li class = "divider"></li>
<li><a href = "#">Separated link</a></li>
</ul>
</div><!-- /btn-group -->
</div><!-- /input-group -->
</div><!-- /.col-lg-6 -->
</div><!-- /.row -->
</form>
</div>
Segmented Buttons
要分割输入组中的按钮下拉菜单,请使用与下拉按钮相同的通用样式,但添加一个主要操作以及下拉菜单,如以下示例所示:
<div style = "padding: 100px 100px 10px;">
<form class = "bs-example bs-example-form" role = "form">
<div class = "row">
<div class = "col-lg-6">
<div class = "input-group">
<div class = "input-group-btn">
<button type = "button" class = "btn btn-default"
tabindex = "-1">Dropdown Menu</button>
<button type = "button" class = "btn btn-default dropdown-toggle"
data-toggle = "dropdown" tabindex = "-1">
<span class = "caret"></span>
<span class = "sr-only">Toggle Dropdown</span>
</button>
<ul class = "dropdown-menu">
<li><a href = "#">Action</a></li>
<li><a href ="#">Another action</a></li>
<li><a href = "#">Something else here</a></li>
<li class = "divider"></li>
<li><a href = "#">Separated link</a></li>
</ul>
</div><!-- /btn-group -->
<input type = "text" class = "form-control">
</div><!-- /input-group -->
</div><!-- /.col-lg-6 --><br>
<div class = "col-lg-6">
<div class = "input-group">
<input type = "text" class = "form-control">
<div class = "input-group-btn">
<button type = "button" class = "btn btn-default" tabindex = "-1">
Dropdown Menu
</button>
<button type = "button" class = "btn btn-default dropdown-toggle"
data-toggle = "dropdown" tabindex = "-1">
<span class = "caret"></span>
<span class = "sr-only">Toggle Dropdown</span>
</button>
<ul class = "dropdown-menu pull-right">
<li><a href = "#">Action</a></li>
<li><a href = "#">Another action</a></li>
<li><a href = "#">Something else here</a></li>
<li class = "divider"></li>
<li><a href = "#">Separated link</a></li>
</ul>
</div><!-- /btn-group -->
</div><!-- /input-group -->
</div><!-- /.col-lg-6 -->
</div><!-- /.row -->
</form>
</div>
Bootstrap - Navigation Elements
Bootstrap 为导航元素的样式提供了几种不同的选项。所有这些都共享相同的标记和基本类 .nav 。Bootstrap 还提供一个辅助类,以共享标记和状态。交换修饰符类以在每种样式之间切换。
Tabular Navigation or Tabs
创建选项卡式导航菜单:
-
从具有基本类 .nav 的基本无序列表开始
-
Add class .nav-tabs.
以下示例演示了这一点−
<p>Tabs Example</p>
<ul class = "nav nav-tabs">
<li class = "active"><a href = "#">Home</a></li>
<li><a href = "#">SVN</a></li>
<li><a href = "#">iOS</a></li>
<li><a href = "#">VB.Net</a></li>
<li><a href = "#">Java</a></li>
<li><a href = "#">PHP</a></li>
</ul>
Pills Navigation
Basic Pills
要将选项卡变成药丸,请按照上述相同的步骤执行,使用类 .nav-pills 代替 .nav-tabs 。
以下示例演示了这一点−
<p>Pills Example</p>
<ul class = "nav nav-pills">
<li class = "active"><a href = "#">Home</a></li>
<li><a href = "#">SVN</a></li>
<li><a href = "#">iOS</a></li>
<li><a href = "#">VB.Net</a></li>
<li><a href = "#">Java</a></li>
<li><a href = "#">PHP</a></li>
</ul>
Vertical Pills
你可以使用类 .nav-stacked 以及类 .nav, .nav-pills 垂直堆叠药丸。
以下示例演示了这一点−
<p>Vertical Pills Example</p>
<ul class = "nav nav-pills nav-stacked">
<li class = "active"><a href = "#">Home</a></li>
<li><a href = "#">SVN</a></li>
<li><a href = "#">iOS</a></li>
<li><a href = "#">VB.Net</a></li>
<li><a href = "#">Java</a></li>
<li><a href = "#">PHP</a></li>
</ul>
Justified Nav
使用类 .nav-justified 以及 .nav, .nav-tabs 或 .nav, .nav-pills ,分别在屏幕宽度大于 768 个像素时,可以使选项卡或药丸与它们的父级等宽。在较小的屏幕上,导航链接被堆叠。
以下示例演示了这一点−
<p>Justified Nav Elements Example</p>
<ul class = "nav nav-pills nav-justified">
<li class = "active"><a href = "#">Home</a></li>
<li><a href = "#">SVN</a></li>
<li><a href = "#">iOS</a></li>
<li><a href = "#">VB.Net</a></li>
<li><a href = "#">Java</a></li>
<li><a href = "#">PHP</a></li>
</ul>
<br>
<br>
<br>
<ul class = "nav nav-tabs nav-justified">
<li class = "active"><a href = "#">Home</a></li>
<li><a href = "#">SVN</a></li>
<li><a href = "#">iOS</a></li>
<li><a href = "#">VB.Net</a></li>
<li><a href = "#">Java</a></li>
<li><a href = "#">PHP</a></li>
</ul>
Disabled Links
对于每个 .nav 类,如果添加 .disabled 类,它将创建一个灰色链接,它还会禁用 :hover 状态,如下例所示:
<p>Disabled Link Example</p>
<ul class = "nav nav-pills">
<li class = "active"><a href = "#">Home</a></li>
<li><a href = "#">SVN</a></li>
<li class = "disabled"><a href = "#">iOS(disabled link)</a></li>
<li><a href = "#">VB.Net</a></li>
<li><a href = "#">Java</a></li>
<li><a href = "#">PHP</a></li>
</ul>
<br>
<br>
<ul class = "nav nav-tabs">
<li class = "active"><a href = "#">Home</a></li>
<li><a href = "#">SVN</a></li>
<li><a href = "#">iOS</a></li>
<li class = "disabled"><a href = "#">VB.Net(disabled link)</a></li>
<li><a href = "#">Java</a></li>
<li><a href = "#">PHP</a></li>
</ul>
Dropdowns
导航菜单与下拉菜单共享相似的语法。默认情况下,你有一个列表项,该列表项具有一个锚点,与某些 data-attributes 结合使用,以触发一个具有 .dropdown-menu 类的无序列表。
Tabs with Dropdowns
向标签添加下拉列表:
-
从具有基本类 .nav 的基本无序列表开始
-
Add the class .nav-tabs.
-
现在添加一个具有 .dropdown-menu 类的无序列表。
<p>Tabs With Dropdown Example</p>
<ul class = "nav nav-tabs">
<li class = "active"><a href = "#">Home</a></li>
<li><a href = "#">SVN</a></li>
<li><a href = "#">iOS</a></li>
<li><a href = "#">VB.Net</a></li>
<li class = "dropdown">
<a class = "dropdown-toggle" data-toggle = "dropdown" href = "#">
Java
<span class = "caret"></span>
</a>
<ul class = "dropdown-menu">
<li><a href = "#">Swing</a></li>
<li><a href = "#">jMeter</a></li>
<li><a href = "#">EJB</a></li>
<li class = "divider"></li>
<li><a href = "#">Separated link</a></li>
</ul>
</li>
<li><a href = "#">PHP</a></li>
</ul>
Pills with Dropdowns
要对药丸执行相同操作,只需将 .nav-tabs 类与 .nav-pills 交换,如下例所示。
<p>Pills With Dropdown Example</p>
<ul class = "nav nav-pills">
<li class = "active"><a href = "#">Home</a></li>
<li><a href = "#">SVN</a></li>
<li><a href = "#">iOS</a></li>
<li><a href = "#">VB.Net</a></li>
<li class = "dropdown">
<a class = "dropdown-toggle" data-toggle = "dropdown" href = "#">
Java <span class = "caret"></span>
</a>
<ul class = "dropdown-menu">
<li><a href = "#">Swing</a></li>
<li><a href = "#">jMeter</a></li>
<li><a href = "#">EJB</a></li>
<li class = "divider"></li>
<li><a href = "#">Separated link</a></li>
</ul>
</li>
<li><a href = "#">PHP</a></li>
</ul>
Bootstrap - Navbar
导航栏是 Bootstrap 站点的重要功能之一。导航栏是响应式的“元”组件,用作应用程序或网站的导航头。导航栏在移动视图中折叠,并随着可用的视口宽度增加而变为水平。其核心是,导航栏包括为站点名称和基本导航设置样式。
Default Navbar
创建默认导航栏:
-
将 .navbar, .navbar-default 类添加到 <nav> 标签。
-
在上面的元素中添加 role = "navigation" ,以有助于辅助功能。
-
向 <div> 元素添加一个标题类 .navbar-header 。包含一个带 navbar-brand 类的 <a> 元素。这会使文本大小略大。
-
若要向导航栏添加链接,只需添加一个未排序列表,其类为 .nav, .navbar-nav 。
以下示例演示了这一点−
<nav class = "navbar navbar-default" role = "navigation">
<div class = "navbar-header">
<a class = "navbar-brand" href = "#">TutorialsPoint</a>
</div>
<div>
<ul class = "nav navbar-nav">
<li class = "active"><a href = "#">iOS</a></li>
<li><a href = "#">SVN</a></li>
<li class = "dropdown">
<a href = "#" class = "dropdown-toggle" data-toggle = "dropdown">
Java
<b class = "caret"></b>
</a>
<ul class = "dropdown-menu">
<li><a href = "#">jmeter</a></li>
<li><a href = "#">EJB</a></li>
<li><a href = "#">Jasper Report</a></li>
<li class = "divider"></li>
<li><a href = "#">Separated link</a></li>
<li class = "divider"></li>
<li><a href = "#">One more separated link</a></li>
</ul>
</li>
</ul>
</div>
</nav>
Responsive Navbar
若要向导航栏添加响应式功能,则需要将你想折叠的内容包装在一个 <div> 中,其类为 .collapse, .navbar-collapse 。折叠特性是由一个带有 .navbar-toggle 类的按钮触发的,然后具有两个 data- 元素。第一个 data-toggle 用于告诉 JavaScript 按钮应执行什么操作,而第二个 data-target 则指示要切换哪个元素。然后,使用一个类 .icon-bar 创建我们称之为汉堡包按钮的内容。这将切换 .nav-collapse <div> 中的元素。此功能需要包括 Bootstrap Collapse Plugin 才能工作。
以下示例演示了这一点−
<nav class = "navbar navbar-default" role = "navigation">
<div class = "navbar-header">
<button type = "button" class = "navbar-toggle"
data-toggle = "collapse" data-target = "#example-navbar-collapse">
<span class = "sr-only">Toggle navigation</span>
<span class = "icon-bar"></span>
<span class = "icon-bar"></span>
<span class = "icon-bar"></span>
</button>
<a class = "navbar-brand" href = "#">TutorialsPoint</a>
</div>
<div class = "collapse navbar-collapse" id = "example-navbar-collapse">
<ul class = "nav navbar-nav">
<li class = "active"><a href = "#">iOS</a></li>
<li><a href = "#">SVN</a></li>
<li class = "dropdown">
<a href = "#" class = "dropdown-toggle" data-toggle = "dropdown">
Java
<b class = "caret"></b>
</a>
<ul class = "dropdown-menu">
<li><a href = "#">jmeter</a></li>
<li><a href = "#">EJB</a></li>
<li><a href = "#">Jasper Report</a></li>
<li class = "divider"></li>
<li><a href = "#">Separated link</a></li>
<li class = "divider"></li>
<li><a href = "#">One more separated link</a></li>
</ul>
</li>
</ul>
</div>
</nav>
Forms in Navbar
不要使用第 Bootstrap Forms 章中的默认基于类的表单,而是使用出现在导航栏中的 .navbar-form 类表单。这样可以确保表单在狭窄视口中拥有适当的垂直对齐方式和折叠动作。使用对齐选项(在“组件对齐”部分中解释)来决定它位于导航栏内容中的哪个位置。
以下示例演示了这一点−
<nav class = "navbar navbar-default" role = "navigation">
<div class = "navbar-header">
<a class = "navbar-brand" href = "#">TutorialsPoint</a>
</div>
<div>
<form class = "navbar-form navbar-left" role = "search">
<div class = "form-group">
<input type = "text" class = "form-control" placeholder = "Search">
</div>
<button type = "submit" class = "btn btn-default">Submit</button>
</form>
</div>
</nav>
Buttons in Navbar
你可以使用类 .navbar-btn 将按钮添加到不位于 <form> 中的 <button> 元素,以将其垂直居中到导航栏。 .navbar-btn 可以用于 <a> 和 <input> 元素。
以下示例演示了这一点−
<nav class = "navbar navbar-default" role = "navigation">
<div class = "navbar-header">
<a class = "navbar-brand" href = "#">TutorialsPoint</a>
</div>
<div>
<form class = "navbar-form navbar-left" role = "search">
<div class = "form-group">
<input type = "text" class = "form-control" placeholder = "Search">
</div>
<button type = "submit" class = "btn btn-default">Submit Button</button>
</form>
<button type = "button" class = "btn btn-default navbar-btn">Navbar Button</button>
</div>
</nav>
Text in Navbar
若要将字符串文本包装在元素中,请使用类 .navbar-text 。这通常与 <p> 标记一起使用,以获得正确的行距和颜色。以下示例演示了此操作:
<nav class = "navbar navbar-default" role = "navigation">
<div class = "navbar-header">
<a class = "navbar-brand" href = "#">TutorialsPoint</a>
</div>
<div>
<p class = "navbar-text">Signed in as Thomas</p>
</div>
</nav>
Non-nav Links
如果你想使用不在常规导航栏导航组件中的标准链接,则使用类 navbar-link 为默认导航栏选项和反向导航栏选项添加适当的颜色,如以下示例所示:
<nav class = "navbar navbar-default" role = "navigation">
<div class = "navbar-header">
<a class = "navbar-brand" href = "#">TutorialsPoint</a>
</div>
<div>
<p class = "navbar-text navbar-right">
Signed in as
<a href = "#" class = "navbar-link">Thomas</a>
</p>
</div>
</nav>
Component Alignment
你可以使用实用程序类 .navbar-left 或 .navbar-right 将导航链接、表单、按钮或文本等组件在导航栏中左对齐或右对齐。这两个类都将在指定的方向上添加 CSS 浮动。以下示例演示了此操作:
<nav class = "navbar navbar-default" role = "navigation">
<div class = "navbar-header">
<a class = "navbar-brand" href = "#">TutorialsPoint</a>
</div>
<div>
<!--Left Align-->
<ul class = "nav navbar-nav navbar-left">
<li class = "dropdown">
<a href = "#" class = "dropdown-toggle" data-toggle = "dropdown">
Java
<b class = "caret"></b>
</a>
<ul class = "dropdown-menu">
<li><a href = "#">jmeter</a></li>
<li><a href = "#">EJB</a></li>
<li><a href = "#">Jasper Report</a></li>
<li class = "divider"></li>
<li><a href = "#">Separated link</a></li>
<li class = "divider"></li>
<li><a href = "#">One more separated link</a></li>
</ul>
</li>
</ul>
<form class = "navbar-form navbar-left" role = "search">
<button type = "submit" class = "btn btn-default">Left align-Submit Button</button>
</form>
<p class = "navbar-text navbar-left">Left align-Text</p>
<!--Right Align-->
<ul class = "nav navbar-nav navbar-right">
<li class = "dropdown">
<a href = "#" class = "dropdown-toggle" data-toggle = "dropdown">
Java
<b class = "caret"></b>
</a>
<ul class = "dropdown-menu">
<li><a href = "#">jmeter</a></li>
<li><a href = "#">EJB</a></li>
<li><a href = "#">Jasper Report</a></li>
<li class = "divider"></li>
<li><a href = "#">Separated link</a></li>
<li class = "divider"></li>
<li><a href = "#">One more separated link</a></li>
</ul>
</li>
</ul>
<form class = "navbar-form navbar-right" role = "search">
<button type = "submit" class = "btn btn-default">
Right align-Submit Button
</button>
</form>
<p class = "navbar-text navbar-right">Right align-Text</p>
</div>
</nav>
Fixed to Top
Bootstrap 导航栏的定位可以是动态的。默认情况下,它是一个块级元素,根据其在 HTML 中的位置进行定位。通过几个帮助程序类,你可以将其放置在页面顶部或底部,或者使其随页面静态滚动。
如果你想将导航栏固定在顶部,则将类 .navbar-fixed-top 添加到 .navbar class 。以下示例演示了此操作:
<nav class = "navbar navbar-default navbar-fixed-top" role = "navigation">
<div class = "navbar-header">
<a class = "navbar-brand" href = "#">TutorialsPoint</a>
</div>
<div>
<ul class = "nav navbar-nav">
<li class = "active"><a href = "#">iOS</a></li>
<li><a href = "#">SVN</a></li>
<li class = "dropdown">
<a href = "#" class = "dropdown-toggle" data-toggle = "dropdown">
Java
<b class = "caret"></b>
</a>
<ul class = "dropdown-menu">
<li><a href = "#">jmeter</a></li>
<li><a href = "#">EJB</a></li>
<li><a href = "#">Jasper Report</a></li>
<li class = "divider"></li>
<li><a href = "#">Separated link</a></li>
<li class = "divider"></li>
<li><a href = "#">One more separated link</a></li>
</ul>
</li>
</ul>
</div>
</nav>
Fixed to Bottom
如果你想将导航栏固定在页面的底部,请将类 .navbar-fixed-bottom 添加到 .navbar class 。以下示例演示了此操作:
<nav class = "navbar navbar-default navbar-fixed-bottom" role = "navigation">
<div class = "navbar-header">
<a class = "navbar-brand" href = "#">TutorialsPoint</a>
</div>
<div>
<ul class = "nav navbar-nav">
<li class = "active"><a href = "#">iOS</a></li>
<li><a href="#">SVN</a></li>
<li class = "dropdown">
<a href = "#" class = "dropdown-toggle" data-toggle = "dropdown">
Java
<b class ="caret"></b>
</a>
<ul class = "dropdown-menu">
<li><a href = "#">jmeter</a></li>
<li><a href = "#">EJB</a></li>
<li><a href = "#">Jasper Report</a></li>
<li class = "divider"></li>
<li><a href = "#">Separated link</a></li>
<li class = "divider"></li>
<li><a href = "#">One more separated link</a></li>
</ul>
</li>
</ul>
</div>
</nav>
Static Top
若要创建一个随页面滚动的导航栏,请添加 .navbar-static-top 类。此类不需要向 <body> 添加填充。
<nav class = "navbar navbar-default navbar-static-top" role = "navigation">
<div class = "navbar-header">
<a class = "navbar-brand" href = "#">TutorialsPoint</a>
</div>
<div>
<ul class = "nav navbar-nav">
<li class = "active"><a href = "#">iOS</a></li>
<li><a href = "#">SVN</a></li>
<li class = "dropdown">
<a href = "#" class = "dropdown-toggle" data-toggle = "dropdown">
Java
<b class = "caret"></b>
</a>
<ul class = "dropdown-menu">
<li><a href = "#">jmeter</a></li>
<li><a href = "#">EJB</a></li>
<li><a href = "#">Jasper Report</a></li>
<li class = "divider"></li>
<li><a href = "#">Separated link</a></li>
<li class = "divider"></li>
<li><a href = "#">One more separated link</a></li>
</ul>
</li>
</ul>
</div>
</nav>
Inverted Navbar
若要创建一个具有黑色背景和白色文本的反向导航栏,只需将 .navbar-inverse 类添加到 .navbar 类,如下例所示:
<nav class = "navbar navbar-inverse" role = "navigation">
<div class = "navbar-header">
<a class = "navbar-brand" href = "#">TutorialsPoint</a>
</div>
<div>
<ul class = "nav navbar-nav">
<li class = "active"><a href = "#">iOS</a></li>
<li><a href = "#">SVN</a></li>
<li class = "dropdown">
<a href = "#" class = "dropdown-toggle" data-toggle = "dropdown">
Java
<b class = "caret"></b>
</a>
<ul class = "dropdown-menu">
<li><a href = "#">jmeter</a></li>
<li><a href = "#">EJB</a></li>
<li><a href = "#">Jasper Report</a></li>
<li class = "divider"></li>
<li><a href = "#">Separated link</a></li>
<li class = "divider"></li>
<li><a href = "#">One more separated link</a></li>
</ul>
</li>
</ul>
</div>
</nav>
Bootstrap - Breadcrumb
面包屑非常适合显示网站的基于层次结构的信息。对于博客,面包屑可以显示发布日期、类别或标签。它们指出导航层次结构中当前页面的位置。
Bootstrap 中的面包屑只是一个带有类 .breadcrumb 的无序列表。分隔符通过以下类由 CSS(bootstrap.min.css)自动添加:
.breadcrumb > li + li:before {
color: #CCCCCC;
content: "/ ";
padding: 0 5px;
}
以下示例演示了面包屑:
<ol class = "breadcrumb">
<li><a href = "#">Home</a></li>
<li><a href = "#">2013</a></li>
<li class = "active">November</li>
</ol>
Bootstrap - Pagination
本章讨论了 Bootstrap 支持的分界功能。分界是一个无序列表,Bootstrap 处理它就像很多其他界面元素一样。
Pagination
下表列出了 Bootstrap 提供的用于处理分页的类。
Class |
Description |
Sample code |
.pagination |
在此页面添加此类以获取分页。 |
<ul class = "pagination"> <li><a href = " ">«</a></li> <li><a href = " ">1</a></li> …….</ul> |
.disabled, .active |
可以通过使用 .disabled (不可点击的链接)和 .active (指出当前页)来自定义链接。 |
<ul class = "pagination"> <li class = "disabled"><a href = " ">«</a></li> <li class = "active"><a href = " ">1<span class = "sr-only">(current)</span></a></li> …….</ul> |
.pagination-lg, .pagination-sm |
使用这些类可以获取不同大小的条目。 |
<ul class = "pagination pagination-lg">…</ul><ul class = "pagination">…</ul><ul class = "pagination pagination-sm">…</ul> |
Default Pagination
以下示例演示了上表中讨论的类 .pagination 的用法 −
<ul class = "pagination">
<li><a href = "#">«</a></li>
<li><a href = "#">1</a></li>
<li><a href = "#">2</a></li>
<li><a href = "#">3</a></li>
<li><a href = "#">4</a></li>
<li><a href = "#">5</a></li>
<li><a href = "#">»</a></li>
</ul>
States
以下示例演示了上表中讨论的类 .disabled, .active 的用法 −
<ul class = "pagination">
<li><a href = "#">«</a></li>
<li class = "active"><a href = "#">1</a></li>
<li class = "disabled"><a href = "#">2</a></li>
<li><a href = "#">3</a></li>
<li><a href = "#">4</a></li>
<li><a href = "#">5</a></li>
<li><a href = "#">»</a></li>
</ul>
Sizing
以下示例演示了上表中讨论的用于调整 .pagination- * 大小的类的用法 −
<ul class = "pagination pagination-lg">
<li><a href = "#">«</a></li>
<li><a href = "#">1</a></li>
<li><a href = "#">2</a></li>
<li><a href = "#">3</a></li>
<li><a href = "#">4</a></li>
<li><a href = "#">5</a></li>
<li><a href = "#">»</a></li>
</ul>
<br>
<ul class = "pagination">
<li><a href = "#">«</a></li>
<li><a href = "#">1</a></li>
<li><a href = "#">2</a></li>
<li><a href = "#">3</a></li>
<li><a href = "#">4</a></li>
<li><a href = "#">5</a></li>
<li><a href = "#">»</a></li>
</ul>
<br>
<ul class = "pagination pagination-sm">
<li><a href = "#">«</a></li>
<li><a href = "#">1</a></li>
<li><a href = "#">2</a></li>
<li><a href = "#">3</a></li>
<li><a href = "#">4</a></li>
<li><a href = "#">5</a></li>
<li><a href = "#">»</a></li>
</ul>
Pager
如果您需要创建超出文本范围的简单分页链接,分页器将非常适合。和分页链接一样,分页器也是无序列表。默认情况下,链接居中。下表列出了 Bootstrap 为分页器提供的类。
Class |
Description |
Sample code |
.pager |
在此添加此类以获取分页器链接。 |
<ul class = "pager"> <li><a href = " ">Previous</a></li> <li><a href = " ">Next</a></li></ul> |
.previous, .next |
使用类 .previous 向左对齐,使用 .next 向右对齐链接。 |
<ul class = "pager"> <li class = "previous"><a href = "{s10}">Newer →</a></li></ul> |
.disabled |
添加此类以获取静音外观。 |
<ul class = "pager"> <li class = "previous disabled"><a href = " ">← Older</a></li> <li class = "next"><a href = " ">Newer →</a></li></ul> |
Default Pager
以下示例演示了上表中讨论的类 .pager 的用法 −
<ul class = "pager">
<li><a href = "#">Previous</a></li>
<li><a href = "#">Next</a></li>
</ul>
Bootstrap - Labels
本章介绍 Bootstrap 标签。标签非常适合为页面提供计数、提示或其他标记。使用类 .label 来显示标签,如以下示例所示 -
<h1>Example Heading <span class = "label label-default">Label</span></h1>
<h2>Example Heading <span class =" label label-default">Label</span></h2>
<h3>Example Heading <span class = "label label-default">Label</span></h3>
<h4>Example Heading <span class = "label label-default">Label</span></h4>
您可以使用修改器类(例如 label-default, label-primary, label-success, label-info, label-warning, label-danger )来修改标签的外观,如以下示例所示 -
<span class = "label label-default">Default Label</span>
<span class = "label label-primary">Primary Label</span>
<span class = "label label-success">Success Label</span>
<span class = "label label-info">Info Label</span>
<span class = "label label-warning">Warning Label</span>
<span class = "label label-danger">Danger Label</span>
Bootstrap - Badges
本章将讨论 Bootstrap 徽章。徽章类似于标签;主要区别是边角更圆润。
徽章主要用于突出显示新项或未读项。要使用徽章,只需将 <span class = "badge"> 添加到链接、Bootstrap 导航等等。
以下示例演示了这一点−
<a href = "#">Mailbox <span class = "badge">50</span></a>
当没有新项或未读项时,徽章将通过 CSS 的 * :empty* 选择器简单地折叠,前提是其中不存在内容。
Active Nav States
您可以将徽章放置在药丸和列表导航的活动状态中。您可以通过将 <span class = "badge"> 放置到活动链接中来实现此目的,如以下示例所示 -
<h4>Example for Active State in Pill </h4>
<ul class = "nav nav-pills">
<li class = "active"><a href = "#">Home <span class ="badge">42</span></a></li>
<li><a href = "#">Profile</a></li>
<li><a href = "#">Messages <span class = "badge">3</span></a></li>
</ul>
<br>
<h4>Example for Active State in navigations</h4>
<ul class = "nav nav-pills nav-stacked" style = "max-width: 260px;">
<li class = "active">
<a href = "#">
<span class = "badge pull-right">42</span>
Home
</a>
</li>
<li><a href = "#">Profile</a></li>
<li>
<a href = "#">
<span class = "badge pull-right">3</span>
Messages
</a>
</li>
</ul>
Bootstrap - Jumbotron
本章将讨论 Bootstrap 支持的另一项功能 Jumbotron。顾名思义,此组件可以选择增大标题的大小,并为登陆页面内容添加大量空白。如何使用 Jumbotron -
-
使用 .jumbotron 类创建容器 <div>。
-
除了较大的 <h1> 外,字体粗细还减小为 200px。
以下示例演示了这一点−
<div class = "container">
<div class = "jumbotron">
<h1>Welcome to landing page!</h1>
<p>This is an example for jumbotron.</p>
<p>
<a class = "btn btn-primary btn-lg" role = "button">Learn more</a>
</p>
</div>
</div>
要获得一个全宽且没有圆角的 Jumbotron,请在所有 .container 类的外部使用 .jumbotron 类,并改为在内部添加 .container ,如以下示例所示 -
<div class = "jumbotron">
<div class = "container">
<h1>Welcome to landing page!</h1>
<p>This is an example for jumbotron.</p>
<p>
<a class = "btn btn-primary btn-lg" role = "button">Learn more</a>
</p>
</div>
</div>
Bootstrap - Page Header
页面页眉是一个很小的功能,可以在页面上标题周围添加适当的间距。这在这样的网页上特别有用,您可能有几个帖子标题,并且需要一种方法来区分它们。要使用页面页眉,请使用 .page-header 类将标题包装在 <div> 中 -
<div class = "page-header">
<h1>
Example page header
<small>Subtext for header</small>
</h1>
</div>
<p>This is a sample text.This is a sample text.This is a sample text. This is a sample text.</p>
Bootstrap - Thumbnails
本章讨论 Bootstrap 缩略图。许多网站需要一种方法,来以网格布局图像、视频、文本等,而 Bootstrap 提供了一种简单的方法,通过缩略图来实现。如何使用 Bootstrap 创建缩略图 -
-
在图像周围添加一个具有 .thumbnail 类的 <a> 标记。
-
这会添加四像素的填充和灰色边框。
-
在悬停时,动画发光会勾勒出图像。
以下示例演示了默认缩略图 -
<div class = "row">
<div class = "col-sm-6 col-md-3">
<a href = "#" class = "thumbnail">
<img src = "/bootstrap/images/kittens.jpg" alt = "Generic placeholder thumbnail">
</a>
</div>
<div class = "col-sm-6 col-md-3">
<a href = "#" class = "thumbnail">
<img src = "/bootstrap/images/kittens.jpg" alt = "Generic placeholder thumbnail">
</a>
</div>
<div class = "col-sm-6 col-md-3">
<a href = "#" class = "thumbnail">
<img src = "/bootstrap/images/kittens.jpg" alt = "Generic placeholder thumbnail">
</a>
</div>
<div class = "col-sm-6 col-md-3">
<a href = "#" class = "thumbnail">
<img src = "/bootstrap/images/kittens.jpg" alt = "Generic placeholder thumbnail">
</a>
</div>
</div>
Adding Custom Content
现在我们有了基本缩略图,就可以在缩略图中添加标题、段落或按钮等任何种类的 HTML 内容。请按照以下步骤操作 -
-
将具有 .thumbnail 类的 <a> 标记更改为 <div>。
-
在该 <div> 内,您可以添加任何您需要的内容。由于这是一个 <div>,我们可以使用基于 span 的默认命名约定来确定大小。
-
如果您想要对多张图片进行分组,请将它们放在一个无序列表中,则每个列表项都会浮动到左侧。
以下示例演示了这一点−
<div class = "row">
<div class = "col-sm-6 col-md-3">
<div class = "thumbnail">
<img src = "/bootstrap/images/kittens.jpg" alt = "Generic placeholder thumbnail">
</div>
<div class = "caption">
<h3>Thumbnail label</h3>
<p>Some sample text. Some sample text.</p>
<p>
<a href = "#" class = "btn btn-primary" role = "button">
Button
</a>
<a href = "#" class = "btn btn-default" role = "button">
Button
</a>
</p>
</div>
</div>
<div class = "col-sm-6 col-md-3">
<div class = "thumbnail">
<img src = "/bootstrap/images/kittens.jpg" alt = "Generic placeholder thumbnail">
</div>
<div class = "caption">
<h3>Thumbnail label</h3>
<p>Some sample text. Some sample text.</p>
<p>
<a href = "#" class = "btn btn-primary" role = "button">
Button
</a>
<a href = "#" class = "btn btn-default" role = "button">
Button
</a>
</p>
</div>
</div>
<div class = "col-sm-6 col-md-3">
<div class = "thumbnail">
<img src = "/bootstrap/images/kittens.jpg" alt = "Generic placeholder thumbnail">
</div>
<div class = "caption">
<h3>Thumbnail label</h3>
<p>Some sample text. Some sample text.</p>
<p>
<a href = "#" class = "btn btn-primary" role = "button">
Button
</a>
<a href = "#" class = "btn btn-default" role =" button">
Button
</a>
</p>
</div>
</div>
<div class = "col-sm-6 col-md-3">
<div class = "thumbnail">
<img src = "/bootstrap/images/kittens.jpg" alt = "Generic placeholder thumbnail">
</div>
<div class = "caption">
<h3>Thumbnail label</h3>
<p>Some sample text. Some sample text.</p>
<p>
<a href = "#" class = "btn btn-primary" role = "button">
Button
</a>
<a href = "#" class = "btn btn-default" role = "button">
Button
</a>
</p>
</div>
</div>
</div>
Bootstrap - Alerts
本章将讨论警报,以及 Bootstrap 专门为警报提供的类。警报提供了一种向用户发送样式消息的方法。它们提供典型用户操作的上下文反馈消息。
您可以在警报中添加一个可选的关闭图标。在内联消除模式下使用 Alerts jQuery plugin 。
通过创建一个包装器 <div> 并添加一个 .alert 类以及四个上下文类(例如 .alert-success, .alert-info, .alert-warning, .alert-danger )中的一个,您可以添加一个基本警报。以下示例演示了这一点 −
<div class = "alert alert-success">Success! Well done its submitted.</div>
<div class = "alert alert-info">Info! take this info.</div>
<div class = "alert alert-warning">Warning ! Dont submit this.</div>
<div class = "alert alert-danger">Error ! Change few things.</div>
Dismissal Alerts
要构建一个消除警报:
-
通过创建一个包装器 <div> 并添加一个 .alert 类以及四个上下文类(例如 .alert-success, .alert-info, .alert-warning, .alert-danger )中的一个,添加一个基本警报
-
在上述 <div> 类中添加可选的 .alert-dismissable 。
-
Add a close button.
以下示例演示了这一点−
<div class = "alert alert-success alert-dismissable">
<button type = "button" class = "close" data-dismiss = "alert" aria-hidden = "true">
×
</button>
Success! Well done its submitted.
</div>
<div class = "alert alert-info alert-dismissable">
<button type = "button" class = "close" data-dismiss = "alert" aria-hidden = "true">
×
</button>
Info! take this info.
</div>
<div class = "alert alert-warning alert-dismissable">
<button type = "button" class = "close" data-dismiss = "alert" aria-hidden = "true">
×
</button>
Warning ! Dont submit this.
</div>
<div class = "alert alert-danger alert-dismissable">
<button type = "button" class = "close" data-dismiss = "alert" aria-hidden = "true">
×
</button>
Error ! Change few things.
</div>
Links in Alerts
要在警报中实现链接:
-
通过创建一个包装器 <div> 并添加一个 .alert 类以及四个上下文类(例如 .alert-success, .alert-info, .alert-warning, .alert-danger )中的一个,添加一个基本警报
-
使用 .alert-link 实用类可在任何警报中快速提供匹配颜色的链接。
<div class = "alert alert-success">
<a href = "#" class = "alert-link">Success! Well done its submitted.</a>
</div>
<div class = "alert alert-info">
<a href = "#" class = "alert-link">Info! take this info.</a>
</div>
<div class = "alert alert-warning">
<a href = "#" class = "alert-link">Warning ! Dont submit this.</a>
</div>
<div class = "alert alert-danger">
<a href = "#" class = "alert-link">Error ! Change few things.</a>
</div>
Bootstrap - Progress Bars
本章讨论了 Bootstrap 进度条。进度条的目的是显示资产正在加载、处于进行中,或者页面中的元素正在发生操作。
Default Progress Bar
要创建一个基本进度条:
-
添加一个具有 .progress 类的 <div>。
-
接下来,在上述 <div> 内,添加一个具有 .progress-bar 类的空 <div>。
-
添加一个样式属性,其中宽度以百分比表示。例如,style = "60%"; 表示进度条为 60%。
我们来看下面的示例:
<div class = "progress">
<div class = "progress-bar" role = "progressbar" aria-valuenow = "60"
aria-valuemin = "0" aria-valuemax = "100" style = "width: 40%;">
<span class = "sr-only">40% Complete</span>
</div>
</div>
Alternate Progress Bar
要创建一个具有不同样式的进度条:
-
添加一个具有 .progress 类的 <div>。
-
接下来,在上述 <div> 内,添加一个具有 .progress-bar 类的空 <div> 和 progress-bar- * 类的空 <div>,其中 * 可以是 success, info, warning, danger 。
-
添加一个样式属性,其中宽度以百分比表示。例如,style = "60%"; 表示进度条为 60%。
我们来看下面的示例:
<div class = "progress">
<div class = "progress-bar progress-bar-success" role = "progressbar"
aria-valuenow = "60" aria-valuemin = "0" aria-valuemax = "100" style = "width: 90%;">
<span class = "sr-only">90% Complete (Sucess)</span>
</div>
</div>
<div class = "progress">
<div class = "progress-bar progress-bar-info" role = "progressbar"
aria-valuenow = "60" aria-valuemin = "0" aria-valuemax = "100" style = "width: 30%;">
<span class = "sr-only">30% Complete (info)</span>
</div>
</div>
<div class = "progress">
<div class = "progress-bar progress-bar-warning" role = "progressbar"
aria-valuenow = "60" aria-valuemin = "0" aria-valuemax = "100" style = "width: 20%;">
<span class = "sr-only">20%Complete (warning)</span>
</div>
</div>
<div class = "progress">
<div class = "progress-bar progress-bar-danger" role = "progressbar"
aria-valuenow = "60" aria-valuemin = "0" aria-valuemax = "100" style = "width: 10%;">
<span class = "sr-only">10% Complete (danger)</span>
</div>
</div>
Striped Progress Bar
要创建一个条纹进度条:
-
添加一个具有 .progress 和 .progress-striped 类的 <div>。
-
接下来,在上述 <div> 内,添加一个具有 .progress-bar 类的空 <div> 和 progress-bar- * 类的空 <div>,其中 * 可以是 success, info, warning, danger 。
-
添加一个样式属性,其中宽度以百分比表示。例如,style = "60%"; 表示进度条为 60%。
我们来看下面的示例:
<div class = "progress progress-striped">
<div class = "progress-bar progress-bar-success" role = "progressbar"
aria-valuenow = "60" aria-valuemin = "0" aria-valuemax = "100" style = "width: 90%;">
<span class = "sr-only">90% Complete (Sucess)</span>
</div>
</div>
<div class = "progress progress-striped">
<div class = "progress-bar progress-bar-info" role = "progressbar"
aria-valuenow = "60" aria-valuemin = "0" aria-valuemax = "100" style = "width: 30%;">
<span class = "sr-only">30% Complete (info)</span>
</div>
</div>
<div class = "progress progress-striped">
<div class = "progress-bar progress-bar-warning" role = "progressbar"
aria-valuenow = "60" aria-valuemin = "0" aria-valuemax = "100" style="width: 20%;">
<span class = "sr-only">20%Complete (warning)</span>
</div>
</div>
<div class = "progress progress-striped">
<div class = "progress-bar progress-bar-danger" role = "progressbar"
aria-valuenow = "60" aria-valuemin = "0" aria-valuemax = "100" style = "width: 10%;">
<span class = "sr-only">10% Complete (danger)</span>
</div>
</div>
Animated Progress Bar
若要创建动画进度条 −
-
添加具有 .progress 和 .progress-striped 类的 <div>。同时将类 .active 添加到 .progress-striped 。
-
接下来,在上述 <div> 内,添加一个具有 .progress-bar 类的空 <div>。
-
添加一个样式属性,其中宽度以百分比表示。例如,style = "60%"; 表示进度条为 60%。
这将使条纹从右向左移动。
我们来看下面的示例:
<div class = "progress progress-striped active">
<div class = "progress-bar progress-bar-success" role = "progressbar"
aria-valuenow = "60" aria-valuemin = "0" aria-valuemax = "100" style = "width: 40%;">
<span class = "sr-only">40% Complete</span>
</div>
</div>
Stacked Progress Bar
您甚至可以叠加多个进度条。将多个进度条放入相同的 .progress 中以将其叠加在一起,如下例所示 −
<div class = "progress">
<div class = "progress-bar progress-bar-success" role = "progressbar"
aria-valuenow = "60" aria-valuemin = "0" aria-valuemax = "100" style = "width: 40%;">
<span class = "sr-only">40% Complete</span>
</div>
<div class = "progress-bar progress-bar-info" role = "progressbar"
aria-valuenow = "60" aria-valuemin = "0" aria-valuemax = "100" style = "width: 30%;">
<span class = "sr-only">30% Complete (info)</span>
</div>
<div class = "progress-bar progress-bar-warning" role = "progressbar"
aria-valuenow = "60" aria-valuemin = "0" aria-valuemax = "100" style = "width: 20%;">
<span class = "sr-only">20%Complete (warning)</span>
</div>
</div>
Bootstrap - Media Object
本章讨论媒体对象。这些是用于构建各种类型的组件(例如博客评论、推文等)的抽象对象样式,这些组件在文本内容旁边显示左对齐或右对齐的图像。媒体对象的目标是显著缩短开发这些信息块的代码。
通过将类应用于一些简单的标记,实现了媒体对象的目标(轻量标记、易于扩展)。媒体对象有两种形式 −
-
.media − 此类允许将媒体对象(图像、视频和音频)浮动到内容块的左侧或右侧。
-
.media-list − 如果您正在准备一个其中项目将成为无序列表一部分的列表,请使用一个类。适用于评论线程或文章列表。
让我们看一个以下的默认媒体对象示例 −
<div class = "media">
<a class = "pull-left" href = "#">
<img class = "media-object" src = "/bootstrap/images/64.jpg" alt = "Media Object">
</a>
<div class = "media-body">
<h4 class = "media-heading">Media heading</h4>
This is some sample text. This is some sample text.
This is some sample text. This is some sample text.
This is some sample text. This is some sample text.
This is some sample text. This is some sample text.
</div>
</div>
<div class = "media">
<a class = "pull-left" href = "#">
<img class = "media-object" src = "/bootstrap/images/64.jpg" alt = "Media Object">
</a>
<div class = "media-body">
<h4 class = "media-heading">Media heading</h4>
This is some sample text. This is some sample text.
This is some sample text. This is some sample text.
This is some sample text. This is some sample text.
This is some sample text. This is some sample text.
<div class = "media">
<a class = "pull-left" href = "#">
<img class = "media-object" src = "/bootstrap/images/64.jpg" alt = "Media Object">
</a>
<div class = "media-body">
<h4 class = "media-heading">Media heading</h4>
This is some sample text. This is some sample text.
This is some sample text. This is some sample text.
This is some sample text. This is some sample text.
This is some sample text. This is some sample text.
</div>
</div>
</div>
</div>
让我们看一个媒体列表示例 −
<ul class = "media-list">
<li class = "media">
<a class = "pull-left" href = "#">
<img class = "media-object" src = "/bootstrap/images/64.jpg" alt = "Generic placeholder image">
</a>
<div class = "media-body">
<h4 class = "media-heading">Media heading</h4>
<p>
This is some sample text. This is some sample text.
This is some sample text. This is some sample text.
This is some sample text. This is some sample text.
This is some sample text. This is some sample text.
</p>
<!-- Nested media object -->
<div class = "media">
<a class = "pull-left" href = "#">
<img class = "media-object" src = "/bootstrap/images/64.jpg"
alt = "Generic placeholder image">
</a>
<div class = "media-body">
<h4 class = "media-heading">Nested media heading</h4>
This is some sample text. This is some sample text.
This is some sample text. This is some sample text.
This is some sample text. This is some sample text.
This is some sample text. This is some sample text.
<!-- Nested media object -->
<div class = "media">
<a class = "pull-left" href = "#">
<img class = "media-object" src = "/bootstrap/images/64.jpg"
alt = "Generic placeholder image">
</a>
<div class = "media-body">
<h4 class = "media-heading">Nested media heading</h4>
This is some sample text. This is some sample text.
This is some sample text. This is some sample text.
This is some sample text. This is some sample text.
This is some sample text. This is some sample text.
</div>
</div>
</div>
</div>
<!-- Nested media object -->
<div class = "media">
<a class = "pull-left" href = "#">
<img class = "media-object" src = "/bootstrap/images/64.jpg"
alt = "Generic placeholder image">
</a>
<div class = "media-body">
<h4 class = "media-heading">Nested media heading</h4>
This is some sample text. This is some sample text.
This is some sample text. This is some sample text.
This is some sample text. This is some sample text.
This is some sample text. This is some sample text.
</div>
</div>
</div>
</li>
<li class = "media">
<a class = "pull-right" href = "#">
<img class = "media-object" src = "/bootstrap/images/64.jpg"
alt = "Generic placeholder image">
</a>
<div class = "media-body">
<h4 class = "media-heading">Media heading</h4>
This is some sample text. This is some sample text.
This is some sample text. This is some sample text.
This is some sample text. This is some sample text.
This is some sample text. This is some sample text.
</div>
</li>
</ul>
Bootstrap - List Group
列表组组件的目的是在列表中呈现复杂且自定义的内容。若要获取基本列表组 −
-
将类 .list-group 添加到元素 <ul>。
-
将类 .list-group-item 添加到 <li>。
以下示例演示了这一点−
<ul class = "list-group">
<li class = "list-group-item">Free Domain Name Registration</li>
<li class = "list-group-item">Free Window Space hosting</li>
<li class = "list-group-item">Number of Images</li>
<li class = "list-group-item">24*7 support</li>
<li class = "list-group-item">Renewal cost per year</li>
</ul>
Adding Badges to List Group
我们可以将徽章组件添加到任何列表组项目,它将自动放置在右侧。只需在 <li> 元素内添加 <span class = "badge"> 。以下示例演示了这一点 −
<ul class = "list-group">
<li class = "list-group-item">Free Domain Name Registration</li>
<li class = "list-group-item">Free Window Space hosting</li>
<li class = "list-group-item">Number of Images</li>
<li class = "list-group-item">
<span class = "badge">New</span>
24*7 support
</li>
<li class = "list-group-item">Renewal cost per year</li>
<li class = "list-group-item">
<span class = "badge">New</span>
Disocunt Offer
</li>
</ul>
Linking List Group Items
通过使用锚定标记代替列表项,我们可以链接列表组。我们需要使用 <div> 而不是 <ul> 元素。以下示例演示了这一点 −
<a href = "#" class = "list-group-item active">
Free Domain Name Registration
</a>
<a href = "#" class = "list-group-item">24*7 support</a>
<a href = "#" class = "list-group-item">Free Window Space hosting</a>
<a href = "#" class = "list-group-item">Number of Images</a>
<a href = "#" class = "list-group-item">Renewal cost per year</a>
Add Custom Content to List Group
我们可以将任何 HTML 内容添加到上述链接的列表组。以下示例演示了这一点 −
<div class = "list-group">
<a href = "#" class = "list-group-item active">
<h4 class = "list-group-item-heading">
Starter Website Package
</h4>
</a>
<a href = "#" class = "list-group-item">
<h4 class = "list-group-item-heading">
Free Domain Name Registration
</h4>
<p class = "list-group-item-text">
You will get a free domain registration with website pages.
</p>
</a>
<a href = "#" class = "list-group-item">
<h4 class = "list-group-item-heading">
24*7 support
</h4>
<p class = "list-group-item-text">
We provide 24*7 support.
</p>
</a>
</div>
<div class = "list-group">
<a href = "#" class = "list-group-item active">
<h4 class = "list-group-item-heading">
Business Website Package
</h4>
</a>
<a href = "#" class="list-group-item">
<h4 class = "list-group-item-heading">
Free Domain Name Registration
</h4>
<p class = "list-group-item-text">
You will get a free domain registration with website pages.
</p>
</a>
<a href = "#" class = "list-group-item">
<h4 class = "list-group-item-heading">24*7 support</h4>
<p class = "list-group-item-text">We provide 24*7 support.</p>
</a>
</div>
Bootstrap - Panels
本章将讨论 Bootstrap 面板。当您想将您的 DOM 组件放入一个框中时,会使用面板组件。若要获取一个基本面板,只需将类 .panel 添加到 <div> 元素。还将类 .panel-default 添加到此元素,如下例所示 −
<div class = "panel panel-default">
<div class = "panel-body">
This is a Basic panel
</div>
</div>
Panel with Heading
有两种方法可以添加面板标题 −
-
使用 .panel-heading 类轻松地为面板添加一个标题容器。
-
在任何包含 .panel-title 类的 <h1>-<h6> 中添加预先设置好的标题。
以下示例展示了两种方法 -
<div class = "panel panel-default">
<div class = "panel-heading">
Panel heading without title
</div>
<div class = "panel-body">
Panel content
</div>
</div>
<div class = "panel panel-default">
<div class = "panel-heading">
<h3 class = "panel-title">
Panel With title
</h3>
</div>
<div class = "panel-body">
Panel content
</div>
</div>
Panel with Footer
您可以通过包装在包含类 .panel-footer 的 <div> 中的按钮或辅助文本,来为面板添加页脚。以下示例演示了这一点。
<div class = "panel panel-default">
<div class = "panel-body">
This is a Basic panel
</div>
<div class = "panel-footer">Panel footer</div>
</div>
Panel Contextual Alternatives
使用上下文状态类(例如, panel-primary, panel-success, panel-info, panel-warning, panel-danger ),使面板对特定上下文更有意义。
<div class = "panel panel-primary">
<div class = "panel-heading">
<h3 class = "panel-title">Panel title</h3>
</div>
<div class = "panel-body">
This is a Basic panel
</div>
</div>
<div class = "panel panel-success">
<div class = "panel-heading">
<h3 class = "panel-title">Panel title</h3>
</div>
<div class = "panel-body">
This is a Basic panel
</div>
</div>
<div class = "panel panel-info">
<div class = "panel-heading">
<h3 class = "panel-title">Panel title</h3>
</div>
<div class = "panel-body">
This is a Basic panel
</div>
</div>
<div class = "panel panel-warning">
<div class = "panel-heading">
<h3 class = "panel-title">Panel title</h3>
</div>
<div class = "panel-body">
This is a Basic panel
</div>
</div>
<div class = "panel panel-danger">
<div class = "panel-heading">
<h3 class = "panel-title">Panel title</h3>
</div>
<div class = "panel-body">
This is a Basic panel
</div>
</div>
Panel with Tables
要在面板中获得无边框表格,请在面板中使用类 .table 。假设有一个包含 .panel-body 的 <div>,我们在表格顶部添加一个额外的边框用于分隔。如果没有包含 .panel-body 的 <div>,则组件将从面板标题无中断地移至表格。
以下示例演示了这一点−
<div class = "panel panel-default">
<div class = "panel-heading">
<h3 class = "panel-title">Panel title</h3>
</div>
<div class = "panel-body">
This is a Basic panel
</div>
<table class = "table">
<tr>
<th>Product</th>
<th>Price </th>
</tr>
<tr>
<td>Product A</td>
<td>200</td>
</tr>
<tr>
<td>Product B</td>
<td>400</td>
</tr>
</table>
</div>
<div class = "panel panel-default">
<div class = "panel-heading">Panel Heading</div>
<table class = "table">
<tr>
<th>Product</th>
<th>Price </th>
</tr>
<tr>
<td>Product A</td>
<td>200</td>
</tr>
<tr>
<td>Product B</td>
<td>400</td>
</tr>
</table>
</div>
Panel with List groups
您可以在任何面板中包含列表组。通过向 <div> 元素添加类 .panel 来创建面板。还向该元素添加类 .panel-default 。现在,在该面板中包含您的列表组。您可以从章节 List Groups 中学习如何创建列表组。
<div class = "panel panel-default">
<div class ="panel-heading">Panel heading</div>
<div class = "panel-body">
<p>This is a Basic panel content. This is a Basic panel content.
This is a Basic panel content. This is a Basic panel content.
This is a Basic panel content. This is a Basic panel content.
This is a Basic panel content.</p>
</div>
<ul class = "list-group">
<li class = "list-group-item">Free Domain Name Registration</li>
<li class = "list-group-item">Free Window Space hosting</li>
<li class = "list-group-item">Number of Images</li>
<li class = "list-group-item">24*7 support</li>
<li class = "list-group-item">Renewal cost per year</li>
</ul>
</div>
Bootstrap - Wells
“井”是 <div> 中的容器,会导致内容在页面上显得凹陷或有内嵌效果。要创建“井”,只需包装您希望出现在“井”中的内容,包装在包含 .well 类的 <div> 中即可。以下示例展示了一个默认的“井” −
<div class = "well">Hi, am in well !!</div>
Bootstrap - Plugins Overview
在 Layout Components 下的前面章节中讨论的组件仅是开始。Bootstrap 附带了 12 个 jQuery 插件,这些插件扩展了特性,并且可以为您的网站添加更多交互。要开始使用 Bootstrap 的 JavaScript 插件,您不需要高级 JavaScript 开发人员。通过利用 Bootstrap 数据 API,大多数插件都可以触发,而无需编写一行代码。
Bootstrap 插件可以在两种形式的网站上包括 -
-
Individually − 使用 Bootstrap 的各个 *.js 文件。一些插件和 CSS 组件依赖其他插件。如果您单独包含插件,请务必在文档中查看这些依赖项。
-
或 compiled (all at once) − 使用 bootstrap.js 或缩小的 bootstrap.min.js。不要尝试同时包含这两个文件,因为 bootstrap.js 和 bootstrap.min.js 都将所有插件包含在一个文件中。
Data Attributes
-
可以使用随附的数据 API 访问所有 Bootstrap 插件。因此,您无需包含一行 JavaScript 即可调用任何插件特性。
-
在某些情况下,关闭此 Data API 功能可能是可取的。如果您需要关闭 Data API,可以通过添加以下 JavaScript 行取消绑定属性 −
$(document).off('.data-api')
-
要关闭特定的/单个插件,只需将插件的名称连同 data-api 名称空间一起包含,如下所示 −
$(document).off('.alert.data-api')
Programmatic API
Bootstrap 的开发人员认为您应该能够完全通过 JavaScript API 使用所有插件。所有公共 API 都是单个、可链接的方法,并返回采取操作的集合,例如 −
$(".btn.danger").button("toggle").addClass("fat")
所有方法接受可选的选项对象、目标是特定方法的字符串,或无(它使用默认行为启动插件),如下所示 −
// initialized with defaults
$("#myModal").modal()
// initialized with no keyboard
$("#myModal").modal({ keyboard: false })
// initializes and invokes show immediately
$("#myModal").modal('show')
每个插件还在 Constructor 属性上公开其原始构造函数:$.fn.popover.Constructor。如果您想获得特定的插件实例,请直接从元素检索 −
$('[rel = popover]').data('popover').
No Conflict
有时 Bootstrap 插件可与其他 UI 框架一起使用。在这种情况下,偶尔会出现名称空间冲突。要克服这个问题,请调用 .noConflict ,在该插件上还原您想要的值。
// return $.fn.button to previously assigned value
var bootstrapButton = $.fn.button.noConflict()
// give $().bootstrapBtn the Bootstrap functionality
$.fn.bootstrapBtn = bootstrapButton
Events
Bootstrap 为大多数插件的独特操作提供了自定义事件。通常,这些事件有两种形式 −
-
Infinitive form − 这是在事件开始时触发的。例如,show。不带 to 的事件提供 preventDefault 功能。这提供了在操作开始之前停止执行操作的能力。
$('#myModal').on('show.bs.modal', function (e) {
// stops modal from being shown
if (!data) return e.preventDefault()
})
-
Past participle form − 这是在操作完成后触发的。例如,shown。
Bootstrap - Transition Plugin
transition 插件提供了一个简单的过渡效果。
Transition.js 是一个基本的 transitionEnd 事件以及 CSS 过渡模拟器助手。其他插件使用该插件来检查 CSS 过渡支持并捕获挂起的过渡。
Use Cases
以下是一些 transition 插件的示例 −
-
在模态框中滑动或淡出。你可以在章节 Bootstrap Modal Plugin 中找到一个示例。
-
淡出标签页。你可以在章节 Bootstrap Tab Plugin 中找到一个示例。
-
淡出警报。你可以在章节 Bootstrap Alerts 中找到一个示例。
-
滑动走马灯窗格。你可以在章节 Bootstrap Carousel Plugin 中找到一个示例。
Bootstrap - Modal Plugin
模态框是一个分层的子窗口,位于其父窗口之上。通常,其目的是显示源自其他资源的内容,这种内容可以使用一些互动,而无需离开父窗口。子窗口可以提供信息、互动等等。
Usage
你可以切换模态框插件的隐藏内容 −
-
Via data attributes − 在控制器元素(如按钮或链接)上设置属性 data-toggle = "modal" ,并添加 data-target = "#identifier" 或 href = "#identifier" 来定位具有特定模态框(id = "identifier")进行切换。
-
Via JavaScript − 使用此技术,你可以使用一行 JavaScript 调用 id = "identifier" 的模态框 −
$('#identifier').modal(options)
Example
在以下示例中显示了一个静态模态窗口示例 −
<h2>Example of creating Modals with Twitter Bootstrap</h2>
<!-- Button trigger modal -->
<button class = "btn btn-primary btn-lg" data-toggle = "modal" data-target = "#myModal">
Launch demo modal
</button>
<!-- Modal -->
<div class = "modal fade" id = "myModal" tabindex = "-1" role = "dialog"
aria-labelledby = "myModalLabel" aria-hidden = "true">
<div class = "modal-dialog">
<div class = "modal-content">
<div class = "modal-header">
<button type = "button" class = "close" data-dismiss = "modal" aria-hidden = "true">
×
</button>
<h4 class = "modal-title" id = "myModalLabel">
This Modal title
</h4>
</div>
<div class = "modal-body">
Add some text here
</div>
<div class = "modal-footer">
<button type = "button" class = "btn btn-default" data-dismiss = "modal">
Close
</button>
<button type = "button" class = "btn btn-primary">
Submit changes
</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
Details of the preceding code −
-
要调用模态窗口,你需要某种触发器。你可以使用按钮或链接。这里我们使用了按钮。
-
如果你查看上面的代码,你将看到在 <button> 标记中, data-target = "#myModal" 是你想在页面上加载的模态框的目标。此代码允许你在页面上创建多个模态框,然后为每个模态框创建不同的触发器。现在,明确地说,你无法同时加载多个模态框,但你可以创建多个页面,以便在不同的时间加载。
-
模态框中有两个类需要注意 − 第一个是 .modal ,它只是将 <div> 的内容标识为模态框。第二个是 .fade 类。当切换模态框时,它会导致内容淡入和淡出。
-
aria-labelledby = "myModalLabel" ,对 Modal 标题的属性引用。
-
aria-hidden = "true" 属性用于在触发器来临时(例如点击相关按钮)保持 Modal 窗口不可见。
-
<div class = "modal-header">,modal-header 是一个为 Modal 窗口标头定义样式的类。
-
class = "close" 是一个 CSS 类 close,用于为 Modal 窗口的关闭按钮设置样式。
-
data-dismiss = "modal" 是一个自定义的 HTML5 数据属性。此处它用于关闭 Modal 窗口。
-
class = "modal-body" 是一个 Bootstrap CSS 的 CSS 类,用于为 Modal 窗口主体设置样式。
-
class = "modal-footer" 是一个 Bootstrap CSS 的 CSS 类,用于为 Modal 窗口页脚设置样式。
-
data-toggle = "modal" ,HTML5 自定义数据属性 data-toggle 用于打开 Modal 窗口。
Options
有一些选项可以通过数据属性或 JavaScript 传递,以自定义 Modal 窗口的外观和感觉。下表列出了这些选项 -
Option Name |
Type/Default Value |
Data attribute name |
Description |
backdrop |
布尔值或字符串“static”默认值:true |
data-backdrop |
如果您不希望用户点击模态窗口外部时关闭模态窗口,请指定 backdrop 的 static。 |
keyboard |
boolean Default: true |
data-keyboard |
当按下 Esc 键时关闭模态窗口;设置为 false 以禁用。 |
show |
boolean Default: true |
data-show |
初始化后显示模态窗口。 |
remote |
path Default: false |
data-remote |
使用 jQuery .load 方法,将内容注入到模态窗口主体中。如果添加了带有有效 URL 的 href,它将加载该内容。以下是一个示例 -<a data-toggle = "modal" href = "remote.html" data-target = "#modal">Click me</a> |
Methods
以下是可与 modal() 一起使用的部分有用方法。
Method |
Description |
Example |
Options − .modal(options) |
以模态方式激活您的内容。接受一个可选选项对象。 |
$('#identifier').modal({ keyboard: false }) |
Toggle − .modal('toggle') |
Manually toggles a modal. |
$('#identifier').modal('toggle') |
Show − .modal('show') |
Manually opens a modal. |
$('#identifier').modal('show') |
Hide − .modal('hide') |
Manually hides a modal. |
$('#identifier').modal('hide') |
Example
以下示例演示了方法的使用 -
<h2>Example of using methods of Modal Plugin</h2>
<!-- Button trigger modal -->
<button class = "btn btn-primary btn-lg" data-toggle = "modal" data-target = "#myModal">
Launch demo modal
</button>
<!-- Modal -->
<div class = "modal fade" id = "myModal" tabindex = "-1" role = "dialog"
aria-labelledby = "myModalLabel" aria-hidden = "true">
<div class = "modal-dialog">
<div class = "modal-content">
<div class = "modal-header">
<button type = "button" class = "close" data-dismiss = "modal" aria-hidden = "true">
×
</button>
<h4 class = "modal-title" id = "myModalLabel">
This Modal title
</h4>
</div>
<div class = "modal-body">
Press ESC button to exit.
</div>
<div class = "modal-footer">
<button type = "button" class = "btn btn-default" data-dismiss = "modal">
Close
</button>
<button type = "button" class = "btn btn-primary">
Submit changes
</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<script>
$(function () { $('#myModal').modal({
keyboard: true
})});
</script>
只需单击 Esc 按钮,模态窗口便退出。
Events
下表列出了与模态窗口一起工作的事件。这些事件可用于挂接到该函数。
Event |
Description |
Example |
show.bs.modal |
在调用展示方法后激活。 |
$('#identifier').on('show.bs.modal', function () { //…做某事) |
shown.bs.modal |
用户完全看到对话框时激活(将等待 CSS 过渡完成)。 |
$('#identifier').on('shown.bs.modal', function () { //…做某事) |
hide.bs.modal |
在调用 hide 实例方法时激活。 |
$('#identifier').on('hide.bs.modal', function () { //…做某事) |
hidden.bs.modal |
用户看完对话框后激活。 |
$('#identifier').on('hidden.bs.modal', function () { //…做某事) |
Example
以下示例演示了该事件的使用 -
<h2>Example of using events of Modal Plugin</h2>
<!-- Button trigger modal -->
<button class = "btn btn-primary btn-lg" data-toggle = "modal" data-target = "#myModal">
Launch demo modal
</button>
<!-- Modal -->
<div class = "modal fade" id = "myModal" tabindex = "-1" role = "dialog"
aria-labelledby = "myModalLabel" aria-hidden = "true">
<div class = "modal-dialog">
<div class = "modal-content">
<div class = "modal-header">
<button type = "button" class = "close" data-dismiss = "modal" aria-hidden = "true">
×
</button>
<h4 class = "modal-title" id = "myModalLabel">
This Modal title
</h4>
</div>
<div class = "modal-body">
Click on close button to check Event functionality.
</div>
<div class = "modal-footer">
<button type = "button" class = "btn btn-default" data-dismiss = "modal">
Close
</button>
<button type = "button" class = "btn btn-primary">
Submit changes
</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<script>
$(function () { $('#myModal').modal('hide')})});
</script>
<script>
$(function () { $('#myModal').on('hide.bs.modal', function () {
alert('Hey, I heard you like modals...');})
});
</script>
如上面屏幕所示,如果你点击关闭按钮,即隐藏事件,将显示一条“警告”信息。
Bootstrap - Dropdown Plugin
使用下拉插件,您可以向导航栏、标签、胶囊按钮等任意组件添加下拉菜单。
Usage
您可以切换下拉插件的隐藏内容 -
-
Via data attributes - 向链接或按钮添加 data-toggle = "dropdown" 可如下所示切换一个下拉菜单 -
<div class = "dropdown">
<a data-toggle = "dropdown" href = "#">Dropdown trigger</a>
<ul class = "dropdown-menu" role = "menu" aria-labelledby = "dLabel">
...
</ul>
</div>
-
如果您需要保持链接完整(当浏览器不能使用 JavaScript 时很有用),请使用 data-target 属性,代替 href = "#" −
<div class = "dropdown">
<a id = "dLabel" role = "button" data-toggle = "dropdown" data-target = "#" href = "/page.html">
Dropdown
<span class = "caret"></span>
</a>
<ul class = "dropdown-menu" role = "menu" aria-labelledby = "dLabel">
...
</ul>
</div>
-
Via JavaScript - 要通过 JavaScript 调用下拉切换,请使用以下方法 -
$('.dropdown-toggle').dropdown()
Example
Within Navbar
以下示例演示了在导航栏中使用下拉菜单 -
<nav class = "navbar navbar-default" role = "navigation">
<div class = "navbar-header">
<a class = "navbar-brand" href = "#">TutorialsPoint</a>
</div>
<div>
<ul class = "nav navbar-nav">
<li class = "active"><a href = "#">iOS</a></li>
<li><a href = "#">SVN</a></li>
<li class = "dropdown">
<a href = "#" class = "dropdown-toggle" data-toggle = "dropdown">
Java
<b class="caret"></b>
</a>
<ul class = "dropdown-menu">
<li><a href = "#">jmeter</a></li>
<li><a href = "#">EJB</a></li>
<li><a href = "#">Jasper Report</a></li>
<li class = "divider"></li>
<li><a href = "#">Separated link</a></li>
<li class = "divider"></li>
<li><a href = "#">One more separated link</a></li>
</ul>
</li>
</ul>
</div>
</nav>
Within Tabs
以下示例演示了在标签中使用下拉菜单 -
<p>Tabs With Dropdown Example</p>
<ul class = "nav nav-tabs">
<li class = "active"><a href = "#">Home</a></li>
<li><a href = "#">SVN</a></li>
<li><a href = "#">iOS</a></li>
<li><a href = "#">VB.Net</a></li>
<li class = "dropdown">
<a class = "dropdown-toggle" data-toggle = "dropdown" href = "#">
Java
<span class = "caret"></span>
</a>
<ul class = "dropdown-menu">
<li><a href = "#">Swing</a></li>
<li><a href = "#">jMeter</a></li>
<li><a href = "#">EJB</a></li>
<li class = "divider"></li>
<li><a href = "#">Separated link</a></li>
</ul>
</li>
<li><a href = "#">PHP</a></li>
</ul>
Methods
下拉切换有一个简单方法来显示或隐藏下拉菜单。
$().dropdown('toggle')
Example
以下示例演示了下拉插件方法的使用。
<nav class = "navbar navbar-default" role = "navigation">
<div class = "navbar-header">
<a class = "navbar-brand" href = "#">TutorialsPoint</a>
</div>
<div id = "myexample">
<ul class = "nav navbar-nav">
<li class = "active"><a href = "#">iOS</a></li>
<li><a href = "#">SVN</a></li>
<li class = "dropdown">
<a href = "#" class = "dropdown-toggle">Java <b class = "caret"></b></a>
<ul class = "dropdown-menu">
<li><a id = "action-1" href = "#">jmeter</a></li>
<li><a href = "#">EJB</a></li>
<li><a href = "#">Jasper Report</a></li>
<li class = "divider"></li>
<li><a href = "#">Separated link</a></li>
<li class = "divider"></li>
<li><a href = "#">One more separated link</a></li>
</ul>
</li>
</ul>
</div>
</nav>
<script>
$(function(){
$(".dropdown-toggle").dropdown('toggle');
});
</script>
Bootstrap - Scrollspy Plugin
滚动侦听(自动更新导航)插件允许你根据滚动位置来确定页面部分。在它的基本实现中,当你滚动时,你可以基于滚动位置向导航栏添加 .active 类。
Usage
你可以向你的顶部导航栏添加滚动侦听行为——
-
Via data attributes ——向你想侦听的元素(通常是正文)添加 data-spy = "scroll" 。然后添加带有 Bootstrap .nav 组件的任何父元素的 ID 或类的属性 data-target 。为了发挥作用,该页面正文中必须含有与你正在侦听的链接匹配的 ID 的元素。
<body data-spy = "scroll" data-target = ".navbar-example">
...
<div class = "navbar-example">
<ul class = "nav nav-tabs">
...
</ul>
</div>
...
</body>
-
Via JavaScript ——你可以选择要侦听的元素,然后调用 .scrollspy() 函数,用 JavaScript 调用滚动侦听,而不用使用数据属性——
$('body').scrollspy({ target: '.navbar-example' })
Example
以下示例显示了如何通过数据属性来使用滚动侦听插件——
<nav id = "navbar-example" class = "navbar navbar-default navbar-static" role = "navigation">
<div class = "navbar-header">
<button class = "navbar-toggle" type = "button" data-toggle = "collapse"
data-target = ".bs-js-navbar-scrollspy">
<span class = "sr-only">Toggle navigation</span>
<span class = "icon-bar"></span>
<span class = "icon-bar"></span>
<span class = "icon-bar"></span>
</button>
<a class = "navbar-brand" href = "#">Tutorial Name</a>
</div>
<div class = "collapse navbar-collapse bs-js-navbar-scrollspy">
<ul class = "nav navbar-nav">
<li><a href = "#ios">iOS</a></li>
<li><a href = "#svn">SVN</a></li>
<li class = "dropdown">
<a href = "#" id = "navbarDrop1" class = "dropdown-toggle" data-toggle = "dropdown">
Java
<b class = "caret"></b>
</a>
<ul class = "dropdown-menu" role = "menu" aria-labelledby = "navbarDrop1">
<li><a href = "#jmeter" tabindex = "-1">jmeter</a></li>
<li><a href = "#ejb" tabindex = "-1">ejb</a></li>
<li class = "divider"></li>
<li><a href = "#spring" tabindex = "-1">spring</a></li>
</ul>
</li>
</ul>
</div>
</nav>
<div data-spy = "scroll" data-target = "#navbar-example" data-offset = "0"
style = "height:200px; overflow:auto; position: relative;">
<h4 id = "ios">iOS</h4>
<p>iOS is a mobile operating system developed and distributed by Apple
Inc. Originally released in 2007 for the iPhone, iPod Touch, and Apple
TV. iOS is derived from OS X, with which it shares the Darwin
foundation. iOS is Apple's mobile version of the OS X operating system
used on Apple computers.</p>
<h4 id = "svn">SVN</h4>
<p>Apache Subversion which is often abbreviated as SVN, is a software
versioning and revision control system distributed under an open source
license. Subversion was created by CollabNet Inc. in 2000, but now it
is developed as a project of the Apache Software Foundation, and as
such is part of a rich community of developers and users.</p>
<h4 id = "jmeter">jMeter</h4>
<p>jMeter is an Open Source testing software. It is 100% pure Java
application for load and performance testing.</p>
<h4 id = "ejb">EJB</h4>
<p>Enterprise Java Beans (EJB) is a development architecture for building
highly scalable and robust enterprise level applications to be deployed
on J2EE compliant Application Server such as JBOSS, Web Logic etc.</p>
<h4 id = "spring">Spring</h4>
<p>Spring framework is an open source Java platform that provides
comprehensive infrastructure support for developing robust Java
applications very easily and very rapidly.</p>
<p>Spring framework was initially written by Rod Johnson and was first
released under the Apache 2.0 license in June 2003.</p>
</div>
Options
选项可以通过数据属性或 JavaScript 传递。下表列出了选项——
Option Name |
Type/Default Value |
Data attribute name |
Description |
offset |
number Default: 10 |
data-offset |
当计算滚动的值时,从顶部开始偏移的像素数。 |
Methods
.scrollspy('refresh') ——当通过 JavaScript 方法调用滚动侦听时,你需要调用 .refresh 方法来更新 DOM。如果 DOM 的任何元素发生了变化(例如,你添加或删除了某些元素),这将非常有帮助。以下是使用此方法的语法。
$('[data-spy = "scroll"]').each(function () {
var $spy = $(this).scrollspy('refresh')
})
Example
以下示例演示了如何使用 .scrollspy('refresh') 方法——
<nav id = "myScrollspy" class = "navbar navbar-default navbar-static" role = "navigation">
<div class = "navbar-header">
<button class = "navbar-toggle" type = "button" data-toggle = "collapse"
data-target = ".bs-js-navbar-scrollspy">
<span class = "sr-only">Toggle navigation</span>
<span class = "icon-bar"></span>
<span class = "icon-bar"></span>
<span class = "icon-bar"></span>
</button>
<a class = "navbar-brand" href = "#">Tutorial Name</a>
</div>
<div class = "collapse navbar-collapse bs-js-navbar-scrollspy">
<ul class = "nav navbar-nav">
<li class = "active"><a href = "#ios">iOS</a></li>
<li><a href = "#svn">SVN</a></li>
<li class = "dropdown">
<a href = "#" id = "navbarDrop1" class = "dropdown-toggle" data-toggle = "dropdown">
Java
<b class = "caret"></b>
</a>
<ul class = "dropdown-menu" role = "menu" aria-labelledby = "navbarDrop1">
<li><a href = "#jmeter" tabindex = "-1">jmeter</a></li>
<li><a href = "#ejb" tabindex = "-1">ejb</a></li>
<li class = "divider"></li>
<li><a href = "#spring" tabindex = "-1">spring</a></li>
</ul>
</li>
</ul>
</div>
</nav>
<div data-spy = "scroll" data-target = "#myScrollspy" data-offset = "0"
style = "height:200px; overflow:auto; position: relative;">
<div class = "section">
<h4 id = "ios">iOS<small><a href = "#" onclick = "removeSection(this);">
× Remove this section</a></small>
</h4>
<p>iOS is a mobile operating system developed and distributed by
Apple Inc. Originally released in 2007 for the iPhone, iPod Touch, and
Apple TV. iOS is derived from OS X, with which it shares the Darwin
foundation. iOS is Apple's mobile version of the OS X operating system
used on Apple computers.</p>
</div>
<div class = "section">
<h4 id = "svn">SVN<small></small></h4>
<p>Apache Subversion which is often abbreviated as SVN, is a software
versioning and revision control system distributed under an open source
license. Subversion was created by CollabNet Inc. in 2000, but
now it is developed as a project of the Apache Software Foundation,
and as such is part of a rich community of developers and users.</p>
</div>
<div class = "section">
<h4 id = "jmeter">jMeter<small><a href = "#" onclick = "removeSection(this);">
× Remove this section</a></small>
</h4>
<p>jMeter is an Open Source testing software. It is 100% pure Java
application for load and performance testing.</p>
</div>
<div class = "section">
<h4 id = "ejb">EJB</h4>
<p>Enterprise Java Beans (EJB) is a development architecture for
building highly scalable and robust enterprise level applications
to be deployed on J2EE compliant Application Server such as
JBOSS, Web Logic etc.</p>
</div>
<div class = "section">
<h4 id = "spring">Spring</h4>
<p>Spring framework is an open source Java platform that provides
comprehensive infrastructure support for developing robust Java
applications very easily and very rapidly.</p>
<p>Spring framework was initially written by Rod Johnson and was first
released under the Apache 2.0 license in June 2003.</p>
</div>
</div>
<script type = "text/javascript">
$(function(){
removeSection = function(e) {
$(e).parents(".section").remove();
$('[data-spy = "scroll"]').each(function () {
var $spy = $(this).scrollspy('refresh')
});
}
$("#myScrollspy").scrollspy();
});
</script>
Events
下表列出了用于滚动侦听的事件。可以使用此事件连接到函数。
Event |
Description |
Example |
activate.bs.scrollspy |
每当滚动侦听激活新项目时,就会触发此事件。 |
$('#myScrollspy').on('activate.bs.scrollspy', function () { // do something…}) |
Example
以下示例演示了如何使用 activate.bs.scrollspy 事件——
<html>
<head>
<link rel = "stylesheet" href = "bootstrap/css/bootstrap.min.css">
<script src = "bootstrap/scripts/jquery.min.js"></script>
<script src = "bootstrap/js/bootstrap.min.js"></script>
<script>
$(document).ready(function(){
removeSection = function(e) {
$(e).parents(".subject").remove();
$('[data-spy="scroll"]').each(function () {
var $spy = $(this).scrollspy('refresh')
});
}
$("#Navexample").scrollspy();
// The event is fired when an item gets actived with the scrollspy
$("#Navexample").on('activate.bs.scrollspy', function () {
var currentSection = $(".nav li.active > a").text();
$("#spyevent").html("Current Item being viewed >> " + currentSection);
})
});
</script>
<style>
.scroll-box {
height: 250px;
position: relative;
overflow: auto;
font-size:2em;
}
</style>
</head>
<body>
<div class = "container">
<nav id = "Navexample" class = "navbar navbar-default" role = "navigation">
<!-- Nav Bar -->
<div class = "navbar-header">
<button type = "button" class = "navbar-toggle"
data-toggle = "collapse" data-target = "#navbarCollapse">
<span class = "sr-only">Toggle navigation</span>
<span class = "icon-bar"></span>
<span class = "icon-bar"></span>
<span class = "icon-bar"></span>
</button>
<a class = "navbar-brand" href = "#">Tutorials Point</a>
</div>
<!-- Links and Sublinks -->
<div class = "collapse navbar-collapse" id = "navbarCollapse">
<ul class = "nav navbar-nav">
<li class = "active"><a href = "#subject-1">Subject 1</a></li>
<li><a href = "#subject-2">Subject 2</a></li>
<li class = "dropdown"><a href = "#" class = "dropdown-toggle"
data-toggle = "dropdown">Subject 3<b class = "caret"></b></a>
<ul class = "dropdown-menu">
<li><a href = "#subject-3-1">Subject 3.1</a></li>
<li><a href = "#subject-3-2">Subject 3.2</a></li>
<li><a href = "#subject-3-3">Subject 3.3</a></li>
</ul>
</li>
<li><a href = "#subject-4">Subject 4</a></li>
</ul>
</div>
</nav>
<div class = "scroll-box" data-spy = "scroll" data-offset = "0">
<div class = "subject">
<h3 id = "subject-1">Subject 1
<small><a href = "#" onclick = "removeSubject(this);">Remove Subject ×</a></small>
</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
<hr>
<div class = "subject">
<h3 id = "subject-2">Subject 2
<small><a href = "#" onclick = "removeSubject(this);">Remove Subject ×</a></small>
</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
<hr>
<div class = "subject">
<h3 id = "subject-3">Subject 3
<small><a href = "#" onclick = "removeSubject(this);"> Remove Subject ×</a></small>
</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.</p><
</div>
<hr>
<div class = "subject">
<h4 id = "subject-3-1">Subject 3.1
<small><a href = "#" onclick = "removeSubject(this);"> Remove Subject ×</a></small>
</h4>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
<div class = "subject">
<h4 id = "subject-3-2">Subject 3.2
<small><a href = "#" onclick = "removeSubject(this);"> Remove Subject ×</a></small>
</h4>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
<div class = "subject">
<h4 id = "subject-3-3">Subject 3.3
<small><a href = "#" onclick = "removeSubject(this);">Remove Subject ×</a></small>
</h4>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
<hr>
<div class = "subject">
<h3 id = "subject-4">Subject 4
<small><a href = "#" onclick = "removeSubject(this);">Remove Subject ×</a></small>
</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
</div>
<hr>
<h4 id = "spyevent" class = "text-info"></h4>
</div>
</body>
</html>
Bootstrap - Tab Plugin
在章节 Bootstrap Navigation Elements 中引入了选项卡。通过组合一些数据属性,你可以轻松创建选项卡界面。使用此插件,你可以切换选项卡或选项卡下拉菜单中的本地内容窗格。
Usage
可以通过以下两种方式启用选项卡——
-
Via data attributes ——你需要向锚点添加 data-toggle = "tab" 或 data-toggle = "pill" 。向选项卡 ul 添加 nav 和 nav-tabs 类将应用 Bootstrap tab styling ,而添加 nav 和 nav-pills 类则会应用 pill styling 。
<ul class = "nav nav-tabs">
<li><a href = "#identifier" data-toggle = "tab">Home</a></li>
...
</ul>
-
Via JavaScript − 你可以使用 Javscript 如下启用选项卡 −
$('#myTab a').click(function (e) {
e.preventDefault()
$(this).tab('show')
})
-
以下是激活各个选项卡的不同方式的示例 −
// Select tab by name
$('#myTab a[href = "#profile"]').tab('show')
// Select first tab
$('#myTab a:first').tab('show')
// Select last tab
$('#myTab a:last').tab('show')
// Select third tab (0-indexed)
$('#myTab li:eq(2) a').tab('show')
Fade Effect
为选项卡添加淡入淡出效果,给每个 .tab-pane 添加 .fade 。第一个选项卡窗格也必须有 .in 来正确地淡入初始内容 −
<div class = "tab-content">
<div class = "tab-pane fade in active" id = "home">...</div>
<div class = "tab-pane fade" id = "svn">...</div>
<div class = "tab-pane fade" id = "ios">...</div>
<div class = "tab-pane fade" id = "java">...</div>
</div>
Example
使用数据属性和淡入淡出效果的选项卡插件示例如下 −
<ul id = "myTab" class = "nav nav-tabs">
<li class = "active">
<a href = "#home" data-toggle = "tab">
Tutorial Point Home
</a>
</li>
<li><a href = "#ios" data-toggle = "tab">iOS</a></li>
<li class = "dropdown">
<a href = "#" id = "myTabDrop1" class = "dropdown-toggle" data-toggle = "dropdown">
Java
<b class = "caret"></b>
</a>
<ul class = "dropdown-menu" role = "menu" aria-labelledby = "myTabDrop1">
<li><a href = "#jmeter" tabindex = "-1" data-toggle = "tab">jmeter</a></li>
<li><a href = "#ejb" tabindex = "-1" data-toggle = "tab">ejb</a></li>
</ul>
</li>
</ul>
<div id = "myTabContent" class = "tab-content">
<div class = "tab-pane fade in active" id = "home">
<p>Tutorials Point is a place for beginners in all technical areas.
This website covers most of the latest technologies and explains each of
the technology with simple examples.</p>
</div>
<div class = "tab-pane fade" id = "ios">
<p>iOS is a mobile operating system developed and distributed
by Apple Inc. Originally released in 2007 for the iPhone, iPod Touch,
and Apple TV. iOS is derived from OS X, with which it shares the
Darwin foundation. iOS is Apple's mobile version of the OS X
operating system used on Apple computers.</p>
</div>
<div class = "tab-pane fade" id = "jmeter">
<p>jMeter is an Open Source testing software. It is 100% pure Java
application for load and performance testing.</p>
</div>
<div class = "tab-pane fade" id = "ejb">
<p>Enterprise Java Beans (EJB) is a development architecture for
building highly scalable and robust enterprise level applications to be
deployed on J2EE compliant Application Server such as JBOSS, Web Logic etc.</p>
</div>
</div>
Methods
.$().tab − 此方法激活选项卡元素和内容容器。选项卡应具有一个 data-target 或一个 href ,它们都针对 DOM 中的容器节点。
<ul class = "nav nav-tabs" id = "myTab">
<li class = "active"><a href = "#identifier" data-toggle = "tab">Home</a></li>
.....
</ul>
<div class = "tab-content">
<div class = "tab-pane active" id = "home">...</div>
.....
</div>
<script>
$(function () {
$('#myTab a:last').tab('show')
})
</script>
Example
以下示例显示了选项卡插件方法 .tab 的用法。在此示例中,第二个选项卡 iOS 已激活 −
<ul id = "myTab" class = "nav nav-tabs">
<li class = "active">
<a href = "#home" data-toggle = "tab">
Tutorial Point Home
</a>
</li>
<li><a href = "#ios" data-toggle = "tab">iOS</a></li>
<li class = "dropdown">
<a href = "#" id = "myTabDrop1" class = "dropdown-toggle" data-toggle = "dropdown">
Java
<b class = "caret"></b>
</a>
<ul class = "dropdown-menu" role = "menu" aria-labelledby = "myTabDrop1">
<li>
<a href = "#jmeter" tabindex = "-1" data-toggle = "tab">
jmeter
</a>
</li>
<li>
<a href = "#ejb" tabindex = "-1" data-toggle = "tab">
ejb
</a>
</li>
</ul>
</li>
</ul>
<div id = "myTabContent" class = "tab-content">
<div class = "tab-pane fade in active" id = "home">
<p>Tutorials Point is a place for beginners in all technical areas.
This website covers most of the latest technologies and explains each of
the technology with simple examples.</p>
</div>
<div class = "tab-pane fade" id = "ios">
<p>iOS is a mobile operating system developed and distributed by
Apple Inc. Originally released in 2007 for the iPhone, iPod Touch, and
Apple TV. iOS is derived from OS X, with which it shares the Darwin
foundation. iOS is Apple's mobile version of the OS X operating system
used on Apple computers.</p>
</div>
<div class = "tab-pane fade" id = "jmeter">
<p>jMeter is an Open Source testing software. It is 100% pure Java
application for load and performance testing.</p>
</div>
<div class = "tab-pane fade" id = "ejb">
<p>Enterprise Java Beans (EJB) is a development architecture for
building highly scalable and robust enterprise level applications to be
deployed on J2EE compliant Application Server such as JBOSS,
Web Logic etc.</p>
</div>
</div>
<script>
$(function () {
$('#myTab li:eq(1) a').tab('show');
});
</script>
Events
下表列出了与选项卡插件协同工作的事件。此事件可用于连接到函数。
Event |
Description |
Example |
show.bs.tab |
此事件在显示选项卡时触发,但新的选项卡尚未显示。使用 event.target 和 event.relatedTarget 分别以活动选项卡和前一个活动选项卡(如可用)为目标。 |
$('a[data-toggle = "tab"]').on('show.bs.tab', function (e) { e.target // 已激活选项卡 e.relatedTarget // 前一个选项卡}) |
shown.bs.tab |
此事件在选项卡显示后触发。使用 event.target 和 event.relatedTarget 分别以活动选项卡和前一个活动选项卡(如可用)为目标。 |
$('a[data-toggle = "tab"]').on('shown.bs.tab', function (e) { e.target // 已激活选项卡 e.relatedTarget // 前一个选项卡}) |
Example
以下示例显示了选项卡插件事件的用法。在此示例中,我们将显示当前和前一个已访问的选项卡 −
<hr>
<p class = "active-tab"><strong>Active Tab</strong>: <span></span></p>
<p class = "previous-tab"><strong>Previous Tab</strong>: <span></span></p>
<hr>
<ul id = "myTab" class = "nav nav-tabs">
<li class = "active">
<a href = "#home" data-toggle = "tab">
Tutorial Point Home
</a>
</li>
<li><a href = "#ios" data-toggle = "tab">iOS</a></li>
<li class = "dropdown">
<a href = "#" id = "myTabDrop1" class = "dropdown-toggle" data-toggle = "dropdown">
Java
<b class = "caret"></b>
</a>
<ul class = "dropdown-menu" role = "menu" aria-labelledby = "myTabDrop1">
<li>
<a href = "#jmeter" tabindex = "-1" data-toggle = "tab">jmeter</a>
</li>
<li>
<a href = "#ejb" tabindex = "-1" data-toggle = "tab">ejb</a>
</li>
</ul>
</li>
</ul>
<div id = "myTabContent" class = "tab-content">
<div class = "tab-pane fade in active" id = "home">
<p>Tutorials Point is a place for beginners in all technical areas.
This website covers most of the latest technologies and explains each of
the technology with simple examples.</p>
</div>
<div class = "tab-pane fade" id = "ios">
<p>iOS is a mobile operating system developed and distributed by
Apple Inc. Originally released in 2007 for the iPhone, iPod Touch, and
Apple TV. iOS is derived from OS X, with which it shares the Darwin
foundation. iOS is Apple's mobile version of the OS X operating system
used on Apple computers.</p>
</div>
<div class = "tab-pane fade" id = "jmeter">
<p>jMeter is an Open Source testing software. It is 100% pure Java
application for load and performance testing.</p>
</div>
<div class = "tab-pane fade" id = "ejb">
<p>Enterprise Java Beans (EJB) is a development architecture for
building highly scalable and robust enterprise level applications to be
deployed on J2EE compliant Application Server such as JBOSS, Web Logic etc.</p>
</div>
</div>
<script>
$(function(){
$('a[data-toggle = "tab"]').on('shown.bs.tab', function (e) {
// Get the name of active tab
var activeTab = $(e.target).text();
// Get the name of previous tab
var previousTab = $(e.relatedTarget).text();
$(".active-tab span").html(activeTab);
$(".previous-tab span").html(previousTab);
});
});
</script>
Bootstrap - Tooltip Plug-in
当需要描述链接时,工具提示非常有用。此插件的灵感来自 Jason Frame 编写的 jQuery.tipsy 插件。自此以后,工具提示已更新为可以在没有图片的情况下工作、使用 CSS 动画和使用数据属性进行本地标题存储。
Usage
工具提示插件按需生成内容和标记,并且默认情况下将工具提示放置在触发元素之后。你可以用以下两种方式添加工具提示 −
-
Via data attributes − 要添加工具提示,请在锚点标记中添加 data-toggle = "tooltip" 。锚点的标题将成为工具提示的文本。默认情况下,该插件将工具提示设置为顶部。
<a href = "#" data-toggle = "tooltip" title = "Example tooltip">Hover over me</a>
-
Via JavaScript − 通过 JavaScript 触发工具提示 −
$('#identifier').tooltip(options)
Example
以下示例演示了通过数据属性使用工具提示插件。
<h4>Tooltip examples for anchors</h4>
This is a <a href = "#" class = "tooltip-test" data-toggle = "tooltip"
title = "Tooltip on left"> Default Tooltip</a>.
This is a <a href = "#" class = "tooltip-test" data-toggle = "tooltip"
data-placement = "left" title = "Tooltip on left">Tooltip on Left</a>.
This is a <a href = "#" data-toggle = "tooltip" data-placement = "top"
title = "Tooltip on top">Tooltip on Top</a>.
This is a <a href = "#" data-toggle = "tooltip" data-placement = "bottom"
title = "Tooltip on bottom">Tooltip on Bottom</a>.
This is a <a href = "#" data-toggle = "tooltip" data-placement = "right"
title = "Tooltip on right">Tooltip on Right</a>
<br>
<h4>Tooltip examples for buttons</h4>
<button type = "button" class = "btn btn-default" data-toggle = "tooltip" title = "Tooltip on left">
Default Tooltip
</button>
<button type = "button" class = "btn btn-default" data-toggle = "tooltip"
data-placement = "left" title = "Tooltip on left">
Tooltip on left
</button>
<button type = "button" class = "btn btn-default" data-toggle = "tooltip"
data-placement = "top" title = "Tooltip on top">
Tooltip on top
</button>
<button type = "button" class = "btn btn-default" data-toggle = "tooltip"
data-placement = "bottom" title = "Tooltip on bottom">
Tooltip on bottom
</button>
<button type = " button" class = " btn btn-default" data-toggle = "tooltip"
data-placement = "right" title = "Tooltip on right">
Tooltip on right
</button>
<script>
$(function () { $("[data-toggle = 'tooltip']").tooltip(); });
</script>
Options
有几个选项可以通过 Bootstrap 数据 API 或通过 JavaScript 调用添加。下表列出了这些选项 −
Option Name |
Type/Default Value |
Data attribute name |
Description |
animation |
boolean Default: true |
data-animation |
将 CSS 淡入淡出过渡应用于工具提示。 |
html |
boolean Default: false |
data-html |
将 HTML 插入到工具提示中。如果为 false,则会使用 jQuery 的文本方法将内容插入到 dom 中。如果担心 XSS 攻击,请使用文本。 |
placement |
string |
function Default: top |
data-placement |
指定如何定位工具提示(即顶部 |
bottom |
left |
right |
自动)。指定自动时,它将动态重新定向工具提示。例如,如果位置是“自动左侧”,当可能时,工具提示将显示在左侧,否则将在右侧显示。 |
selector |
string Default: false |
data-selector |
如果提供了选择器,则将工具提示对象委托给指定的目标。 |
title |
string |
function Default: " |
data-title |
如果标题属性不存在,则 title 选项是默认的标题值。 |
trigger |
string Default: 'hover focus' |
data-trigger |
定义如何触发工具提示:*单击 |
hover |
focus |
手动*。你可以传递多个触发器;使用空格分隔它们。 |
content |
string |
function Default: '' |
data-content |
如果 data-content 属性不存在,则为默认内容值 |
delay |
number |
object Default: 0 |
data-delay |
延迟以毫秒显示和隐藏工具提示——不适用于手动触发类型。如果提供了一个数字,则延迟将应用于隐藏/显示。对象结构为 −延迟:{显示:500,隐藏:100} |
container |
string |
false Default: false |
data-container |
将工具提示附加到特定元素。示例:容器:'主体' |
Methods
以下是工具提示的一些有用方法 −
Method |
Description |
Example |
Options − .tooltip(options) |
将工具提示处理程序附加到元素集合。 |
$().tooltip(options) |
Toggle − .tooltip('toggle') |
Toggles an element’s tooltip. |
$('#element').tooltip('toggle') |
Show − .tooltip('show') |
Reveals an element’s tooltip. |
$('#element').tooltip('show') |
Hide − .tooltip('hide') |
Hides an element’s tooltip. |
$('#element').tooltip('hide') |
Destroy − .tooltip('destroy') |
隐藏并销毁元素的工具提示。 |
$('#element').tooltip('destroy') |
Example
以下示例演示了通过数据属性使用工具提示插件。
<div style = "padding: 100px 100px 10px;">
This is a <a href = "#" class = "tooltip-show" data-toggle = "tooltip"
title = "show">Tooltip on method show</a>.
This is a <a href = "#" class = "tooltip-hide" data-toggle = "tooltip"
data-placement = "left" title = "hide">Tooltip on method hide</a>.
This is a <a href = "#" class = "tooltip-destroy" data-toggle = "tooltip"
data-placement = "top" title = "destroy">Tooltip on method destroy</a>.
This is a <a href = "#" class = "tooltip-toggle" data-toggle = "tooltip"
data-placement = "bottom" title = "toggle">Tooltip on method toggle</a>.
<br><br><br><br><br><br>
<p class = "tooltip-options" >
This is a <a href = "#" data-toggle = "tooltip" title = "<h2>'am Header2
</h2>">Tooltip on method options</a>.
</p>
<script>
$(function () { $('.tooltip-show').tooltip('show');});
$(function () { $('.tooltip-hide').tooltip('hide');});
$(function () { $('.tooltip-destroy').tooltip('destroy');});
$(function () { $('.tooltip-toggle').tooltip('toggle');});
$(function () { $(".tooltip-options a").tooltip({html : true });});
</script>
</div>
Events
下表列出了与工具提示插件配合使用的一些事件。该事件可以用来挂接函数。
Event |
Description |
Example |
show.bs.tooltip |
当调用 show 实例方法时,此事件将立即触发。 |
$('#myTooltip').on('show.bs.tooltip', function () { // do something}) |
shown.bs.tooltip |
当工具提示对用户可见时触发此事件(将等待 CSS 转换完成)。 |
$('#myTooltip').on('shown.bs.tooltip', function () { // do something}) |
hide.bs.tooltip |
当立即调用隐藏实例方法时,此事件将触发。 |
$('#myTooltip').on('hide.bs.tooltip', function () { // do something }) |
hidden.bs.tooltip |
当工具提示已完成对用户隐藏时触发此事件(将等待 CSS 过渡完成)。 |
$('#myTooltip').on('hidden.bs.tooltip', function () { // do something }) |
Example
以下示例演示了通过数据属性使用工具提示插件。
<h4>Tooltip examples for anchors</h4>
This is a <a href = "#" class = "tooltip-show" data-toggle = "tooltip"
title = "Default Tooltip">Default Tooltip</a>.
<script>
$(function () { $('.tooltip-show').tooltip('show');});
$(function () { $('.tooltip-show').on('show.bs.tooltip', function () {
alert("Alert message on show");
})});
</script>
Bootstrap - Popover Plugin
弹出框与工具提示类似,提供了带有标题的完整扩展视图。要激活弹出框,用户只需将光标悬停在元素上即可。弹出框的内容可以使用 Bootstrap 数据 API 完全填充。此方法需要一个工具提示。
Usage
弹出框插件会按需生成内容和标记,并且默认情况下会在其触发元素后放置弹出框。您可以通过以下两种方式添加弹出框 −
-
Via data attributes − 要添加弹出框,请向锚点/按钮标签添加 data-toggle = "popover" 。锚点的标题将是弹出框的文本。默认情况下,弹出框已通过插件设置为顶部。
<a href = "#" data-toggle = "popover" title = "Example popover">
Hover over me
</a>
-
Via JavaScript − 使用以下语法通过 JavaScript 启用弹出框 −
$('#identifier').popover(options)
Example
以下示例演示了通过数据属性使用弹出框插件。
<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 = "Some content in Popover on left">
Popover on left
</button>
<button type = "button" class = "btn btn-primary" title = "Popover title"
data-container = "body" data-toggle = "popover" data-placement = "top"
data-content = "Some content in Popover on top">
Popover on top
</button>
<button type = "button" class = "btn btn-success" title = "Popover title"
data-container = "body" data-toggle = "popover" data-placement = "bottom"
data-content = "Some content in Popover on bottom">
Popover on bottom
</button>
<button type = "button" class = "btn btn-warning" title = "Popover title"
data-container = "body" data-toggle = "popover" data-placement = "right"
data-content = "Some content in Popover on right">
Popover on right
</button>
</div>
<script>
$(function (){
$("[data-toggle = 'popover']").popover();
});
</script>
Options
有几个选项可以通过 Bootstrap 数据 API 或通过 JavaScript 调用添加。下表列出了这些选项 −
Option Name |
Type/Default Value |
Data attribute name |
Description |
animation |
boolean Default − true |
data-animation |
Applies a CSS fade transition to the popover. |
html |
boolean Default − false |
data-html |
Inserts 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. |
placement |
string |
function Default − top |
data-placement |
指定了如何定位弹出提示(例如,顶部 |
bottom |
left |
right |
自动)。如果指定了自动,它将动态地重新定位弹出提示。例如,如果位置是“自动左”,弹出提示将尽可能显示在左边,否则将显示在右边。 |
selector |
string Default − false |
data-selector |
如果提供了选择器,弹出提示对象将被委派给指定的目标。 |
title |
string |
function Default − " |
data-title |
如果标题属性不存在,则 title 选项是默认的标题值。 |
trigger |
字符串默认 - '悬停焦点' |
data-trigger |
定义弹出提示如何触发 - *点击 |
hover |
focus |
手动*。你可以传递多个触发器;使用空格分隔它们。 |
delay |
number |
object Default − 0 |
data-delay |
延迟以毫秒为单位显示和隐藏弹出提示 - 不适用于手动触发类型。如果提供了一个数字,则延迟将同时应用于隐藏/显示。对象结构为 - 延迟:{显示:500,隐藏:100} |
container |
string |
Methods
以下是用于弹出提示的一些有用的方法 -
Method |
Description |
Example |
Options − .popover(options) |
Attaches a popover handler to an element collection. |
$().popover(options) |
Toggle − .popover('toggle') |
Toggles an element’s popover. |
$('#element').popover('toggle') |
Show − .popover('show') |
Reveals an element’s popover. |
$('#element').popover('show') |
Hide − .popover('hide') |
Hides an element’s popover. |
$('#element').popover('hide') |
Destroy − .popover('destroy') |
Hides and destroys an element’s popover. |
$('#element').popover('destroy') |
Example
下面的示例演示了弹出层插件方法 -
<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 = "Some content in Popover with show method">
Popover on left
</button>
<button type = "button" class = "btn btn-primary popover-hide"
title = "Popover title" data-container = "body"
data-toggle = "popover" data-placement = "top"
data-content = "Some content in Popover-hide method">
Popover on top
</button>
<button type = "button" class = "btn btn-success popover-destroy"
title = "Popover title" data-container = "body"
data-toggle = "popover" data-placement = "bottom"
data-content = "Some content in Popover-destroy method">
Popover on bottom
</button>
<button type = "button" class = "btn btn-warning popover-toggle"
title = "Popover title" data-container = "body"
data-toggle = "popover" data-placement = "top"
data-content = "Some content in Popover-toggle method">
Popover on right
</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>Some content in Popover-options method</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>
Events
下表列出了与弹出层插件配合使用的事件。此事件可用于链接到函数。
Event |
Description |
Example |
show.bs.popover |
当调用 show 实例方法时,此事件将立即触发。 |
$('#mypopover').on('show.bs.popover', function () { // 执行操作}) |
shown.bs.popover |
当弹出层对用户可见后触发此事件(将等待 CSS 过渡完成)。 |
$('#mypopover').on('shown.bs.popover', function () { // 执行操作}) |
hide.bs.popover |
当立即调用隐藏实例方法时,此事件将触发。 |
$('#mypopover').on('hide.bs.popover', function () { // 执行操作}) |
hidden.bs.popover |
当弹出层对用户隐藏后(将等待 CSS 过渡完成)触发此事件。 |
$('#mypopover').on('hidden.bs.popover', function () { // 执行操作}) |
Example
下面的示例演示了弹出层插件事件 -
<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 = "Some content in Popover with show method">
Popover on left
</button>
</div>
<script>
$(function () { $('.popover-show').popover('show');});
$(function () { $('.popover-show').on('shown.bs.popover', function () {
alert("Alert message on show");
})});
</script>
Bootstrap - Alert Plugin
警告消息通常用于向终端用户显示信息,例如警告或确认消息。使用警告消息插件,您可以为所有警告消息添加关闭功能。
Usage
您可以用以下两种方式启用关闭警告 -
-
Via data attributes − 要通过数据 API 解除,只需将 data-dismiss = "alert" 添加到关闭按钮以自动提供关闭警告功能。
<a class = "close" data-dismiss = "alert" href = "#" aria-hidden = "true">
×
</a>
-
Via JavaScript − 要通过 JavaScript 解除,请使用以下语法 -
$(".alert").alert()
Methods
以下是某些对警告插件有用的方法 -
Method |
Description |
Example |
.alert() |
此方法为所有警告封装了关闭功能。 |
$('#identifier').alert(); |
Close Method .alert('close') |
要启用关闭所有警告,请添加此方法。 |
$('#identifier').alert('close'); |
Example
以下示例演示了 .alert() 方法的使用 -
<h3>Alert messages to demonstrate alert() method </h3>
<div id = "myAlert" class = "alert alert-success">
<a href = "#" class = "close" data-dismiss = "alert">×</a>
<strong>Success!</strong> the result is successful.
</div>
<div id = "myAlert" class = "alert alert-warning">
<a href = "#" class = "close" data-dismiss = "alert">×</a>
<strong>Warning!</strong> There was a problem with your network connection.
</div>
<script type = "text/javascript">
$(function(){
$(".close").click(function(){
$("#myAlert").alert();
});
});
</script>
以下示例演示了 .alert('close') 方法的使用 −
<h3>Alert messages to demonstrate alert('close') method </h3>
<div id = "myAlert" class = "alert alert-success">
<a href = "#" class = "close" data-dismiss = "alert">×</a>
<strong>Success!</strong> the result is successful.
</div>
<div id = "myAlert" class = "alert alert-warning">
<a href = "#" class = "close" data-dismiss = "alert">×</a>
<strong>Warning!</strong> There was a problem with your network connection.
</div>
<script type = "text/javascript">
$(function(){
$(".close").click(function(){
$("#myAlert").alert('close');
});
});
</script>
尝试使用 Try it 编辑器运行此代码。你可以看到关闭功能应用于所有警报消息,即关闭任何一个警报消息,其余的警报消息也将随之关闭。
Events
下表列出了用于处理警报插件的事件。此事件可用于挂接到警报函数。
Event |
Description |
Example |
close.bs.alert |
当调用关闭实例方法时,此事件将立即触发。 |
$('#myalert').bind('close.bs.alert', function () { // do something}) |
closed.bs.alert |
警报关闭后触发此事件(将等待 CSS 过渡完成)。 |
$('#myalert').bind('closed.bs.alert', function () { // do something}) |
Example
以下示例演示了警报插件事件 −
<div id = "myAlert" class = "alert alert-success">
<a href = "#" class = "close" data-dismiss = "alert">×</a>
<strong>Success!</strong> the result is successful.
</div>
<script type = "text/javascript">
$(function(){
$("#myAlert").bind('closed.bs.alert', function () {
alert("Alert message box is closed.");
});
});
</script>
Bootstrap - Button Plugin
按钮已在章节 Bootstrap Buttons 中进行了说明。通过此插件,你可以添加一些交互,例如控制按钮状态或为更多组件(例如工具栏)创建按钮组。
Loading State
若要为按钮添加加载状态,只需将 data-loading-text = "Loading…" 作为属性添加到按钮元素,如下例所示 −
<button id = "fat-btn" class = "btn btn-primary" data-loading-text = "Loading..." type = "button">
Loading state
</button>
<script>
$(function() {
$(".btn").click(function(){
$(this).button('loading').delay(1000).queue(function() {
// $(this).button('reset');
});
});
});
</script>
当你单击按钮时,输出将如以下图像所示 −
Single toggle
若要在单个按钮上激活切换(即,将按钮的正常状态更改为按下状态,反之亦然),请将 data-toggle = "button" 作为属性添加到按钮元素,如下例所示 −
<button type = "button" class = "btn btn-primary" data-toggle = "button">
Single toggle
</button>
Checkbox
你可以创建复选框组并通过简单地将数据属性 data-toggle = "buttons" 添加到 btn-group. 来为其添加切换功能。
<div class = "btn-group" data-toggle = "buttons">
<label class = "btn btn-primary">
<input type = "checkbox"> Option 1
</label>
<label class = "btn btn-primary">
<input type = "checkbox"> Option 2
</label>
<label class = "btn btn-primary">
<input type = "checkbox"> Option 3
</label>
</div>
Radio
同样,你还可以创建一组单选输入并通过简单地将数据属性 data-toggle = "buttons" 添加到 btn-group 来为其添加切换功能。
<div class = "btn-group" data-toggle = "buttons">
<label class = "btn btn-primary">
<input type = "radio" name =" options" id = "option1"> Option 1
</label>
<label class = "btn btn-primary">
<input type = "radio" name = "options" id = "option2"> Option 2
</label>
<label class = "btn btn-primary">
<input type = "radio" name = "options" id = "option3"> Option 3
</label>
</div>
Methods
下面列出了一些对按钮插件有用的方法 −
Method |
Description |
Example |
button('toggle') |
切换按下状态。使按钮显示为已激活的外观。你还可以使用 data-toggle 属性启用按钮的自动切换。 |
$().button('toggle') |
.button('loading') |
加载时,按钮将禁用,并且文本将更改为按钮元素的 data-loading-text 属性中的选项 |
$().button('loading') |
.button('reset') |
重置按钮状态,将原始内容恢复到文本。当你需要将按钮恢复到主状态时,此方法非常有用 |
$().button('reset') |
.button(string) |
本方法中的字符串是指用户声明的任何字符串。若要重置按钮状态并导入新内容,请使用此方法。 |
$().button(string) |
Example
以下示例演示上述方法的使用:
<h2>Click on each of the buttons to see the effects of methods</h2>
<h4>Example to demonstrate .button('toggle') method</h4>
<div id = "myButtons1" class = "bs-example">
<button type = "button" class = "btn btn-primary">Primary</button>
</div>
<h4>Example to demonstrate .button('loading') method</h4>
<div id = "myButtons2" class = "bs-example">
<button type = "button" class = "btn btn-primary" data-loading-text = "Loading...">
Primary
</button>
</div>
<h4>Example to demonstrate .button('reset') method</h4>
<div id = "myButtons3" class = "bs-example">
<button type = "button" class = "btn btn-primary" data-loading-text = "Loading...">
Primary
</button>
</div>
<h4>Example to demonstrate .button(string) method</h4>
<button type = "button" class = "btn btn-primary" id = "myButton4"
data-complete-text = "Loading finished">
Click Me
</button>
<script type = "text/javascript">
$(function () {
$("#myButtons1 .btn").click(function(){
$(this).button('toggle');
});
});
$(function() {
$("#myButtons2 .btn").click(function(){
$(this).button('loading').delay(1000).queue(function() {
});
});
});
$(function() {
$("#myButtons3 .btn").click(function(){
$(this).button('loading').delay(1000).queue(function() {
$(this).button('reset');
});
});
});
$(function() {
$("#myButton4").click(function(){
$(this).button('loading').delay(1000).queue(function() {
$(this).button('complete');
});
});
});
</script>
Bootstrap - Collapse Plugin
合并插件可以轻松创建页面的折叠区段。无论你使用它来构建手风琴式导航还是内容框,它都能提供大量内容选项。
你可以使用合并插件:
-
To create collapsible groups or accordion 。它可以像以下示例中一样创建:
<div class = "panel-group" id = "accordion">
<div class = "panel panel-default">
<div class = "panel-heading">
<h4 class = "panel-title">
<a data-toggle = "collapse" data-parent = "#accordion" href = "#collapseOne">
Click me to expand. Click me again to collapse.Section 1
</a>
</h4>
</div>
<div id = "collapseOne" class = "panel-collapse collapse in">
<div class = "panel-body">
Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred
nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo.
</div>
</div>
</div>
<div class = "panel panel-default">
<div class = "panel-heading">
<h4 class = "panel-title">
<a data-toggle = "collapse" data-parent = "#accordion" href = "#collapseTwo">
Click me to expand. Click me again to collapse.Section 2
</a>
</h4>
</div>
<div id = "collapseTwo" class = "panel-collapse collapse">
<div class = "panel-body">
Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt
sapiente ea proident. Ad vegan excepteur butcher vice lomo.
</div>
</div>
</div>
<div class = "panel panel-default">
<div class = "panel-heading">
<h4 class = "panel-title">
<a data-toggle = "collapse" data-parent = "#accordion" href = "#collapseThree">
Click me to expand. Click me again to collapse.Section 3
</a>
</h4>
</div>
<div id = "collapseThree" class = "panel-collapse collapse">
<div class = "panel-body">
Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt
sapiente ea proident. Ad vegan excepteur butcher vice lomo.
</div>
</div>
</div>
</div>
-
data-toggle = "collapse" 被添加到点击以展开或折叠组件的链接上。
-
href 或 data-target 属性被添加到父组件中,其值为子组件的 id。
-
data-parent 属性被添加以创建类似手风琴的动态。
-
若要创建不带手风琴标记的简单可折叠组件,则可以像以下示例一样创建:
<button type = "button" class = "btn btn-primary" data-toggle = "collapse" data-target = "#demo">
simple collapsible
</button>
<div id = "demo" class = "collapse in">
Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt
sapiente ea proident. Ad vegan excepteur butcher vice lomo.
</div>
如你所见,在这个示例中,我们创建了一个简单的可折叠组件,与手风琴不同,我们没有添加 data-parent 属性。
Usage
下表列出了合并插件用于处理繁重任务的类:
Sr.No. |
Class &Description |
1 |
.collapse Hides the content. |
2 |
.collapse.in Shows the content. |
3 |
.collapsing 当过渡开始时添加,并在过渡结束时移除。 |
你可以用以下两种方法使用合并插件:
-
Via data attributes - 向元素添加 data-toggle = "collapse" 和 data-target 以自动分配对可折叠元素的控制。 data-target 属性将接受一个 CSS 选择器以应用合并。务必向可折叠元素添加类 .collapse 。如果你希望其默认展开,则包含其他类 .in 。若要向可折叠控件添加类似手风琴的组管理,则添加数据属性 data-parent = "#selector" 。
-
Via JavaScript - 可以通过 JavaScript 激活合并方法,如下所示:
$('.collapse').collapse()
Options
可以通过数据属性或 JavaScript 传递某些选项,它们列在以下表中:
Option Name |
Type/Default Value |
Data attribute name |
Description |
parent |
selector Default − false |
data-parent |
如果选择器为 false,则指定父元素下的所有可折叠元素将被关闭(类似于传统的手风琴行为 - 这取决于手风琴组类)。 |
toggle |
boolean Default − true |
data-toggle |
调用时切换可折叠元素。 |
Methods
以下列出了与可折叠元素一起使用的一些有用的方法。
Method |
Description |
Example |
Options − .collapse(options) |
让你内容成为折叠元素。接受可选的选项对象。 |
$('#identifier').collapse({ toggle: false }) |
Toggle − .collapse('toggle') |
切换折叠元素,以显示或隐藏。 |
$('#identifier').collapse('toggle') |
Show − .collapse('show') |
Shows a collapsible element. |
$('#identifier').collapse('show') |
Hide − .collapse('hide') |
Hides a collapsible element. |
$('#identifier').collapse('hide') |
Example
以下示例演示了方法的使用 -
<div class = "panel-group" id = "accordion">
<div class = "panel panel-default">
<div class = "panel-heading">
<h4 class = "panel-title">
<a data-toggle = "collapse" data-parent = "#accordion" href = "#collapseOne">
Click me to expand. Click me again to collapse. Section 1--hide method
</a>
</h4>
</div>
<div id = "collapseOne" class = "panel-collapse collapse in">
<div class = "panel-body">
Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt
sapiente ea proident. Ad vegan excepteur butcher vice lomo.
</div>
</div>
</div>
<div class = "panel panel-success">
<div class = "panel-heading">
<h4 class = "panel-title">
<a data-toggle = "collapse" data-parent = "#accordion" href = "#collapseTwo">
Click me to expand. Click me again to collapse. Section 2--show method
</a>
</h4>
</div>
<div id = "collapseTwo" class = "panel-collapse collapse">
<div class = "panel-body">
Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt
sapiente ea proident. Ad vegan excepteur butcher vice lomo.
</div>
</div>
</div>
<div class = "panel panel-info">
<div class = "panel-heading">
<h4 class = "panel-title">
<a data-toggle = "collapse" data-parent = "#accordion" href = "#collapseThree">
Click me to expand. Click me again to collapse. Section 3--toggle method
</a>
</h4>
</div>
<div id = "collapseThree" class = "panel-collapse collapse">
<div class = "panel-body">
Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt
sapiente ea proident. Ad vegan excepteur butcher vice lomo.
</div>
</div>
</div>
<div class = "panel panel-warning">
<div class = "panel-heading">
<h4 class = "panel-title">
<a data-toggle = "collapse" data-parent = "#accordion" href = "#collapseFour">
Click me to expand. Click me again to collapse. Section 4--options method
</a>
</h4>
</div>
<div id = "collapseFour" class = "panel-collapse collapse">
<div class = "panel-body">
Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt
sapiente ea proident. Ad vegan excepteur butcher vice lomo.
</div>
</div>
</div>
</div>
<script type = "text/javascript">
$(function () { $('#collapseFour').collapse({
toggle: false
})});
$(function () { $('#collapseTwo').collapse('show')});
$(function () { $('#collapseThree').collapse('toggle')});
$(function () { $('#collapseOne').collapse('hide')});
</script>
Events
下表列出了几个可与折叠功能搭配使用的事件。
Event |
Description |
Example |
show.bs.collapse |
在调用展示方法后激活。 |
$('#identifier)。on('show.bs.collapse', function(){ // 执行操作}) |
shown.bs.collapse |
当折叠元素对用户可见时触发该事件(将等待 CSS 转换完成)。 |
$('#identifier)。on('shown.bs.collapse', function(){ // 执行操作}) |
hide.bs.collapse |
在调用 hide 实例方法时激活。 |
$('#identifier)。on('hide.bs.collapse', function(){ // 执行操作}) |
hidden.bs.collapse |
当折叠元素对用户隐藏时触发该事件(将等待 CSS 转换完成)。 |
$('#identifier)。on('hidden.bs.collapse', function(){ // 执行操作}) |
Example
以下示例演示了该事件的使用 -
<div class = "panel-group" id = "accordion">
<div class = "panel panel-info">
<div class = "panel-heading">
<h4 class = "panel-title">
<a data-toggle = "collapse" data-parent = "#accordion" href = "#collapseexample">
Click me to expand. Click me again to collapse. Section --shown event
</a>
</h4>
</div>
<div id = "collapseexample" class = "panel-collapse collapse">
<div class = "panel-body">
Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt
sapiente ea proident. Ad vegan excepteur butcher vice lomo.
</div>
</div>
</div>
</div>
<script type = "text/javascript">
$(function () {
$('#collapseexample').on('show.bs.collapse', function () {
alert('Hey, this alert shows up when you expand it');
})
});
</script>
Bootstrap - Carousel Plugin
Bootstrap 轮播是一种灵活、响应的方式,可在你网站上添加滑块。除了响应性之外,内容的灵活性也足够大,可让你使用图像、iframe、视频或几乎任何你可能需要的类型的其他内容。
Example
下面的一个简单幻灯片展示了一个通用组件,它可借助 Bootstrap 轮播插件,以轮播的方式循环浏览元素。要实现轮播,你只需使用标记添加代码即可。无需数据属性,只需要基于类的简单的开发即可。
<div id = "myCarousel" class = "carousel slide">
<!-- Carousel indicators -->
<ol class = "carousel-indicators">
<li data-target = "#myCarousel" data-slide-to = "0" class = "active"></li>
<li data-target = "#myCarousel" data-slide-to = "1"></li>
<li data-target = "#myCarousel" data-slide-to = "2"></li>
</ol>
<!-- Carousel items -->
<div class = "carousel-inner">
<div class = "item active">
<img src = "/bootstrap/images/slide1.png" alt = "First slide">
</div>
<div class = "item">
<img src = "/bootstrap/images/slide2.png" alt = "Second slide">
</div>
<div class = "item">
<img src = "/bootstrap/images/slide3.png" alt = "Third slide">
</div>
</div>
<!-- Carousel nav -->
<a class = "carousel-control left" href = "#myCarousel" data-slide = "prev">‹</a>
<a class = "carousel-control right" href = "#myCarousel" data-slide = "next">›</a>
</div>
Optional Captions
你可以使用 .carousel-caption 元素在任何 .item 内轻松添加幻灯片标题。只需将几乎任何可选项 HTML 放置在其中,它将自动对齐和格式化。以下示例展示了这一点 −
<div id = "myCarousel" class = "carousel slide">
<!-- Carousel indicators -->
<ol class = "carousel-indicators">
<li data-target = "#myCarousel" data-slide-to = "0" class = "active"></li>
<li data-target = "#myCarousel" data-slide-to = "1"></li>
<li data-target = "#myCarousel" data-slide-to = "2"></li>
</ol>
<!-- Carousel items -->
<div class = "carousel-inner">
<div class = "item active">
<img src = "/bootstrap/images/slide1.png" alt = "First slide">
<div class = "carousel-caption">This Caption 1</div>
</div>
<div class = "item">
<img src = "/bootstrap/images/slide2.png" alt = "Second slide">
<div class = "carousel-caption">This Caption 2</div>
</div>
<div class = "item">
<img src = "/bootstrap/images/slide3.png" alt = "Third slide">
<div class = "carousel-caption">This Caption 3</div>
</div>
</div>
<!-- Carousel nav -->
<a class = "carousel-control left" href = "#myCarousel" data-slide = "prev">‹</a>
<a class = "carousel-control right" href = "#myCarousel" data-slide = "next">›</a>+
</div>
Usage
-
Via data attributes − 使用数据属性轻松控制轮播的位置。属性 data-slide 接受关键字 prev 或 next,该关键字根据其当前位置改变幻灯片位置。使用 data-slide-to 向轮播 data-slide-to = "2" 传递原始幻灯片索引,将幻灯片位置从 0 开始移动到特定的索引。 data-ride = "carousel" 属性用于将轮播标记为从页面加载开始的动画。
-
Via JavaScript − 轮播可以使用 JavaScript 以如下方式手动调用 −
$('.carousel').carousel()
Options
以下表列出了可以通过数据属性或 JavaScript 传递的某些选项 −
Option Name |
Type/Default Value |
Data attribute name |
Description |
interval |
number Default − 5000 |
data-interval |
自动循环一个项目之间的时间量。如果为 false,轮播不会自动循环。 |
pause |
string Default − "hover" |
data-pause |
鼠标悬停时暂停轮播的循环,鼠标离开时恢复轮播的循环。 |
wrap |
boolean Default − true |
data-wrap |
轮播应连续循环还是有硬停止。 |
Methods
下面是一个可用于轮播代码的有用方法列表。
Method |
Description |
Example |
.carousel(options) |
使用可选的选项对象初始化滚动图库,并开始循环显示项目。 |
$('#identifier').carousel({ interval: 2000 }) |
.carousel('cycle') |
从左向右循环显示滚动图库中的项目。 |
$('#identifier').carousel('cycle') |
.carousel('pause') |
停止滚动图库项目。 |
$('#identifier')..carousel('pause') |
.carousel(number) |
使滚动图库循环到特定帧(从 0 开始,类似于数组)。 |
$('#identifier').carousel(number) |
.carousel('prev') |
循环到上一个项目。 |
$('#identifier').carousel('prev') |
.carousel('next') |
循环到下一个项目。 |
$('#identifier').carousel('next') |
Example
以下示例演示了方法的使用 -
<div id = "myCarousel" class = "carousel slide">
<!-- Carousel indicators -->
<ol class = "carousel-indicators">
<li data-target = "#myCarousel" data-slide-to = "0" class = "active"></li>
<li data-target = "#myCarousel" data-slide-to = "1"></li>
<li data-target = "#myCarousel" data-slide-to = "2"></li>
</ol>
<!-- Carousel items -->
<div class = "carousel-inner">
<div class = "item active">
<img src = "/bootstrap/images/slide1.png" alt = "First slide">
</div>
<div class = "item">
<img src = "/bootstrap/images/slide2.png" alt = "Second slide">
</div>
<div class = "item">
<img src = "/bootstrap/images/slide3.png" alt = "Third slide">
</div>
</div>
<!-- Carousel nav -->
<a class = "carousel-control left" href = "#myCarousel" data-slide = "prev">‹</a>
<a class = "carousel-control right" href = "#myCarousel" data-slide = "next">›</a>
<!-- Controls buttons -->
<div style = "text-align:center;">
<input type = "button" class = "btn prev-slide" value = "Previous Slide">
<input type = "button" class = "btn next-slide" value = "Next Slide">
<input type = "button" class = "btn slide-one" value = "Slide 1">
<input type = "button" class = "btn slide-two" value = "Slide 2">
<input type = "button" class = "btn slide-three" value = "Slide 3">
</div>
</div>
<script>
$(function() {
// Cycles to the previous item
$(".prev-slide").click(function() {
$("#myCarousel").carousel('prev');
});
// Cycles to the next item
$(".next-slide").click(function() {
$("#myCarousel").carousel('next');
});
// Cycles the carousel to a particular frame
$(".slide-one").click(function() {
$("#myCarousel").carousel(0);
});
$(".slide-two").click(function() {
$("#myCarousel").carousel(1);
});
$(".slide-three").click(function() {
$("#myCarousel").carousel(2);
});
});
</script>
Events
Bootstrap 的滚动图库类公开了两个事件,用于钩入滚动图库功能,如下表所示。
Event |
Description |
Example |
slide.bs.carousel |
此事件在调用幻灯片实例方法时立即触发。 |
$('#identifier').on('slide.bs.carousel', function () { // do something}) |
slid.bs.carousel |
当滚动图库完成其幻灯片转换时,触发此事件。 |
$('#identifier').on('slid.bs.carousel', function () { // do something}) |
Example
以下示例演示了该事件的使用 -
<div id = "myCarousel" class = "carousel slide">
<!-- Carousel indicators -->
<ol class = "carousel-indicators">
<li data-target = "#myCarousel" data-slide-to = "0" class = "active"></li>
<li data-target = "#myCarousel" data-slide-to = "1"></li>
<li data-target = "#myCarousel" data-slide-to = "2"></li>
</ol>
<!-- Carousel items -->
<div class = "carousel-inner">
<div class = "item active">
<img src = "/bootstrap/images/slide1.png" alt = "First slide">
</div>
<div class = "item">
<img src = "/bootstrap/images/slide2.png" alt = "Second slide">
</div>
<div class = "item">
<img src = "/bootstrap/images/slide3.png" alt = "Third slide">
</div>
</div>
<!-- Carousel nav -->
<a class = "carousel-control left" href = "#myCarousel" data-slide = "prev">‹</a>
<a class = "carousel-control right" href = "#myCarousel" data-slide = "next">›</a>
</div>
<script>
$(function() {
$('#myCarousel').on('slide.bs.carousel', function () {
alert("This event fires immediately when the slide instance method" +"is invoked.");
});
});
</script>
Bootstrap - Affix Plugin
定位插件允许将 <div> 固定到页面上的某个位置。你还可以使用此插件切换其固定状态。此项功能的典型示例是社交媒体图标。它们会从一个位置开始,但当页面达到某个标记时,<div>会被锁定,并停止随页面其他部分一起滚动。
Usage
你可以如下图通过数据属性或手动使用自己的 JavaScript 来使用定位插件。
-
Via data attributes − 要轻松地为任何元素添加定位行为,只需向想要监视的元素添加 data-spy = "affix" 。使用偏移量来定义何时切换对元素的固定。
Example
以下示例演示了通过数据属性进行使用 −
<div class = "container">
<div class = "jumbotron">
<h1>Bootstrap Affix Plugin example</h1>
</div>
<div id = "myNav" data-spy = "affix" data-offset-top = "60" data-offset-bottom = "200">
<div class = "col-md-3">
<ul class = "nav nav-tabs nav-stacked affix" data-spy = "affix" data-offset-top = "190">
<li class = "active"><a href = "#one">Tutorial One</a></li>
<li><a href = "#two">Tutorial Two</a></li>
<li><a href = "#three">Tutorial Three</a></li>
</ul>
</div>
<div class = "col-md-9">
<h2 id = "one">Tutorial One</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Nam eu sem tempor, varius quam at, luctus dui. Mauris magna metus,
dapibus nec turpis vel, semper malesuada ante. Vestibulum
id metus ac nisl bibendum scelerisque non non purus. Suspendisse
varius nibh non aliquet sagittis. In tincidunt orci sit amet
elementum vestibulum. Vivamus fermentum in arcu in aliquam. Quisque
aliquam porta odio in fringilla. Vivamus nisl leo, blandit at bibendum
eu, tristique eget risus. Integer aliquet quam ut elit suscipit,
id interdum neque porttitor. Integer faucibus ligula.</p>
<p>Vestibulum quis quam ut magna consequat faucibus.
Pellentesque eget nisi a mi suscipit tincidunt. Ut tempus dictum
risus. Pellentesque viverra sagittis quam at mattis. Suspendisse
potenti. Aliquam sit amet gravida nibh, facilisis gravida odio.
Phasellus auctor velit at lacus blandit, commodo iaculis justo
viverra. Etiam vitae est arcu. Mauris vel congue dolor. Aliquam eget
mi mi. Fusce quam tortor, commodo ac dui quis, bibendum viverra erat.
Maecenas mattis lectus enim, quis tincidunt dui molestie euismod.
Curabitur et diam tristique, accumsan nunc eu, hendrerit tellus.</p>
<hr>
<h2 id = "two">Tutorial Two</h2>
<p>Nullam hendrerit justo non leo aliquet imperdiet. Etiam in
sagittis lectus. Suspendisse ultrices placerat accumsan. Mauris quis
dapibus orci. In dapibus velit blandit pharetra tincidunt.
Quisque non sapien nec lacus condimentum facilisis ut iaculis enim.
Sed viverra interdum bibendum. Donec ac sollicitudin dolor. Sed
fringilla vitae lacus at rutrum. Phasellus congue vestibulum ligula sed consequat.</p>
<p>Vestibulum consectetur scelerisque lacus, ac fermentum lorem
convallis sed. Nam odio tortor, dictum quis malesuada at,
pellentesque vitae orci. Vivamus elementum, felis eu auctor lobortis,
diam velit egestas lacus, quis fermentum metus ante quis urna. Sed at
facilisis libero. Cum sociis natoque penatibus et magnis dis
parturient montes, nascetur ridiculus mus. Vestibulum bibendum
blandit dolor. Nunc orci dolor, molestie nec nibh in, hendrerit
tincidunt ante. Vivamus sem augue, hendrerit non sapien in,
mollis ornare augue.</p>
<hr>
<h2 id = "three">Tutorial Three</h2>
<p>Integer pulvinar leo id risus pellentesque vestibulum.
Sed diam libero, sodales eget sapien vel, porttitor bibendum enim.
Donec sed nibh vitae lorem porttitor blandit in nec ante.
Pellentesque vitae metus ipsum. Phasellus sed nunc ac sem malesuada
condimentum. Etiam in aliquam lectus. Nam vel sapien diam. Donec
pharetra id arcu eget blandit. Proin imperdiet mattis augue in
porttitor. Quisque tempus enim id lobortis feugiat. Suspendisse
tincidunt risus quis dolor fringilla blandit. Ut sed sapien at purus
lacinia porttitor. Nullam iaculis, felis a pretium ornare, dolor nisl
semper tortor, vel sagittis lacus est consequat eros. Sed id pretium
nisl. Curabitur dolor nisl, laoreet vitae aliquam id, tincidunt sit
amet mauris. </p>
<p>Phasellus vitae suscipit justo. Mauris pharetra feugiat ante
id lacinia. Etiam faucibus mauris id tempor egestas. Duis luctus
turpis at accumsan tincidunt. Phasellus risus risus,
volutpat vel tellus ac, tincidunt fringilla massa. Etiam hendrerit
dolor eget ante rutrum adipiscing. Cras interdum ipsum mattis,
tempus mauris vel, semper ipsum. Duis sed dolor ut enim lobortis
pellentesque ultricies ac ligula. Pellentesque convallis elit nisi, id
vulputate ipsum ullamcorper ut. Cras ac pulvinar purus, ac viverra est. Suspendisse
potenti. Integer pellentesque neque et elementum tempus.
Curabitur bibendum in ligula ut rhoncus.</p>
<p>Quisque pharetra velit id velit iaculis pretium. Nullam a justo
sed ligula porta semper eu quis enim. Pellentesque pellentesque,
metus at facilisis hendrerit, lectus velit facilisis leo, quis
volutpat turpis arcu quis enim. Nulla viverra lorem elementum
interdum ultricies. Suspendisse accumsan quam nec ante mollis tempus.
Morbi vel accumsan diam, eget convallis tellus. Suspendisse potenti.</p>
</div>
</div>
</div>
-
Via JavaScript − 可以按照如下所示通过 JavaScript 手动固定元素。
$('#myAffix').affix({
offset: {
top: 100, bottom: function () {
return (this.bottom = $('.bs-footer').outerHeight(true))
}
}
})
Example
以下示例演示了通过数据属性进行使用 −
<div class = "container">
<div class = "jumbotron">
<h1>Bootstrap Affix Plugin example</h1>
</div>
<div>
<div class = "col-md-3">
<ul class = "nav nav-tabs nav-stacked affix" id = "myNav">
<li class = "active"><a href = "#one">Tutorial One</a></li>
<li><a href = "#two">Tutorial Two</a></li>
<li><a href = "#three">Tutorial Three</a></li>
</ul>
</div>
<div class = "col-md-9">
<h2 id = "one">Tutorial One</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Nam eu sem tempor, varius quam at, luctus dui. Mauris magna metus,
dapibus nec turpis vel, semper malesuada ante. Vestibulum id metus
ac nisl bibendum scelerisque non non purus. Suspendisse varius nibh
non aliquet sagittis. In tincidunt orci sit amet elementum vestibulum.
Vivamus fermentum in arcu in aliquam. Quisque aliquam porta odio
in fringilla. Vivamus nisl leo, blandit at bibendum eu, tristique
eget risus. Integer aliquet quam ut elit suscipit, id interdum
neque porttitor. Integer faucibus ligula.</p>
<p>Vestibulum quis quam ut magna consequat faucibus.
Pellentesque eget nisi a mi suscipit tincidunt. Ut tempus dictum
risus. Pellentesque viverra sagittis quam at mattis. Suspendisse
potenti. Aliquam sit amet gravida nibh, facilisis gravida odio.
Phasellus auctor velit at lacus blandit, commodo iaculis justo
viverra. Etiam vitae est arcu. Mauris vel congue dolor.
Aliquam eget mi mi. Fusce quam tortor, commodo ac dui quis,
bibendum viverra erat. Maecenas mattis lectus enim, quis tincidunt
dui molestie euismod. Curabitur et diam tristique, accumsan nunc eu,
hendrerit tellus.</p>
<hr>
<h2 id = "two">Tutorial Two</h2>
<p>Nullam hendrerit justo non leo aliquet imperdiet. Etiam in
sagittis lectus. Suspendisse ultrices placerat accumsan. Mauris
quis dapibus orci. In dapibus velit blandit pharetra tincidunt.
Quisque non sapien nec lacus condimentum facilisis ut iaculis enim.
Sed viverra interdum bibendum. Donec ac sollicitudin dolor.
Sed fringilla vitae lacus at rutrum. Phasellus congue vestibulum
ligula sed consequat.</p>
<p>Vestibulum consectetur scelerisque lacus, ac fermentum lorem
convallis sed. Nam odio tortor, dictum quis malesuada at,
pellentesque vitae orci. Vivamus elementum, felis eu auctor lobortis,
diam velit egestas lacus, quis fermentum metus ante quis urna.
Sed at facilisis libero. Cum sociis natoque penatibus et magnis dis
parturient montes, nascetur ridiculus mus. Vestibulum bibendum
blandit dolor. Nunc orci dolor, molestie nec nibh in, hendrerit
tincidunt ante. Vivamus sem augue, hendrerit non sapien in,
mollis ornare augue.</p>
<hr>
<h2 id = "three">Tutorial Three</h2>
<p>Integer pulvinar leo id risus pellentesque vestibulum.
Sed diam libero, sodales eget sapien vel, porttitor bibendum enim.
Donec sed nibh vitae lorem porttitor blandit in nec ante.
Pellentesque vitae metus ipsum. Phasellus sed nunc ac sem malesuada
condimentum. Etiam in aliquam lectus. Nam vel sapien diam.
Donec pharetra id arcu eget blandit. Proin imperdiet mattis augue in
porttitor. Quisque tempus enim id lobortis feugiat. Suspendisse
tincidunt risus quis dolor fringilla blandit. Ut sed sapien at
purus lacinia porttitor. Nullam iaculis, felis a pretium ornare,
dolor nisl semper tortor, vel sagittis lacus est consequat eros.
Sed id pretium nisl. Curabitur dolor nisl, laoreet vitae aliquam id,
tincidunt sit amet mauris.</p>
<p>Phasellus vitae suscipit justo. Mauris pharetra feugiat ante id
lacinia. Etiam faucibus mauris id tempor egestas. Duis luctus turpis
at accumsan tincidunt. Phasellus risus risus, volutpat vel tellus ac,
tincidunt fringilla massa. Etiam hendrerit dolor eget ante
rutrum adipiscing. Cras interdum ipsum mattis, tempus mauris vel,
semper ipsum. Duis sed dolor ut enim lobortis pellentesque ultricies
ac ligula. Pellentesque convallis elit nisi, id vulputate ipsum
ullamcorper ut. Cras ac pulvinar purus, ac viverra est. Suspendisse
potenti. Integer pellentesque neque et elementum tempus.
Curabitur bibendum in ligula ut rhoncus.</p>
<p>Quisque pharetra velit id velit iaculis pretium. Nullam a justo
sed ligula porta semper eu quis enim. Pellentesque pellentesque,
metus at facilisis hendrerit, lectus velit facilisis leo, quis
volutpat turpis arcu quis enim. Nulla viverra lorem elementum
interdum ultricies. Suspendisse accumsan quam nec ante mollis tempus.
Morbi vel accumsan diam, eget convallis tellus. Suspendisse potenti.</p>
</div>
</div>
</div>
<script type = "text/javascript">
$(function () {
$('#myNav').affix({
offset: {
top: 60
}
});
});
</script>
Positioning via CSS
在这两种情况下,你都必须提供内容定位的 CSS。定位插件在三个类之间切换,每个类表示一个特定状态 − .affix、.affix-top 和 .affix-bottom。请按照以下步骤设置任一上述使用选项的 CSS。
-
首先,插件添加 .affix-top 以指示元素处于其最高位置。此时不需要 CSS 定位。
-
滚动经过想要固定的元素应触发实际固定。这是 .affix 替换 .affix-top 并设置 position: fixed; (由 Bootstrap 的代码 CSS 提供)的位置。
-
如果定义了底部偏移量,向后滚动应将 .affix 替换为 .affix-bottom 。由于偏移量是可选的,因此设置一个偏移量要求您设置适当的 CSS。在这种情况下,在必要时添加 position: absolute; 。
Bootstrap - Grid Demo
Working of Bootstrap Grid System
网格系统用于通过包含您内容的一系列行和列创建页面布局。以下是如何使用 Bootstrap 网格系统 −
-
必须将行放入 .container 类中以进行适当的对齐和填充。
-
使用行创建列的水平组。
-
内容应置于列内,并且只有列才能成为行的直接子元素。
-
.row and .col-xs-4 等预定义网格类可用于快速生成网格布局。LESS 混入也可用于更语义化的布局。
-
列通过填充创建间隔(列内容之间的间隙)。该填充通过 .rows 对第一列和最后一列有负的利润来对齐的。
-
网格列是通过指定希望跨越的 12 个可用列的数量来创建的。例如,三列相等将使用三个 .col-xs-4 。
Example |
Description |
Download link |
此示例表示 Bootstrap 中网格结构。 |
Bootstrap - Table Demo
Bootstrap 提供了构建表的简洁布局。Bootstrap 支持的部分表格元素包括:
Sr.No. |
Tag & Description |
1 |
<table> 用于以表格格式显示数据的包装元素 |
2 |
<thead> 表头行(<tr>)的容器元素,用于标记表格列。 |
3 |
<tbody> 表格主体中表行(<tr>)的容器元素。 |
4 |
<tr> 出现在单行中的一组表单元格(<td>或<th>)的容器元素。 |
5 |
<td> Default table cell. |
6 |
<th> 专用于列(或行,具体取决于范围和放置)标签的特殊表单元格。必须在 <thead> 中使用 |
7 |
<caption> 表所包含内容的描述或摘要。 |
Example |
Description |
Download link |
此示例指示 Bootstrap 中的购买表结构。 |
||
此示例指示 Bootstrap 中的计划表结构。 |
||
此示例指示 Bootstrap 中的画廊表结构。 |
Bootstrap - Form Demo
Form Layout
Bootstrap 为您提供了以下类型的表单布局 -
-
Vertical (default) form
-
In-line form
-
Horizontal form
Vertical or Basic Form
基本表单结构附带 Bootstrap;各个表单控件自动接收一些全局样式。若要创建基本表单,请执行以下操作 -
-
将角色表单添加到父级 <form>元素。
-
使用类 .form-group 将标签和控件包裹在 <div>中。这对最佳间距来说是必需的。
-
向所有文本 <input>、<textarea> 和 <select> 元素添加 .form-control 类。
Bootstrap - Buttons Demo
Bootstrap 提供了一些样式按钮的选项,这些选项汇总在以下表格中-
Sr.No. |
Class & Description |
1 |
btn Default/ Standard button. |
2 |
btn-primary 提供额外的视觉权重,并标识一系列按钮中的主要操作。 |
3 |
btn-success 表示成功或积极的操作。 |
4 |
btn-info 用于信息警报消息的上下文按钮。 |
5 |
btn-warning 表示此操作应谨慎执行。 |
6 |
btn-danger 表示危险或潜在消极的操作。 |
7 |
btn-link 通过让按钮看起来像链接但保持按钮行为来降低按钮的重要性。 |
Example |
Description |
Download link |
此示例指示 Bootstrap 中按钮结构。 |
||
此示例指示 Bootstrap 中的图像按钮结构 |
||
此示例指示 Bootstrap 中的阴影按钮结构 |
||
此示例指示 Bootstrap 中的注册按钮结构 |
Bootstrap - Images Demo
Bootstrap 提供了三个类,可用于对图像应用一些简单的样式-
-
.img-rounded − adds border-radius:6px to give the image rounded corners.
-
.img-circle − makes the entire image round by adding border-radius:500px.
-
.img-thumbnail − adds a bit of padding and a gray border:
Example |
Description |
Download link |
此示例指示 Bootstrap 中的图像库结构 |
||
此示例指示 Bootstrap 中的图像缩放结构 |
||
此示例指示 Bootstrap 中的网格图像结构 |
||
这个例子说明了 Bootstrap 中的背景图片结构 |
||
这个例子说明了 Bootstrap 中的图片结构 |
Bootstrap - Responsive Demo
Bootstrap 提供了一些有用的辅助类,用于快速移动端友好开发。这些辅助类可以通过媒体查询,结合大型、小型和中型设备,用于通过设备显示和隐藏内容。
谨慎使用这些辅助类,避免创建完全不同的网站版本。 Responsive utilities are currently only available for block and table toggling 。
Classes |
Devices |
.visible-xs |
超小(小于 768px)可见 |
.visible-sm |
小(高达 768 px)可见 |
.visible-md |
中(768 px 到 991 px)可见 |
.visible-lg |
大(992 px 及以上)可见 |
.hidden-xs |
超小(小于 768px)隐藏 |
.hidden-sm |
小(高达 768 px)隐藏 |
.hidden-md |
中(768 px 到 991 px)隐藏 |
.hidden-lg |
大(992 px 及以上)隐藏 |
Example |
Description |
Download link |
这个例子说明了 Bootstrap 中的卡片结构 |
||
这个例子说明了 Bootstrap 中的评价结构 |
||
这个例子说明了 Bootstrap 中的网格视频结构 |
||
这个例子说明了 Bootstrap 中的横幅图片结构 |
||
这个例子说明了 Bootstrap 中带有面部指示符的轮播结构 |
Bootstrap - Tabbed Slider Demo
Bootstrap提供图表组件给不同的选项。以下显示的是带标签的样式的图表 −
Example |
Description |
Download link |
此示例表示Bootstrap中的标签式图表 |
Bootstrap - Caption Demo
Bootstrap为标题提供了一些不同的选项。缩略图标题悬停效果如下图所示 −
Example |
Description |
Download link |
此示例指代Bootstrap中的图像标题 |
Bootstrap - Map Demo
Bootstrap为地图提供了一些不同的选项。地图示例如下图所示 −
Example |
Description |
Download Link |
此示例指代Bootstrap中的地图 |
Bootstrap - Featured Demo
Bootstrap 为设计 Bootstrap 元素提供了一些不同的选项。一些 Bootstrap 元素如下所示 −
Example |
Description |
Download link |
此示例说明 Bootstrap 中的“材料设计”开关结构 |
||
此示例说明 Bootstrap 中的 Windows 8 Bootstrap 模态结构 |
||
此示例说明 Bootstrap 中的功能区按钮结构 |
||
此示例说明 Bootstrap 中的标注 - 行动号召结构 |
||
此示例说明 Bootstrap 中的报告错误结构 |
||
此示例说明 Bootstrap 中的带有悬停标题的轮播结构 |
||
此示例说明 Bootstrap 中的 FaceBook 墙结构 |
||
此示例说明 Bootstrap 中的移动警报结构 |
||
此示例说明 Bootstrap 中的圆角分页 |
||
此示例说明 Bootstrap 中的视频列表缩略图 |
||
此示例说明 Bootstrap 中的媒体控制按钮 |
||
此示例说明 Bootstrap 中的简单垂直选项卡 |