美文网首页
【H5】适配iphoneX底部横条高度

【H5】适配iphoneX底部横条高度

作者: mocoe | 来源:发表于2020-12-19 14:27 被阅读0次

问题

iphoneX系列手机底部有个横条,会挡住正常页面内容,开发时,需要对这类机型做兼容。

解决

思路

在底部添加一个div占位元素,判断当前为iphoneX机型时,设置占位元素高度,留出安全距离。

代码(react+scss)

组件IPhoneXBottomHeight

// IPhoneXBottomHeight/index.tsx
import React from 'react';
import useStyles from 'isomorphic-style-loader/useStyles';
import s from './index.iso.scss';
const IPhoneXBottomHeight = () => {
  return (
    <div className={s.iphonexBottomHeight} />
  );
};
export default IPhoneXBottomHeight;
// IPhoneXBottomHeight/index.iso.scss
@media only screen and (min-device-height: 812px) {
  @supports (-webkit-overflow-scrolling: touch) {
     .iphonexBottomHeight {
       height: constant(safe-area-inset-bottom);
       height: env(safe-area-inset-bottom);
     }
  }
}

组件使用:

import React from 'react';
import { observer } from 'mobx-react';
import IPhoneXBottomHeight from 'src/IPhoneXBottomHeight';
const A = () => {
  return (
    <MainContent />
    <IPhoneXBottomHeight />
  );
};
export default observer(A);

知识点

iphone各机型开发尺寸

手机型号 屏幕尺寸(英寸) ppi 开发尺寸 像素尺寸 倍图
4/4S 3.5 326 320*480 pt 640*960 px @2x
5/5S/5C 4 326 320*568 pt 640*1136 px @2x
6/6S/7/8 4.7 326 375*667 pt 750*1334 px @2x
6+/6S+/7+/8+ 5.5 401 414*736 pt 1242*2208 px @3x
X 5.8 458 375*812 pt 1125*2436 px @3x

相关文章

网友评论

      本文标题:【H5】适配iphoneX底部横条高度

      本文链接:https://www.haomeiwen.com/subject/xixziktx.html