這篇文章全面的為大家解析了bootstrap格子布局的相關(guān)資料,感興趣的朋友可以參考一下
一、源碼文件
_grid.scss:格子系統(tǒng)類文件
Mixins/_grid.scss:支持格子系統(tǒng)實(shí)現(xiàn)的mixin集合
Mixins/_grid-framework.scss:格子系統(tǒng)實(shí)現(xiàn)的核心mixin
二、支持的功能
1. 實(shí)現(xiàn)按百分比布局
2. 實(shí)現(xiàn)格子的定位
3. 實(shí)現(xiàn)格子的嵌套
4. 如果只使用格子系統(tǒng),可以只編碼bootstrap-grid.scss文件
三、實(shí)現(xiàn)原理
1、 按百分比布局,主要思考的問題如何在不同的設(shè)備上平均分配的寬度,bootstrap只是用了簡單的百分比,在任何尺寸設(shè)備下都是使用相同的百分比。
2、 格子的定位:解決了格子向左移動(dòng)、向右移動(dòng)、以格子向右偏移幾個(gè)單元格的能力
3、 格子的嵌套:實(shí)現(xiàn)了格子內(nèi)容再嵌套格子布局系統(tǒng)。
四、源碼分析
1、_grid.scss:格子系統(tǒng)生成的主類,引用了mixins/_grid.scss、mixins/_grid-framework.scss、variables.scss類中的變量及相關(guān)方法。
首先:定義兩個(gè)容器類
a) container:格子容器,根據(jù)不同設(shè)備定義不同的寬度,不會(huì)充滿全屏;
b) continaer-fluid:格子容器,在任何支持下都會(huì)充滿全屏
container和container-fluid都使用了make-container(mixins/_grid.scss),make-container只實(shí)現(xiàn)了居中、左右內(nèi)邊距、清除浮動(dòng)等控制;其中container根據(jù)不同設(shè)備定義了容器的寬度
然后:定義row(行):
調(diào)用了make-row(mixins/_grids.scss)實(shí)現(xiàn)清除浮動(dòng)、左右外邊距的定義,在4.0中,如果開啟了flex布局的支持,就設(shè)定容器的display為flex和flex-wrap為wrap,并去掉清浮動(dòng)。
再則:直接調(diào)用make-grid-columns(mixins/_grid-framework.scss)實(shí)現(xiàn)單元格的建立
a) make-grid-columns:單元格生成的入口方法,傳遞所能支持的格子總數(shù)、外邊距寬度、所支持的幾種尺寸
b) make-grid-columns引用了mixins/_grid.scss中的許多方法:
a) 用到了map的map-key函數(shù),用于遍歷一個(gè)map的key集合;
用到了@extend函數(shù),用于繼承,實(shí)現(xiàn)所有col左浮動(dòng),以及所有col都相對(duì)定位。
@for $i from 1 through $columns {
.col-#{$breakpoint}-#{$i} {
@extend %grid-column; //extend是繼承,將此合并為一個(gè)樣式集合
//.col-xs-1,col-xs-2{ positiona:relative; .... }
}
}
a) Make-col-span函數(shù),實(shí)現(xiàn)col寬度的計(jì)算
b) 調(diào)用mixins/_grid.scss中的make-col-modifier方法,實(shí)現(xiàn)push、pull、offset的樣式的生成:
i. Push:向右推幾個(gè)格子,用的是left
ii. Pull:向左推幾個(gè)格子,用的是right
iii. Offset:利用的是margin-left實(shí)現(xiàn),向右推向個(gè)百分比。
@mixin make-col-offset($size, $columns: $grid-columns) {
margin-left: percentage($size / $columns);
}
@mixin make-col-push($size, $columns: $grid-columns) {
left: if($size > 0, percentage($size / $columns), auto);
}
@mixin make-col-pull($size, $columns: $grid-columns) {
right: if($size > 0, percentage($size / $columns), auto);
}
@mixin make-col-modifier($type, $size, $columns) {
// Work around the lack of dynamic mixin @include support
@if $type == push {
@include make-col-push($size, $columns);
} @else if $type == pull {
@include make-col-pull($size, $columns);
} @else if $type == offset {
@include make-col-offset($size, $columns);
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家學(xué)習(xí)javascript程序設(shè)計(jì)有所幫助。