简体   繁体   中英

Get width and height of viewport excluding scroll bars

Okay, I've read about 1000 different ideas here but no answer seems to be complete.

I have a page that scrolls both horizontally and vertically, therefore, I have both horizontal and vertical scrollbars. I want the width of the viewport EXCLUDING the scrollbar and the height of the viewport EXCLUDING the scrollbar. Please also consider padding, and margins and borders etc.

For width, I originally had window.innerWidth but that included the scroll bar so I came to this solution that also accounts for body margin, padding and border, document.body.offsetWidth + parseInt(window.getComputedStyle(document.body).getPropertyValue('margin-left')) + parseInt(window.getComputedStyle(document.body).getPropertyValue('margin-right'))

Most other solutions for height either recommend window.innerHeight (but this includes the horizontal scrollbar which is not good) or document.body.clientHeight (which returns the whole length of the page, not just the viewport.)

So, does anyone have a solution for something so seemingly trivial?

document.body.clientWidth 和 .clientHeight 可能是您正在寻找的。

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