Skip to content

Instantly share code, notes, and snippets.

@imbyc
Created June 3, 2020 08:18
Show Gist options
  • Save imbyc/f5f603dd8480dd36ad5ae0f91324237d to your computer and use it in GitHub Desktop.
Save imbyc/f5f603dd8480dd36ad5ae0f91324237d to your computer and use it in GitHub Desktop.
[js 判断是否是刘海屏]
// 判断刘海屏
function hasNotch() {
var proceed = false
var div = document.createElement('div')
if (CSS.supports('padding-bottom: env(safe-area-inset-bottom)')) {
div.style.paddingBottom = 'env(safe-area-inset-bottom)'
proceed = true
} else if (CSS.supports('padding-bottom: constant(safe-area-inset-bottom)')) {
div.style.paddingBottom = 'constant(safe-area-inset-bottom)'
proceed = true
}
if (proceed) {
document.body.appendChild(div)
var calculatedPadding = parseInt(window.getComputedStyle(div).paddingBottom)
document.body.removeChild(div)
if (calculatedPadding > 0) {
return true
}
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment