针对iPhone刘海屏的scss媒体查询封装

该博客介绍了如何针对iPhone的刘海屏设备,利用CSS媒体查询和JavaScript进行屏幕尺寸及像素比的判断,确保网页在不同型号的iPhone上正确显示。涉及的型号包括iPhone 12系列、iPhone XS、iPhone 11系列以及iPhone XR和iPhone 11 Pro Max。同时,提到了在某些模拟器和开发者工具中可能出现的判定误差。
摘要由CSDN通过智能技术生成

最新 iPhone 刘海屏设备信息整理 包含iPhone12 最新系列
在这里插入图片描述
使用css媒体查询

@mixin bangs {
	@media only screen and (device-width: 360px) and (device-height: 780px) and (-webkit-device-pixel-ratio: 3), //12 mini 
	only screen and (device-width: 375px) and (device-height: 812px) and (-webkit-device-pixel-ratio: 3), // X Xs 11pro
	only screen and (device-width: 390px) and (device-height: 844px) and (-webkit-device-pixel-ratio: 3), // 12 12pro
	only screen and (device-width: 414px) and (device-height: 896px) and (-webkit-device-pixel-ratio: 3), // Xsm 11promax 
	only screen and (device-width: 414px) and (device-height: 896px) and (-webkit-device-pixel-ratio: 2), //XR 11
	only screen and (device-width: 428px) and (device-height: 926px) and (-webkit-device-pixel-ratio: 3) //12promax
	{
		@content;
	}
}

使用js判断

function isBangs() {
	const Iphone = /iphone/gi.test(window.navigator.userAgent) // 是否iphone机型
	const Ratio2 = window.devicePixelRatio && window.devicePixelRatio === 2 // 像素比是否为2
	const Ratio3 = window.devicePixelRatio && window.devicePixelRatio === 3 // 像素比是否为3
	
	const Mini12 = window.screen.width === 360 && window.screen.height === 780   // 12mini
	const Pro11 = window.screen.width === 375 && window.screen.height === 812    // X Xs 11pro
	const Pro12 = window.screen.width === 390 && window.screen.height === 844    // 12 12pro
	const Promax11 = window.screen.width === 414 && window.screen.height === 896 // Xsm XR 11 11promax 
	const Promax12 = window.screen.width === 428 && window.screen.height === 926 // 12promax
	
	switch(true) {
		case Iphone && Ratio3 && Mini12:   // 12 mini 
		case Iphone && Ratio3 && Pro11:    // X Xs 11pro
		case Iphone && Ratio3 && Pro12:    // 12 12pro
		case Iphone && Ratio2 && Promax11: // XR 11
		case Iphone && Ratio3 && Promax11: // Xsm 11promax
		case Iphone && Ratio3 && Promax12: // 12promax
			return true;
		default:
			return false
	}
}

注意:

  1. 使用hubilderX内置的浏览器模拟器会判定false,因为它的devicePixelRatio恒为1。
  2. 使用微信开发者工具公众号开发工具也会判定false,因为它的window.screen.height会减去公众号默认的头部高度。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值