欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript圣杯布局與雙飛翼布局實現(xiàn)案例詳解

 更新時間:2022年08月04日 16:45:42   作者:飯啊飯°  
這篇文章主要介紹了JavaScript圣杯布局與雙飛翼布局實現(xiàn)案例,這是前端面試中需要掌握的知識點,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

一、圣杯布局和雙飛翼布局的功能介紹

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

二、圣杯布局

高度如何自適應(yīng)屏幕高度

  • 其實這個并不是圣杯布局中的要求,圣杯布局是可以指定高度的,但是可以作為一個思考;
  • 方法就是使用flex布局,將主軸設(shè)置為縱軸,再將outer的flex設(shè)為1,意思就是填充多余空白,即可達(dá)到自適應(yīng)屏幕高度的效果。
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的位置;
  • 三個盒子都設(shè)置float:left,這時left和right就會被擠到下一行;
  • left設(shè)置margin-left:-100%;相對定位+left:-left的寬度;right設(shè)置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>

三、雙飛翼布局

雙飛翼布局的思路

  • 給三個盒子都設(shè)置為左浮動;
  • center的寬度設(shè)為100%;
  • left設(shè)置margin-left:-100%;right設(shè)置margin-left=-right的寬度即可將兩個盒子歸位,但是將center的兩端擋住了;
  • 在center盒子中再寫一個content盒子,設(shè)置margin-left和margin-right為兩側(cè)的寬度,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;
  • 雙飛翼布局是先設(shè)置中間盒子的寬度為100%,再用margin移動左右盒子覆蓋了中間盒子的兩側(cè),再將outer中間加入一個盒子,留出兩側(cè)的margin值,達(dá)到三欄布局的效果。

四、圣杯布局和雙飛翼布局面試問題

回答:圣杯布局和雙飛翼布局都可以實現(xiàn)三欄布局,即兩側(cè)寬度固定,中間自適應(yīng)的效果。圣杯布局是先用padding將中間內(nèi)容留出,再定位左右盒子到相應(yīng)位置;而雙飛翼布局首先將中間盒子的寬度設(shè)為了100%,在定位左右盒子的時候會覆蓋中間盒子的兩端,這樣就需要在中間盒子中在定義一個盒子,并留出margin的兩側(cè)值。兩種布局都需要把center盒子寫在left和right前面,為了最先渲染。

到此這篇關(guān)于JavaScript圣杯布局與雙飛翼布局實現(xiàn)案例詳解的文章就介紹到這了,更多相關(guān)JS圣杯布局與雙飛翼布局內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論