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

4 months ago
  1. export function getWindowScrollTop(win) {
  2. let scrollTop = 0
  3. // 获取父窗口滚动偏移
  4. if (win.document.documentElement && win.document.documentElement.scrollTop) {
  5. scrollTop = win.document.documentElement.scrollTop
  6. } else if (win.document.body) {
  7. scrollTop = win.document.body.scrollTop
  8. }
  9. // 如果嵌套在 iframe 中,计算 iframe 的位置
  10. if (win.frameElement) {
  11. const iframeRect = win.frameElement.getBoundingClientRect()
  12. scrollTop += iframeRect.top // 加上 iframe 在父窗口中的偏移
  13. }
  14. return scrollTop
  15. }