iOS Telegram WebView 中 position: fixed 头部显示偏移
先给结论
问题一般不在 Next.js 的路由或 header 组件,而在 iOS Telegram 内嵌的 WKWebView:它的视口、安全区和滚动容器会让 position: fixed、position: sticky 的参照位置跟普通 Safari/Chrome 对不上。处理时先用 visualViewport.offsetTop 做动态补偿,再清掉祖先元素上的 transform、filter、perspective、overflow,安全区交给 env(safe-area-inset-top)。
为什么只在 Telegram iOS 出现
- Telegram iOS 用应用内 WebView 打开网页,顶部可能多出外壳、安全区或视口偏移。
fixed一般相对布局视口定位,而此时的视觉视口已经偏移了。 sticky依赖滚动容器。header 的祖先只要带overflow、transform、限高,或者被 Telegram 注入的容器包住,sticky 就会相对那个容器定位,而不是整个页面。- 这几个条件在 Android WebView 和普通浏览器上不一定成立,所以同一套 CSS 看起来像“只在这一个环境坏”。
排查步骤
- 用真机在 Telegram iOS 里打开页面,打印下面这些数值:
console.log({
innerHeight: window.innerHeight,
clientHeight: document.documentElement.clientHeight,
vvHeight: window.visualViewport?.height,
vvOffsetTop: window.visualViewport?.offsetTop,
vvPageTop: window.visualViewport?.pageTop,
headerTop: document.querySelector('.headerMobile')?.getBoundingClientRect().top,
});
如果 vvOffsetTop 或 headerTop 跟预期差一个固定值,就按这个差值补偿。
-
顺着
.headerMobile的祖先链往上查,确认没有transform、filter、perspective、contain、overflow: hidden和固定高度。fixed一旦遇到transform祖先,包含块就变了。 -
看
html/body有没有被设成overflow: hidden或者限高。要让sticky生效,滚动必须发生在页面根滚动容器上。
修复方案
方案 A:fixed + visualViewport 动态补偿
header 的 top 用 CSS 变量控制,在 Next.js 客户端读取 visualViewport 并把值写进变量。下面的代码只在检测到视觉视口确实有偏移时才补偿,普通浏览器不会被动到:
:root {
--app-header-top: 0px;
}
html,
body {
margin: 0;
min-height: 100%;
}
.headerMobile {
position: fixed;
top: var(--app-header-top, 0px);
left: 0;
right: 0;
z-index: 100;
width: 100%;
display: none;
background-color: #fff;
box-sizing: border-box;
padding-top: env(safe-area-inset-top, 0px);
}
main {
padding-top: calc(64px + env(safe-area-inset-top, 0px));
}
Next.js 客户端组件中:
'use client';
import { useEffect } from 'react';
export default function ViewportTopFix() {
useEffect(() => {
const vv = window.visualViewport;
if (!vv) return;
let raf = 0;
const update = () => {
cancelAnimationFrame(raf);
raf = requestAnimationFrame(() => {
const offsetTop = Math.max(0, vv.offsetTop || 0);
document.documentElement.style.setProperty(
'--app-header-top',
`${offsetTop}px`
);
});
};
update();
vv.addEventListener('resize', update);
vv.addEventListener('scroll', update);
window.addEventListener('orientationchange', update);
return () => {
cancelAnimationFrame(raf);
vv.removeEventListener('resize', update);
vv.removeEventListener('scroll', update);
window.removeEventListener('orientationchange', update);
};
}, []);
return null;
}
在布局里挂一次 <ViewportTopFix /> 就行。只想在 Telegram iOS 启用,可以在 update 里加 UA 判断,但 UA 字符串会变,更稳的做法是看 vv.offsetTop 是否稳定非零,或者把启用范围收窄到真机验证过的版本。
方案 B:sticky 作为固定头部
header 不该跟着页面滚出视口的话,优先用 sticky,前提是滚动容器就是页面本身:
html,
body {
margin: 0;
overflow-x: hidden;
overflow-y: auto;
}
.headerMobile {
position: sticky;
top: 0;
z-index: 100;
width: 100%;
background-color: #fff;
padding-top: env(safe-area-inset-top, 0px);
box-sizing: border-box;
}
同时给 main 留出高度,别让内容被盖住。body 或中间层只要有 overflow: hidden,sticky 就会失效,或者相对错误的容器定位。
注意事项
- 只有加了
viewport-fit=cover,安全区才需要env(safe-area-inset-top);不加的时候这个值一般是 0。 width: 100lvw在部分 WebView 里的兼容性不如width: 100%或100dvw,可以先换掉再测。原代码用的是100lvw,如果本意是占满宽度,100%更稳。- 别用负
margin-top硬把 header 拉上去,换个 Telegram 版本或者弹出键盘就容易再次错位。 - Telegram iOS 的 WebView 行为跟版本、系统、是否全屏、键盘是否弹出都有关系,上线前在目标版本的真机上回归一遍。
备注:内容仅供参考。
