| 值 | 描述 |
|---|---|
| auto | 默認(rèn)值,跟 contain 表現(xiàn)一致。頁面內(nèi)容顯示在safe area內(nèi)。 viewprot-fit:auto 等同于 viewport-fit:contain |
| contain | 可視窗口完全包含網(wǎng)頁內(nèi)容(左圖)。頁面內(nèi)容顯示在 safe area 內(nèi)。 viewport-fit:contain |
| cover | 網(wǎng)頁內(nèi)容完全覆蓋可視窗口(右圖)。頁面內(nèi)容充滿屏幕。 viewport-fit:cover |
constant 函數(shù)
iOS11 新增特性,Webkit 的一個 CSS 函數(shù),用于設(shè)定安全區(qū)域與邊界的距離,有四個預(yù)定義的變量(單位是px):
safe-area-inset-left safe-area-inset-right safe-area-inset-top safe-area-inset-bottom
注意:網(wǎng)頁默認(rèn)不添加擴(kuò)展的表現(xiàn)是 viewport-fit=contain,需要適配 iPhoneX 必須設(shè)置viewport-fit=cover,不然 constant 函數(shù)是不起作用的,這是適配的必要條件。
注意:網(wǎng)頁默認(rèn)不添加擴(kuò)展的表現(xiàn)是 viewport-fit=contain ,需要適配 iPhone 必須設(shè)置 viewport-fit=cover ,這是適配的關(guān)鍵步驟。
適配例子
第一步:設(shè)置網(wǎng)頁在可視窗口的布局方式
<meta name='viewport' content="width=device-width, viewport-fit=cover" />
第二步:頁面主體內(nèi)容限定在安全區(qū)域內(nèi)
body {
/* 適配齊劉海*/
padding-top: constant(safe-area-inset-top);
/* 適配底部黑條*/
padding-bottom: constant(safe-area-inset-bottom);
}
第三步:fixed 元素的適配
bottom 不是0的情況
/* bottom 不是0的情況 */
.fixed {
bottom: calc(50px(原來的bottom值) + constant(safe-area-inset-bottom));
}
吸底的情況(bottom=0)
/* 方法1 :設(shè)置內(nèi)邊距 擴(kuò)展高度*/
/* 這個方案需要吸底條必須是有背景色的,因?yàn)閿U(kuò)展的部分背景是跟隨外容器的,否則出現(xiàn)鏤空情況。*/
.fix {
padding-bottom: constant(safe-area-inset-bottom);
}
/* 直接擴(kuò)展高度*/
.fix {
height: calc(60px(原來的高度值) + constant(safe-area-inset-bottom));
}
/* 方法2 :在元素下面用一個空div填充, 但是背景色要一致 */
.blank {
position: fixed;
bottom: 0;
height: 0;
width: 100%;
height: constant(safe-area-inset-bottom);
background-color: #fff;
}
/* 吸底元素樣式 */
.fix {
margin-bottom: constant(safe-area-inset-bottom);
}
最后: 使用@supports
因?yàn)橹挥杏旋R劉海和底部黑條的機(jī)型才需要適配樣式,可以用@support配合使用:
@supports (bottom: constant(safe-area-inset-bottom)) {
body {
padding-bottom: constant(safe-area-inset-bottom);
}
}
完整檢測代碼
@supports隔離兼容模式
因?yàn)橹挥杏旋R劉海和底部黑條的機(jī)型才需要適配樣式,可以用@support配合使用:
@mixin iphonex-css {
padding-top: constant(safe-area-inset-top); //為導(dǎo)航欄+狀態(tài)欄的高度 88px
padding-top: env(safe-area-inset-top); //為導(dǎo)航欄+狀態(tài)欄的高度 88px
padding-left: constant(safe-area-inset-left); //如果未豎屏?xí)r為0
padding-left: env(safe-area-inset-left); //如果未豎屏?xí)r為0
padding-right: constant(safe-area-inset-right); //如果未豎屏?xí)r為0
padding-right: env(safe-area-inset-right); //如果未豎屏?xí)r為0
padding-bottom: constant(safe-area-inset-bottom); //為底下圓弧的高度 34px
padding-bottom: env(safe-area-inset-bottom); //為底下圓弧的高度 34px
}
@mixin iphonex-support {
@supports (bottom: constant(safe-area-inset-top)) or (bottom: env(safe-area-inset-top)) {
body.iphonex {
@include iphonex-css;
}
}
}
@media 媒體查詢
@mixin iphonex-css {
padding-top: constant(safe-area-inset-top); //為導(dǎo)航欄+狀態(tài)欄的高度 88px
padding-top: env(safe-area-inset-top); //為導(dǎo)航欄+狀態(tài)欄的高度 88px
padding-left: constant(safe-area-inset-left); //如果未豎屏?xí)r為0
padding-left: env(safe-area-inset-left); //如果未豎屏?xí)r為0
padding-right: constant(safe-area-inset-right); //如果未豎屏?xí)r為0
padding-right: env(safe-area-inset-right); //如果未豎屏?xí)r為0
padding-bottom: constant(safe-area-inset-bottom); //為底下圓弧的高度 34px
padding-bottom: env(safe-area-inset-bottom); //為底下圓弧的高度 34px
}
/* iphonex 適配 */
@mixin iphonex-media {
@media only screen and (device-width: 375px) and (device-height: 812px) and (-webkit-device-pixel-ratio: 3) {
body.iphonex {
@include iphonex-css;
}
}
}
補(bǔ)充
注意項(xiàng)
env 和 constant 只有在 viewport-fit=cover 時候才能生效, 上面使用的safari 的控制臺可以檢測模擬器中網(wǎng)頁開啟web inspector.
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
標(biāo)簽:金昌 赤峰 綏化 阿壩 盤錦 聊城 萍鄉(xiāng) 中山
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Html5 頁面適配iPhoneX(就是那么簡單)》,本文關(guān)鍵詞 Html5,頁面,適配,iPhoneX,就是,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。