彈性佈局flex實現移動端“頭尾固定,中間區域滑動”效果

頭尾固定,中間區域可以滑動效果是移動端最常見的效果,以京東頁面爲例。以前開發時常用的方法是用固定佈局position:fixed;實現,但是該方法在ios上或者其他機型上會出現問題。現在,可以用flex方法快速實現該佈局。

在這裏插入圖片描述
html:

<body>
    <div class="flex-wrap">
        <div class="header">Header</div>
        <div class="cont">
            中間內容區域
        </div>
        <div class="footer">Footer</div>
    </div>
</body>

css:

html,
body {
    width: 100%;
    height: 100%;
}

.flex-wrap {
    display: flex;
    flex-direction: column;
    height: 100%;
}

.flex-wrap .header,
.flex-wrap .footer {
    height: 40px;
    background: lime;
}

.flex-wrap .cont {
    flex: 1;
    overflow: scroll;
    background: red;
}

說明: css樣式中,一定要設置html,body以及最外層包裹容器的高度爲100%,同時中間內容區域的樣式一定要添加flex:1;

flex是flex-grow、flex-shrink、flex-basis的縮寫;
關於彈性佈局的具體用法可以參考文章:

https://www.html.cn/archives/7236

http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html


如有更好方法,歡迎大家留言,文章中有不足之處,歡迎大家指出。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章