JavaScript圣杯布局與雙飛翼布局實現(xiàn)案例詳解
一、圣杯布局和雙飛翼布局的功能介紹
- 圣杯布局和雙飛翼布局是三欄布局中的兩種布局方式,他們實現(xiàn)的效果是相同的,區(qū)別就是實現(xiàn)方法。
- header和footer各自占領屏幕所有寬度,高度固定;
- 中間的outer是一個三欄布局;
- 三欄布局中l(wèi)eft和right不變,center填充其他地方;
- 中間部分的高度是三欄中最高的區(qū)域的高度。

二、圣杯布局
高度如何自適應屏幕高度
- 其實這個并不是圣杯布局中的要求,圣杯布局是可以指定高度的,但是可以作為一個思考;
- 方法就是使用flex布局,將主軸設置為縱軸,再將outer的flex設為1,意思就是填充多余空白,即可達到自適應屏幕高度的效果。
body{
display: flex;
width: 100%;
height: 100%;
flex-direction: column;
}
.header{
background-color:grey;
height: 50px;
}
.footer{
background-color:grey;
height: 50px;
}
.outer{
flex:1;
}圣杯布局思路
- 將center盒子寫在最前面,保證center盒子最先渲染;
- 給outer盒子指定padding-left 和 padding-right值,留出left和right的位置;
- 三個盒子都設置float:left,這時left和right就會被擠到下一行;
- left設置margin-left:-100%;相對定位+left:-left的寬度;right設置margin-left=-right的寬度;相對定位+right:-right的寬度即可將兩個盒子歸位。
圣杯布局代碼
<style>
* {
padding: 0;
margin: 0;
text-align: center;
}
html{
height: 100%;
}
body{
display: flex;
flex-direction: column;
height: 100%;;
}
.header{
width: 100%;
height: 50px;
background-color:dimgray;
}
.footer{
width: 100%;
height: 50px;
background-color:dimgray;
}
.outer{
flex:1;
padding-left: 100px;
padding-right: 200px;
}
.center{
float: left;
background-color: darkslateblue;
height: 100%;
width: 100%;
}
.left{
float: left;
width: 100px;
margin-left: -100%;
background-color: burlywood;
height: 100%;
position: relative;
left: -100px;
}
.right{
float: left;
width: 200px;
margin-left: -200px;
height: 100%;
position: relative;
right: -200px;
background-color: cyan;
}
</style>
<body>
<div class="header">header</div>
<div class="outer">
<div class="center">center</div>
<div class="left">left</div>
<div class="right">right</div>
</div>
<div class="footer">footer</div>
</body>三、雙飛翼布局
雙飛翼布局的思路
- 給三個盒子都設置為左浮動;
- center的寬度設為100%;
- left設置margin-left:-100%;right設置margin-left=-right的寬度即可將兩個盒子歸位,但是將center的兩端擋住了;
- 在center盒子中再寫一個content盒子,設置margin-left和margin-right為兩側的寬度,content盒子作為內(nèi)容。
雙飛翼布局的代碼
<style>
*{
padding: 0;
margin: 0;
}
html{
width: 100%;
height: 100%;
text-align: center;
}
body{
display: flex;
width: 100%;
height: 100%;
flex-direction: column;
}
.header{
background-color:grey;
height: 50px;
}
.footer{
background-color:grey;
height: 50px;
}
.outer{
flex:1;
}
.center{
float: left;
width: 100%;
background-color: darkslateblue;
height: 100%;
}
.left{
float: left;
margin-left: -100%;
width: 100px;
background-color: burlywood;
height: 100%;
}
.right{
float: left;
width: 200px;
background-color: cyan;
margin-left: -200px;
height: 100%;
}
.content{
margin-left: 100px;
margin-right: 200px;
height: 100%;
}
</style>
<body>
<div class="header">header</div>
<div class="outer">
<div class="center">
<div class="content">content</div>
</div>
<div class="left">left</div>
<div class="right">right</div>
</div>
<div class="footer">footer</div>
</body>圣杯布局和雙飛翼布局的區(qū)別
- 圣杯布局是利用padding將中間部分留出,再利用定位、margin的方式將左右盒子歸位,因此不需要外層div;
- 雙飛翼布局是先設置中間盒子的寬度為100%,再用margin移動左右盒子覆蓋了中間盒子的兩側,再將outer中間加入一個盒子,留出兩側的margin值,達到三欄布局的效果。
四、圣杯布局和雙飛翼布局面試問題
回答:圣杯布局和雙飛翼布局都可以實現(xiàn)三欄布局,即兩側寬度固定,中間自適應的效果。圣杯布局是先用padding將中間內(nèi)容留出,再定位左右盒子到相應位置;而雙飛翼布局首先將中間盒子的寬度設為了100%,在定位左右盒子的時候會覆蓋中間盒子的兩端,這樣就需要在中間盒子中在定義一個盒子,并留出margin的兩側值。兩種布局都需要把center盒子寫在left和right前面,為了最先渲染。
到此這篇關于JavaScript圣杯布局與雙飛翼布局實現(xiàn)案例詳解的文章就介紹到這了,更多相關JS圣杯布局與雙飛翼布局內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
JS 退出系統(tǒng)并跳轉到登錄界面的實現(xiàn)代碼
這篇文章介紹了退出系統(tǒng)后跳轉到登陸頁面的簡單JS代碼,有需要的朋友可以參考一下2013-06-06
bootstrap——bootstrapTable實現(xiàn)隱藏列的示例
本篇文章主要介紹了bootstrapTable實現(xiàn)隱藏列的示例,具有一定的參考價值,有興趣的可以了解一下。2017-01-01
JavaScript用JQuery呼叫Server端方法示例代碼
這篇文章主要介紹了JavaScript用JQuery呼叫Server端方法,需要的朋友可以參考下2014-09-09

