導(dǎo)讀先來看下iPhone X機(jī)型的樣子上圖中,Iphonex機(jī)型在頭部和底部新增了這兩個(gè)區(qū)域,所以我們需要針對(duì)這類機(jī)型做些適配,方便我們的webapp的展示h5做成的移動(dòng)端頁(yè)面,常見布局為頭部+軀干+底... 先來看下iPhone X機(jī)型的樣子 上圖中,Iphonex機(jī)型在頭部和底部新增了這兩個(gè)區(qū)域,所以我們需要針對(duì)這類機(jī)型做些適配,方便我們的webapp的展示 <div class="page"> <header></header> <main></main> <footer></footer> </div> 但如果沒采用IphoneX機(jī)型的新的css屬性,而直接采用position: fixed;top:0等常規(guī)寫法,就會(huì)出現(xiàn)頭部的導(dǎo)航欄被手機(jī)自帶的狀態(tài)欄(顯示電量信號(hào)等等)遮擋的情況,底部的導(dǎo)航欄被IphoneX自帶的呼吸燈(圖中手機(jī)底部的白條)遮擋的情況,給用戶的操作和體驗(yàn)帶來困擾,目前針對(duì)這類問題,根據(jù)自己做過的項(xiàng)目,整理了一下幾種解決方案 我使用的是vue框架,在index.html頁(yè)面,我們需要添加 <meta name="viewport" content="width=device-width,viewport-fit=cover"> 然后,在公共的app.vue頁(yè)面,我們每個(gè)組件的展示,都是在這里被router-view替換,所以可以在這里處理一下公共的頭部頂欄,具體的布局如下: <template> <div id="app"> <div class="placeholder_top" :style="{position:fixpositiona?'absolute':'fixed'}"></div> <router-view class="routerview"></router-view> </div> </template> 上面的布局中,我們給class為placeholder_top的div寫下如下: .placeholder_top { position: fixed; top: 0; left: 0; width: 10rem; background-color: #303030; height: constant(safe-area-inset-top); height: env(safe-area-inset-top); z-index: 999; } 這樣的話,我們后續(xù),單獨(dú)的組件,就不用再處理這個(gè)頂部欄的問題,那下面,我們就可以處理下前面提到的頭部問題,一般頭部,我們大多都會(huì)封裝成公共組件,所以在這里,因?yàn)槭艿轿覀冊(cè)赼pp.vue頁(yè)面插入的那個(gè)元素的影響,我們的頭部的css寫法,也需要略微改動(dòng)下,頭部組件頁(yè)面布局如下: <template> <header> <div class="title" :style="{position:fixposition?'absolute':'fixed'}"> 導(dǎo)航內(nèi)容 </div> <div class="placeholder"></div> </header> </template> 頁(yè)面的css為: header{ background-color: #303030; .title{ position: fixed; top:0; top: constant(safe-area-inset-top); top: env(safe-area-inset-top); left: 0; height:88px; z-index: 999; } .placeholder{ height: 88px; width: 10rem; } } 這樣寫,這個(gè)頭部導(dǎo)航欄就會(huì)位居于手機(jī)狀態(tài)欄之下了,不會(huì)影響到視窗,并且能兼容安卓和ios機(jī)型(這類兼容問題,還涉及到ios的系統(tǒng)問題,不過本文暫未涉及) 下面再來看下main區(qū)域的處理,因?yàn)樯厦鎕eader組件已經(jīng)處理好了,所以main直接如下布局: main { padding-top: constant(safe-area-inset-top); padding-top: env(safe-area-inset-top); padding-bottom: calc(88px + constant(safe-area-inset-bottom)); padding-bottom: calc(88px + env(safe-area-inset-bottom)); ps:這里說明一下,下面的兩行,是用在當(dāng)前頁(yè)面沒有底部導(dǎo)航欄的情況 padding-bottom: constant(safe-area-inset-bottom); padding-bottom: env(safe-area-inset-bottom); }, main里面布局好了,直接寫內(nèi)容就可以了, 然后在看下底部的footer布局 <template> <footer> <div class="foot" :style="{position:fixposition?'absolute':'fixed'}"> 底部?jī)?nèi)容 </div> </footer> </template> 底部?jī)?nèi)容的css如下: footer{ position: fixed; bottom: 0; left: 0; width: 10rem; height: calc(88px + constant(safe-area-inset-bottom)); height: calc(88px + env(safe-area-inset-bottom)); background-color: #303030; .foot{ position: absolute; top:0; left: 0; width: 10rem; height: 88px; } } 這樣寫,底部導(dǎo)航foot里的內(nèi)容,就不會(huì)被手機(jī)自帶的呼吸燈所遮擋 所以可以總結(jié)一下,我們?cè)谶@種webapp適配中,可能需要采用的css寫法如下: position: fixed; top: constant(safe-area-inset-top); top: env(safe-area-inset-top); bottom: constant(safe-area-inset-bottom); bottom: env(safe-area-inset-bottom); top: calc(1rem + constant(safe-area-inset-top)); top: calc(1rem + env(safe-area-inset-top)); bottom: calc(1rem + constant(safe-area-inset-bottom)); bottom: calc(1rem + env(safe-area-inset-bottom)); ps:在上面的寫法中,有寫到:style="{position:fixposition?'absolute':'fixed'}",這個(gè)是為了解決用戶點(diǎn)擊輸入框,彈出軟鍵盤時(shí),這類固定元素的定位不準(zhǔn)的問題,感興趣的可以研究下,本文暫不討論 以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持本站。 |
溫馨提示:喜歡本站的話,請(qǐng)收藏一下本站!