Home Web Front-end JS Tutorial Detailed explanation of Bootstrap framework (code example)

Detailed explanation of Bootstrap framework (code example)

Oct 15, 2018 pm 05:17 PM

This article brings you a detailed explanation (code example) of the Bootstrap framework. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

(Free course recommendation: bootstrap tutorial)

Bootstrap introduction

Bootstrap is Twitter’s open source HTML-based , CSS, JavaScript front-end framework.

It is a set of front-end toolkit designed for rapid development of web applications.

It supports responsive layout and adheres to mobile device priority after the V3 version.

Why use Bootstrap?

Before Bootstrap appeared:

Naming: repetitive, complex, meaningless (it’s hard to come up with a name)

Style: repetitive, redundant, non-standard, Disharmonious

Page: disordered, irregular, disharmonious

After using Bootstrap: Various naming are unified and standardized. The page style is unified and the picture is harmonious.

Bootstrap download

Official address: https://getbootstrap.com

Chinese address: http://www.bootcss.com/

We use the V3 version of Bootstrap, and we downloaded Bootstrap for the production environment.

Bootstrap environment construction

Directory structure:

bootstrap-3.3.7-dist/
├── css  // CSS文件
│   ├── bootstrap-theme.css  // Bootstrap主题样式文件
│   ├── bootstrap-theme.css.map
│   ├── bootstrap-theme.min.css  // 主题相关样式压缩文件
│   ├── bootstrap-theme.min.css.map
│   ├── bootstrap.css
│   ├── bootstrap.css.map
│   ├── bootstrap.min.css  // 核心CSS样式压缩文件
│   └── bootstrap.min.css.map
├── fonts  // 字体文件
│   ├── glyphicons-halflings-regular.eot
│   ├── glyphicons-halflings-regular.svg
│   ├── glyphicons-halflings-regular.ttf
│   ├── glyphicons-halflings-regular.woff
│   └── glyphicons-halflings-regular.woff2
└── js  // JS文件
    ├── bootstrap.js
    ├── bootstrap.min.js  // 核心JS压缩文件
    └── npm.js
Copy after login

Handling dependencies

Since some components of Bootstrap depend on jQuery, so Please make sure to download the corresponding version of jQuery file to ensure that Bootstrap related components run properly.

Bootstrap Global Style

For our commonly used HTML elements such as layout, buttons, tables, forms, pictures, etc., Bootstrap provides global styles.

We can apply Bootstrap styles by setting classes on basic HTML elements, thereby making our pages more beautiful and harmonious.

Title related

Title

<h1>一级标题36px</h1>
<h2>二级标题30px</h2>
<h3>三级标题24px</h3>
<h4>四级标题18px</h4>
<h5>五级标题14px</h5>
<h6>六级标题12px</h6>
<!--除了使用h标签,Bootstrap内置了相应的全局样式-->
<!--内联标签应用标题样式-->
<span class="h1">一级标题36px</span>
<span class="h2">二级标题30px</span>
<span class="h3">三级标题24px</span>
<span class="h4">四级标题18px</span>
<span class="h5">五级标题14px</span>
<span class="h6">六级标题12px</span>
Copy after login

Subtitle

<!--一级标题中嵌入小标题-->
<h1>一级标题<small>小标题</small></h1>
Copy after login

Text Alignment

<!--文本对齐-->
<p class="text-left">文本左对齐</p>
<p class="text-center">文本居中</p>
<p class="text-right">文本右对齐</p>
Copy after login

##Text Case

<!--大小写-->
<p class="text-lowercase">Lowercased text.</p>
<p class="text-uppercase">Uppercased text.</p>
<p class="text-capitalize">Capitalized text.</p>
Copy after login

Table

##Class.table-striped.table-bordered.table-hover.table-condensed.table-responsive
Description
Striped table
With border The table
The table that changes color when hovering the mouse
Compact table
Responsive table

State Class

Class.active.success.info.warning.danger

表单

内联表单

表单状态

带图标的表单

按钮

<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">
Copy after login

按钮样式

<!-- Standard button -->
<button type="button" class="btn btn-default">(默认样式)Default</button>
<!-- Provides extra visual weight and identifies the primary action in a set of buttons -->
<button type="button" class="btn btn-primary">(首选项)Primary</button>
<!-- Indicates a successful or positive action -->
<button type="button" class="btn btn-success">(成功)Success</button>
<!-- Contextual button for informational alert messages -->
<button type="button" class="btn btn-info">(一般信息)Info</button>
<!-- Indicates caution should be taken with this action -->
<button type="button" class="btn btn-warning">(警告)Warning</button>
<!-- Indicates a dangerous or potentially negative action -->
<button type="button" class="btn btn-danger">(危险)Danger</button>
<!-- Deemphasize a button by making it look like a link while maintaining button behavior -->
<button type="button" class="btn btn-link">(链接)Link</button>
Copy after login

按钮大小

<p>
  <button type="button" class="btn btn-primary btn-lg">(大按钮)Large button</button>
  <button type="button" class="btn btn-default btn-lg">(大按钮)Large button</button>
</p>
<p>
  <button type="button" class="btn btn-primary">(默认尺寸)Default button</button>
  <button type="button" class="btn btn-default">(默认尺寸)Default button</button>
</p>
<p>
  <button type="button" class="btn btn-primary btn-sm">(小按钮)Small 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 button</button>
  <button type="button" class="btn btn-default btn-xs">(超小尺寸)Extra small button</button>
</p>
Copy after login

图片

<img src="..." class="img-responsive" alt="Responsive image">
Copy after login

图片形状

<img src="..." alt="..." class="img-rounded">
<img src="..." alt="..." class="img-circle">
<img src="..." alt="..." class="img-thumbnail">
Copy after login

辅助类

文本颜色

<p class="text-muted">...</p>
<p class="text-primary">...</p>
<p class="text-success">...</p>
<p class="text-info">...</p>
<p class="text-warning">...</p>
<p class="text-danger">...</p>
Copy after login

背景颜色

<p class="bg-primary">...</p>
<p class="bg-success">...</p>
<p class="bg-info">...</p>
<p class="bg-warning">...</p>
<p class="bg-danger">...</p>
Copy after login

关闭按钮

<button type="button" class="close" aria-label="Close"><span aria-hidden="true">&times;</span></button>
Copy after login

下拉三角

<span class="caret"></span>
Copy after login

快速浮动

<div class="pull-left">...</div>
<div class="pull-right">...</div>
Copy after login

内容块居中

<div class="center-block">...</div>
Copy after login

清除浮动

<!-- Usage as a class -->
<div class="clearfix">...</div>
Copy after login

显示与隐藏

<div class="show">...</div>
<div class="hidden">...</div>
Copy after login

常用Bootstrap组件

  1. 字体图标

  2. 下拉菜单

  3. 按钮组

  4. 输入框俎

  5. 导航

  6. 分页

  7. 标签和徽章

  8. 页头

  9. 缩率图

  10. 进度条

模拟滚动的进度条:

var $d1 = $("#d1");
var width = 0;
var theID = setInterval(setValue, 200);
function setValue() {  
if (width === 100)
 {
    clearInterval(theID);
  } else {
    width++;
    $d1.css("width", width+"%").text(width+"%");
  }
}
Copy after login

响应式开发

为什么要进行响应式开发?

随着移动设备的流行,网页设计必须要考虑到移动端的设计。同一个网站为了兼容PC端和移动端显示,就需要进行响应式开发。

什么是响应式?

利用媒体查询,让同一个网站兼容不同的终端(PC端、移动端)呈现不同的页面布局。

用到的技术:

CSS3@media查询

用于查询设备是否符合某一特定条件,这些特定条件包括屏幕尺寸、是否可触摸、屏幕精度、横屏竖屏等信息。

常见属性:

  • device-width, device-height 屏幕宽、高

  • width,height 渲染窗口宽、高

  • orientation 设备方向

  • resolution 设备分辨率

语法:

@media mediatype and|not|only (media feature) {
    CSS-Code;
}
Copy after login

不同的媒体使用不同的stylesheet

<link rel="stylesheet" media="mediatype and|not|only (media feature)" href="mystylesheet.css">
Copy after login

viewport

手机浏览器是把页面放在一个虚拟的"窗口"(viewport)中,通常这个虚拟的"窗口"(viewport)比屏幕宽,这样就不用把每个网页挤到很小的窗口中(这样会破坏没有针对手机浏览器优化的网页的布局),用户可以通过平移和缩放来看网页的不同部分。

设置viewport

一个常用的针对移动网页优化过的页面的 viewport meta 标签大致如下:

<meta name=”viewport” content=”width=device-width, initial-scale=1, maximum-scale=1″>
Copy after login

  • width:控制 viewport 的大小,可以指定的一个值,如果 600,或者特殊的值,如 device-width 为设备的宽度(单位为缩放为 100% 时的 CSS 的像素)。

  • height:和 width 相对应,指定高度。

  • initial-scale:初始缩放比例,也即是当页面第一次 load 的时候缩放比例。

  • maximum-scale:允许用户缩放到的最大比例。

  • minimum-scale:允许用户缩放到的最小比例。

  • user-scalable:用户是否可以手动缩放。

Bootstrap的栅格系统

  • container

  • row

  • column

注意事项: 使用Bootstrap的时候不要让自己的名字与Bootstrap的类名冲突。

JavaScript插件

下面常用的Bootatstrap自带插件

模态框

模态框的HTML代码放置的位置

务必将模态框的HTML代码放在文档的最高层级内(也就是说,尽量作为 body 标签的直接子元素),以避免其他组件影响模态框的展现和/或功能。

HTML代码:

<!-- 触发模态框的按钮 -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Launch demo modal
</button>

<!-- 模态框 -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>
Copy after login

注意:

通过为模态框设置 .bs-example-modal-lg和 .bs-example-modal-sm来控制模态框的大小。

通过 .fade类来控制模态框弹出时的动画效果(淡入淡出效果)。

通过在 .modal-bodyp中设置 .row可以使用Bootstrap的栅格系统。

调用方式:

1.通过data属性

通过在一个触发弹出模态框的元素(例如:按钮)上添加 <span class="pln">data<span class="pun">-<span class="pln">toggle<span class="pun">=<span class="str">"modal"</span></span></span></span></span>属性,然后设置 <span class="pln">data<span class="pun">-<span class="pln">target<span class="pun">=<span class="str">"#foo"</span></span></span></span></span>属性或 <span class="pln">href<span class="pun">=<span class="str">"#foo"</span></span></span>属性,用来指向被控制的模态框。

<button type="button" data-toggle="modal" data-target="#myModal">显示模态框</button>
Copy after login

2.通过JS代码调用

$(&#39;#myModal&#39;).modal("show");  // 显示
$(&#39;#myModal&#39;).modal("hide")   // 隐藏
Copy after login

常用参数:

Description
The color set when the mouse is hovering over a row or cell
Identifies a successful or positive action
Identifies a general prompt message or action
Identifies a warning or requires user attention
Identify dangerous or potentially negative actions
名称可选值默认值描述
backdroptrue/false/'static'truefalse表示有没有遮罩层,'static'表示点击遮罩层不关闭模态框
keyboardtrue/falsetrue键盘上的 esc 键被按下时关闭模态框。
showtrue/falsetrue模态框初始化之后就立即显示出来。

方法:

$(&#39;#myModal&#39;).modal({
  keyboard: false
})
Copy after login

轮播图

HTML代码:

<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
  <!-- Indicators -->
  <ol class="carousel-indicators">
    <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
    <li data-target="#carousel-example-generic" data-slide-to="1"></li>
    <li data-target="#carousel-example-generic" data-slide-to="2"></li>
  </ol>
  <!-- Wrapper for slides -->
  <div class="carousel-inner" role="listbox">
    <div class="item active">
      <img src="..." alt="...">
      <div class="carousel-caption">
        ...
      </div>
    </div>
    <div class="item">
      <img src="..." alt="...">
      <div class="carousel-caption">
        ...
      </div>
    </div>
    ...
  </div>
  <!-- Controls -->
  <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
    <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>
Copy after login

可以在为图片添加介绍信息:

<div class="item">
  <img src="..." alt="...">
  <div class="carousel-caption">
    <h3>...</h3>
    <p>...</p>
  </div>
</div>
Copy after login

方法:

设置切换间隔为2秒,默认是5秒。

$(&#39;.carousel&#39;).carousel({
  interval: 2000
})
Copy after login

其他常用插件

FontAwesome字体

Font Awesome

参考网址 : https://fontawesome.com/?from=io

详细用法参见上述站点的Examples。

SweetAlert系列

SweetAlert

参考网址 : https://github.com/t4t5/sweetalert

SweetAlert2

参考网址 : https://github.com/sweetalert2/sweetalert2

SweetAlert 到 SweetAlert2 升级指南

参考网址 : https://github.com/sweetalert2/sweetalert2/wiki/Migration-from-SweetAlert-to-SweetAlert2

示例:

基本使用:

  1. swal("标题","内容","success);

使用SweetAlert在Ajax提交成功(done)或失败(error)时分别提示不用的内容。

这是更新之前版本的写法

function deleteRecord(recordID) {
    swal({
        title: "确定要删除这个产品吗?",
        text: "删除后可就无法恢复了。",
        type: "warning",
        showCancelButton: true,
        closeOnConfirm: false,
        confirmButtonText: "是的,我要删除!",
        confirmButtonColor: "#ec6c62",
        cancelButtonText: "容我三思"
    }, function (isConfirm) {
        if (!isConfirm) return;
        $.ajax({
            type: "post",
            url: "/delete/",
            data: {"id": recordID},
            success: function (data) {
                var dataObj = $.parseJSON(data);
                if (dataObj.status === 0) { //后端删除成功
                    swal("删除成功", dataObj.info, "success");
                    $("#p-" + recordID).remove()  //删除页面中那一行数据
                } else {
                    swal("出错啦。。。", dataObj.msg, "error");  //后端删除失败
                }
            },
            error: function () {  // ajax请求失败
                swal("啊哦。。。", "服务器走丢了。。。", "error");
            }
        })
    });
}
Copy after login

更新之后用这么写

swal({
  title: "这里写标题",
  text: "这里放内容",
  icon: "warning",  // 这里放图标的类型(success,warning,info,error)
  buttons: {
    cancel: {
      text: "容我三思",
      visible: true,
      value: false
    },
    confirm: {
      text: "我就是要删除"
    }
  }
}).then(function (isConfirm) {
    if (isConfirm){
      swal("你死定了", {button: "确认"});
    }
Copy after login

Toastr通知

toastr["success"]("你已经成功被绿!")
Copy after login

jQueryLazyload懒加载

示例:

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta http-equiv="x-ua-compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>懒加载示例</title>
</head>
<body>
<div>
  <div><img src="img/0.jpg" alt="" data-original="img/5.jpg" width="600px" height="400px"></div>
  ...
  <div><img src="img/0.jpg" alt="" data-original="img/6.jpg" width="600px" height="400px"></div>
</div>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script src="jquery.lazyload.min.js"></script>
<script>
  $("img.lazy").lazyload({
    effect: "fadeIn",
    event: "click"
  })
</script>
</body>
</html>
Copy after login

The above is the detailed content of Detailed explanation of Bootstrap framework (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

TypeScript for Beginners, Part 2: Basic Data Types TypeScript for Beginners, Part 2: Basic Data Types Mar 19, 2025 am 09:10 AM

Once you have mastered the entry-level TypeScript tutorial, you should be able to write your own code in an IDE that supports TypeScript and compile it into JavaScript. This tutorial will dive into various data types in TypeScript. JavaScript has seven data types: Null, Undefined, Boolean, Number, String, Symbol (introduced by ES6) and Object. TypeScript defines more types on this basis, and this tutorial will cover all of them in detail. Null data type Like JavaScript, null in TypeScript

Can PowerPoint run JavaScript? Can PowerPoint run JavaScript? Apr 01, 2025 pm 05:17 PM

JavaScript can be run in PowerPoint, and can be implemented by calling external JavaScript files or embedding HTML files through VBA. 1. To use VBA to call JavaScript files, you need to enable macros and have VBA programming knowledge. 2. Embed HTML files containing JavaScript, which are simple and easy to use but are subject to security restrictions. Advantages include extended functions and flexibility, while disadvantages involve security, compatibility and complexity. In practice, attention should be paid to security, compatibility, performance and user experience.

See all articles