一采缚、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函數(shù)
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 底部留白問題因為本身頁面內(nèi)容的高度不夠, 需通過代碼撐開元素的高度.
- 配合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">我是中間區(qū)域</div>
<div class="btn-wrapper">我是fixed按鈕</div>
</div>
// css 區(qū)域
.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%;
}
}