1. 程式人生 > >Bootstrap入門

Bootstrap入門

大小寫 樣式 框架 eight 模態框 插件 -h 錯亂 最小

********Bootstrap框架********

****Bootstrap介紹****

Bootstrap是Twitter開源的基於HTML、CSS、JavaScript的前端框架。
它是為實現快速開發Web應用程序而設計的一套前端工具包。
它支持響應式布局,並且在V3版本之後堅持移動設備優先。

****為什麽要使用Bootstrap?****

在Bootstrap出現之前:
命名:重復、復雜、無意義(想個名字費勁)
樣式:重復、冗余、不規範、不和諧
頁面:錯亂、不規範、不和諧
在使用Bootstrap之後: 各種命名都統一並且規範化。 頁面風格統一,畫面和諧。

****Bootstrap下載****

官方地址:https://getbootstrap.com
中文地址:http://www.bootcss.com/

****Bootstrap環境搭建****

目錄結構:

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

處理依賴
由於Bootstrap的某些組件依賴於jQuery,所以請確保下載對應版本的jQuery文件,來保證Bootstrap相關組件運行正常。


****Bootstrap全局樣式****

排版、按鈕、表格、表單、圖片等我們常用的HTML元素,Bootstrap中都提供了全局樣式。
我們只要在基本的HTML元素上通過設置class就能夠應用上Bootstrap的樣式,從而使我們的頁面更美觀和諧。

**標題相關**

*標題*

<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>

*副標題*

<!--一級標題中嵌入小標題-->
<h1>一級標題<small>小標題</small></h1>

**文本對齊**

<!--文本對齊-->
<p class="text-left">文本左對齊</p>
<p class="text-center">文本居中</p>
<p class="text-right">文本右對齊</p>

**文本大小寫**

<!--大小寫-->
<p class="text-lowercase">Lowercased text.</p>
<p class="text-uppercase">Uppercased text.</p>
<p class="text-capitalize">Capitalized text.</p>

**表格**

    Class              描述
.table-striped        條紋狀表格
.table-bordered        帶邊框的表格
.table-hover        鼠標懸停變色的表格
.table-condensed    緊縮型表格
.table-responsive    響應式表格

**狀態類**

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">

*按鈕樣式*

<!-- 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>

*按鈕大小*

<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>

**圖片**

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

*圖片形狀*

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

**輔助類**

*文本顏色*

<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>

*背景顏色*

<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>



*關閉按鈕*

<button type="button" class="close" aria-label="Close"><span aria-hidden="true">×</span></button>

*下拉三角*

<span class="caret"></span>

*快速浮動*

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

*內容塊居中*

<div class="center-block">...</div>

*清除浮動*

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

*顯示與隱藏*

<div class="show">...</div>
<div class="hidden">...</div>


*******常用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+"%");
  }
}


********響應式開發********

****為什麽要進行響應式開發?****
隨著移動設備的流行,網頁設計必須要考慮到移動端的設計。同一個網站為了兼容PC端和移動端顯示,就需要進行響應式開發。

****什麽是響應式?****
利用媒體查詢,讓同一個網站兼容不同的終端(PC端、移動端)呈現不同的頁面布局。

****用到的及時:****

CSS3@media查詢
用於查詢設備是否符合某一特定條件,這些特定條件包括屏幕尺寸、是否可觸摸、屏幕精度、橫屏豎屏等信息。

常見屬性:

device-width, device-height 屏幕寬、高
width,height 渲染窗口寬、高
orientation 設備方向
resolution 設備分辨率
語法:

@media mediatype and|not|only (media feature) {
    CSS-Code;
}
不同的媒體使用不同的stylesheet

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

手機瀏覽器是把頁面放在一個虛擬的"窗口"(viewport)中,通常這個虛擬的"窗口"(viewport)比屏幕寬,這樣就不用
把每個網頁擠到很小的窗口中(這樣會破壞沒有針對手機瀏覽器優化的網頁的布局),用戶可以通過平移和縮放來看網頁的不
同部分。

設置viewport

一個常用的針對移動網頁優化過的頁面的 viewport meta 標簽大致如下:

<meta name=”viewport” content=”width=device-width, initial-scale=1, maximum-scale=1″>
width:控制 viewport 的大小,可以指定的一個值,如果 600,或者特殊的值,如 device-width 為設備的寬度(單位為縮放為 100% 時的 CSS 的像素)。
height:和 width 相對應,指定高度。
initial-scale:初始縮放比例,也即是當頁面第一次 load 的時候縮放比例。
maximum-scale:允許用戶縮放到的最大比例。
minimum-scale:允許用戶縮放到的最小比例。
user-scalable:用戶是否可以手動縮放。
Bootstrap的柵格系統

container
row
column
註意事項: 使用Bootstrap的時候不要讓自己的名字與Bootstrap的類名沖突。


********JavaScrip插件********

****常用的Bootstrap自帶插件****

**模態框**

模態框的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">×</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>

註意事項:
1.通過為模態框設置 .bs-example-modal-lg和 .bs-example-modal-sm來控制模態框的大小。
2.通過 .fade類來控制模態框彈出時的動畫效果(淡入淡出效果)。
3.通過在 .modal-bodydiv中設置 .row可以使用Bootstrap的柵格系統。

*調用方式*

1.通過data屬性

通過在一個觸發彈出模態框的元素(例如:按鈕)上添加 data-toggle="modal"屬性,然後設置 data-target="#foo"屬性或 href="#foo"屬性,用來指向被控制的模態框。

<button type="button" data-toggle="modal" data-target="#myModal">顯示模態框</button>

2.通過JS代碼調用

$(‘#myModal‘).modal("show");  // 顯示
$(‘#myModal‘).modal("hide")   // 隱藏

常用參數:

名稱                可選值            默認值                描述
backdrop    true/false/‘static‘    true    false表示有沒有遮罩層,‘static‘表示點擊遮罩層不關閉模態框
keyboard    true/false            true    鍵盤上的 esc 鍵被按下時關閉模態框。
show        true/false            true    模態框初始化之後就立即顯示出來。

方法:
$(‘#myModal‘).modal({
  keyboard: false
})

**輪播圖**

*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>

可以再為圖片添加介紹信息

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

方法:

設置切換間隔為2秒,默認是5秒。

$(‘.carousel‘).carousel({
  interval: 2000
})

****其他常用插件****

待續。。。

********Bootstrap實例精選:********

1.封面圖
2.Carousel
3.博客頁面
4.控制臺
5.登錄頁
6.Offcanvas

  

Bootstrap入門