You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

18 lines
545 B

export function getWindowScrollTop(win) {
let scrollTop = 0
// 获取父窗口滚动偏移
if (win.document.documentElement && win.document.documentElement.scrollTop) {
scrollTop = win.document.documentElement.scrollTop
} else if (win.document.body) {
scrollTop = win.document.body.scrollTop
}
// 如果嵌套在 iframe 中,计算 iframe 的位置
if (win.frameElement) {
const iframeRect = win.frameElement.getBoundingClientRect()
scrollTop += iframeRect.top // 加上 iframe 在父窗口中的偏移
}
return scrollTop
}