一、media query方式
/*iPhone X 适配*/
@media only screen and (device-width: 375px) and (device-height: 812px) and (-webkit-device-pixel-ratio: 3) {
.fixed-bottom{
bottom: 37px;
}
}
/*iPhone XS max 适配*/
@media only screen and (device-width: 414px) and (device-height: 896px) and (-webkit-device-pixel-ratio:3) {
.fixed-bottom{
bottom: 37px;
}
}
/*iPhone XR max 适配*/
@media only screen and (device-width: 414px) and (device-height: 896px) and (-webkit-device-pixel-ratio:2) {
.fixed-bottom{
bottom: 37px;
}
}
二、CSS函数
meta标签加入viewport-fit=cover
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, viewport-fit=cover">
css写法,不支持env、constant的浏览器会忽略此样式
.fixed-bottom{
bottom: 0;
bottom: constant(safe-area-inset-bottom);
bottom: env(safe-area-inset-bottom);
}
三、JS判断
if($(window).width() === 375 && $(window).height() === 724 && window.devicePixelRatio === 3){
//
}
解决Iphonex 底部按钮fixed,bottom:0 底部留白问题因为本身页面内容的高度不够, 需通过代码撑开元素的高度.
- 配合viewport-fit=“cover”使用
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1,maximum-scale=1, user-scalable=0,viewport-fit=cover">
Demo
<div class="wrapper">
<div class="main-wrapper">我是中间区域</div>
<div class="btn-wrapper">我是fixed按钮</div>
</div>
// css 区域
.wrapper{
position: relative;
padding-bottom: 100px;
box-sizing: border-box;
}
.main-wrapper{
overflow: auto;
}
.btn-wrapper{
width: 100%;
height: 100px;
position: fixed;
left: 0;
bottom: 0;
}
// iphonex 系列 需增加样式
//iphoneX、iphoneXs
@media only screen and (device-width: 375px) and (device-height: 812px) and (-webkit-device-pixel-ratio: 3) {
.wrapper{
height: 100vh;
overflow: hidden;
}
.main-wrapper{
height: 100%;
}
}
//iphone Xs Max
@media only screen and (device-width: 414px) and (device-height: 896px) and (-webkit-device-pixel-ratio:3) {
.wrapper{
height: 100vh;
overflow: hidden;
}
.main-wrapper{
height: 100%;
}
}
//iphone XR
@media only screen and (device-width: 414px) and (device-height: 896px) and (-webkit-device-pixel-ratio:2) {
.wrapper{
height: 100vh;
overflow: hidden;
}
.main-wrapper{
height: 100%;
}
}