App下載

使用div固定到底部實(shí)現(xiàn)導(dǎo)航條有幾種方式?方法總結(jié)!

猿友 2021-08-04 11:25:53 瀏覽數(shù) (4567)
反饋

這是我們平常比較常見到的內(nèi)容,那么今天小編就把它拿出和大家講講有關(guān)于:“使用div固定到底部實(shí)現(xiàn)導(dǎo)航條有幾種方式?”這個(gè)問題的相關(guān)內(nèi)容分享!

需求:

需要把導(dǎo)航固定在底部?只能滑動(dòng)內(nèi)容,導(dǎo)航菜單固定不動(dòng)的。效果如下:

這篇文章主要講解三種實(shí)現(xiàn)方案,包括:fixed,absolute,以及css3的flex布局。

html結(jié)構(gòu)如下:

<div class="box">
    <div class="roll">滾動(dòng)區(qū)域</div>
    <footer>底部固定菜單</footer>
</div>
<!---公用樣式--->
<style>
html,body{
    margin:0;padding:0;height:100%;width:100%;
}
footer{
    background:#F2F3F6;max-width: 750px;width: 100%;height: 1rem;
}
</style>

方法一:使用fixed

.box{
        .roll{
            padding-bottom:1rem;
         }
    footer{
        position:fixed;bottom:0;z-index:999;
    }
}

方法二:使用absolute  

.box{
    position: relative;height: 100%;
    .roll{
        position: absolute;bottom:1rem;top: 0;overflow-y: scroll;-webkit-overflow-scrolling: touch;height: auto;
    }
    footer{
        position: absolute;bottom:0;
    }
}

方法三:使用flex 

.box{
    display:flex;display: -webkit-flex;height:100%;flex-direction:column;
    .roll{
        flex: 1; width: 100%;overflow-y: scroll;-webkit-overflow-scrolling: touch;height: auto;
    }
}

總結(jié)

1、底部定位為fixed或absolute的時(shí)候,出現(xiàn)優(yōu)先級(jí)別較低,導(dǎo)致被其他div覆蓋的情況,那么這里就需要用到z-index,來讓他成為最高級(jí)別,不至于被覆蓋。

2、底部定位為fixed或absolute,存在輸入框的時(shí)候,會(huì)出現(xiàn)如下情況:

ios:激活輸入框時(shí),底部不會(huì)彈出來(合理)。
Android:激活輸入框時(shí),底部會(huì)跟著輸入框彈出來(不合理)  

傳統(tǒng)解決辦法:通常將底部設(shè)置為fixed,當(dāng)激活輸入框的時(shí)候,將底部定位改為relative,即可兼容ios和Android。

3、使用方法二或者方法三,需要設(shè)置-webkit-overflow-scrolling 屬性。這樣才能保證滾動(dòng)區(qū)域的流暢性,-webkit-overflow-scrolling控制元素在移動(dòng)設(shè)備上是否使用滾動(dòng)回彈效果。

4、在部分瀏覽器中設(shè)置overflow-y: scroll;會(huì)出現(xiàn)滾動(dòng)條,這時(shí)候我們需要全局定義如下樣式:

::-webkit-scrollbar{//scroll滾動(dòng)條設(shè)置
        width: 0px; height: 0px; color: rgb(136, 0, 0);">#fff; 
}

5、移動(dòng)端推薦使用方法三的布局形式。

那么以上就有有關(guān)于“使用div固定到底部實(shí)現(xiàn)導(dǎo)航條有幾種方式?”這方面的相關(guān)內(nèi)容與分享,更多相關(guān)Html5的相關(guān)知識(shí)內(nèi)容大家可以在W3Cschool中進(jìn)行學(xué)習(xí)和了解!

0 人點(diǎn)贊