简体   繁体   中英

Unexpected behavior of getBoundingClientRect()

codepen

I am trying to position footer div at the bottom of the page which is maximum value of the bottom coordinates of menu and content divs ( Math.max(menu, content) ).

But unfolding all three menus (making menu bigger than content) this value becomes smaller than actual menu bottom value (and bigger than content bottom value).

Thank you in advance!

getBoundingClientRect() gives results relative to the viewport , so it's affected by the current scroll position. Notice that if you unfold the menus without scrolling down (eg unfold menu 3 first) then it works correctly.

You can subtract window.scrollY from the bottom values to get the true position relative to the top of the document.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM