mirror of
https://github.com/SECTL/SecScore.git
synced 2026-07-19 21:14:21 +08:00
修复iOS首帧视口高度异常导致白屏
This commit is contained in:
+24
-4
@@ -80,16 +80,36 @@ patchConsole()
|
|||||||
const syncAppViewportHeight = () => {
|
const syncAppViewportHeight = () => {
|
||||||
const rootStyle = document.documentElement.style
|
const rootStyle = document.documentElement.style
|
||||||
|
|
||||||
|
const readViewportHeight = () => {
|
||||||
|
const innerHeight = Number.isFinite(window.innerHeight) ? Math.round(window.innerHeight) : 0
|
||||||
|
const visualViewport = window.visualViewport
|
||||||
|
const visualHeight =
|
||||||
|
visualViewport && Number.isFinite(visualViewport.height) ? Math.round(visualViewport.height) : 0
|
||||||
|
|
||||||
|
return Math.max(innerHeight, visualHeight)
|
||||||
|
}
|
||||||
|
|
||||||
const applyHeight = () => {
|
const applyHeight = () => {
|
||||||
const viewportHeight = window.visualViewport?.height ?? window.innerHeight
|
try {
|
||||||
rootStyle.setProperty("--ss-app-height", `${Math.round(viewportHeight)}px`)
|
const viewportHeight = readViewportHeight()
|
||||||
|
// 避免 iOS 首帧偶发 0 高度把根节点压扁导致白屏
|
||||||
|
if (viewportHeight < 320) return
|
||||||
|
rootStyle.setProperty("--ss-app-height", `${viewportHeight}px`)
|
||||||
|
} catch {
|
||||||
|
void 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
applyHeight()
|
applyHeight()
|
||||||
|
window.setTimeout(applyHeight, 50)
|
||||||
|
window.setTimeout(applyHeight, 300)
|
||||||
window.addEventListener("resize", applyHeight)
|
window.addEventListener("resize", applyHeight)
|
||||||
window.addEventListener("orientationchange", applyHeight)
|
window.addEventListener("orientationchange", applyHeight)
|
||||||
window.visualViewport?.addEventListener("resize", applyHeight)
|
const visualViewport = window.visualViewport
|
||||||
window.visualViewport?.addEventListener("scroll", applyHeight)
|
if (visualViewport && typeof visualViewport.addEventListener === "function") {
|
||||||
|
visualViewport.addEventListener("resize", applyHeight)
|
||||||
|
visualViewport.addEventListener("scroll", applyHeight)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
syncAppViewportHeight()
|
syncAppViewportHeight()
|
||||||
|
|||||||
Reference in New Issue
Block a user