濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > Html5 頁(yè)面適配iPhoneX(就是那么簡(jiǎn)單)

Html5 頁(yè)面適配iPhoneX(就是那么簡(jiǎn)單)

熱門(mén)標(biāo)簽:欣鼎電銷(xiāo)機(jī)器人 效果 電話機(jī)器人技術(shù) 智能電銷(xiāo)機(jī)器人被禁用了么 高德地圖標(biāo)注商戶怎么標(biāo) 黃石ai電銷(xiāo)機(jī)器人呼叫中心 如何查看地圖標(biāo)注 惡搞電話機(jī)器人 ok電銷(xiāo)機(jī)器人 地圖標(biāo)注軟件打印出來(lái)

前言

iPhoneX 取消了物理按鍵,改成底部小黑條,這一改動(dòng)導(dǎo)致網(wǎng)頁(yè)出現(xiàn)了比較尷尬的屏幕適配問(wèn)題。對(duì)于網(wǎng)頁(yè)而言,頂部(劉海部位)的適配問(wèn)題瀏覽器已經(jīng)做了處理,所以我們只需要關(guān)注底部與小黑條的適配問(wèn)題即可(即常見(jiàn)的吸底導(dǎo)航、返回頂部等各種相對(duì)底部 fixed 定位的元素)。

筆者通過(guò)查閱了一些官方文檔,以及結(jié)合實(shí)際項(xiàng)目中的一些處理經(jīng)驗(yàn),整理了一套簡(jiǎn)單的適配方案分享給大家,希望對(duì)大家有所幫助,以下是處理前后效果圖:

大家都知道,iphoneX,屏幕頂部都有一個(gè)齊劉海,iPhoneX 取消了物理按鍵,改成底部小黑條,如果不做適配,這些地方就會(huì)被遮擋,因此本文講述下齊劉海與底部小黑條的適配方法。

幾個(gè)新概念

安全區(qū)域

安全區(qū)域指的是一個(gè)可視窗口范圍,處于安全區(qū)域的內(nèi)容不受圓角(corners)、齊劉海(sensor housing)、小黑條(Home Indicator)影響,如下圖所示:

 

viewport-fit

iOS11 新增特性,蘋(píng)果公司為了適配 iPhoneX 對(duì)現(xiàn)有 viewport meta 標(biāo)簽的一個(gè)擴(kuò)展,用于設(shè)置網(wǎng)頁(yè)在可視窗口的布局方式,可設(shè)置 三個(gè)值

描述
auto 默認(rèn)值,跟 contain 表現(xiàn)一致。頁(yè)面內(nèi)容顯示在safe area內(nèi)。 viewprot-fit:auto 等同于 viewport-fit:contain
contain 可視窗口完全包含網(wǎng)頁(yè)內(nèi)容(左圖)。頁(yè)面內(nèi)容顯示在 safe area 內(nèi)。 viewport-fit:contain
cover 網(wǎng)頁(yè)內(nèi)容完全覆蓋可視窗口(右圖)。頁(yè)面內(nèi)容充滿屏幕。 viewport-fit:cover

constant 函數(shù)

iOS11 新增特性,Webkit 的一個(gè) CSS 函數(shù),用于設(shè)定安全區(qū)域與邊界的距離,有四個(gè)預(yù)定義的變量(單位是px):

safe-area-inset-left
safe-area-inset-right
safe-area-inset-top
safe-area-inset-bottom

注意:網(wǎng)頁(yè)默認(rèn)不添加擴(kuò)展的表現(xiàn)是 viewport-fit=contain,需要適配 iPhoneX 必須設(shè)置viewport-fit=cover,不然 constant 函數(shù)是不起作用的,這是適配的必要條件。

  • 官方文檔中提到將來(lái) env() 要替換 constant () ,目前還不可用
  • 這兩個(gè)函數(shù)都是 webkit 中 css 函數(shù),可以直接使用變量函數(shù),只有在 webkit 內(nèi)核下才支持
  • constant :針對(duì)iOS < 11.2以下系統(tǒng)
  • env :針對(duì)于iOS >= 11.2的系統(tǒng)

注意:網(wǎng)頁(yè)默認(rèn)不添加擴(kuò)展的表現(xiàn)是 viewport-fit=contain ,需要適配 iPhone 必須設(shè)置 viewport-fit=cover ,這是適配的關(guān)鍵步驟。

適配例子

第一步:設(shè)置網(wǎng)頁(yè)在可視窗口的布局方式

<meta name='viewport'  content="width=device-width, viewport-fit=cover"  />

第二步:頁(yè)面主體內(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(原來(lái)的bottom值) + constant(safe-area-inset-bottom));
}

吸底的情況(bottom=0)

/* 方法1 :設(shè)置內(nèi)邊距 擴(kuò)展高度*/
/* 這個(gè)方案需要吸底條必須是有背景色的,因?yàn)閿U(kuò)展的部分背景是跟隨外容器的,否則出現(xiàn)鏤空情況。*/
.fix {
  padding-bottom: constant(safe-area-inset-bottom);
}
/* 直接擴(kuò)展高度*/
.fix {
  height: calc(60px(原來(lái)的高度值) + constant(safe-area-inset-bottom));
}

/* 方法2 :在元素下面用一個(gè)空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);
  }
}

完整檢測(cè)代碼

@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 時(shí)候才能生效, 上面使用的safari 的控制臺(tái)可以檢測(cè)模擬器中網(wǎng)頁(yè)開(kāi)啟web inspector.

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

標(biāo)簽:金昌 赤峰 綏化 阿壩 盤(pán)錦 聊城 萍鄉(xiāng) 中山

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Html5 頁(yè)面適配iPhoneX(就是那么簡(jiǎn)單)》,本文關(guān)鍵詞  Html5,頁(yè)面,適配,iPhoneX,就是,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Html5 頁(yè)面適配iPhoneX(就是那么簡(jiǎn)單)》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于Html5 頁(yè)面適配iPhoneX(就是那么簡(jiǎn)單)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    苍梧县| 海城市| 洱源县| 吐鲁番市| 栾川县| 桂东县| 三明市| 吴江市| 吉隆县| 河南省| 呼玛县| 江北区| 庐江县| 施甸县| 平泉县| 德化县| 环江| 湖州市| 常山县| 临沭县| 太湖县| 绥滨县| 平山县| 吉木乃县| 鲁甸县| 团风县| 即墨市| 威海市| 辽中县| 内丘县| 三原县| 张家界市| 大冶市| 临澧县| 舒城县| 阿图什市| 西峡县| 镇原县| 新余市| 民县| 沾化县|