Window sizes and scrolling
How do we find the width and height of the browser window? How do we get the full width and height of the
document, including the scrolled out part? How do we scroll the page using JavaScript?
For this type of information, we can use the root document element document .documentElement , that
corresponds to the tag. But there are additional methods and peculiarities to consider.
Width/height of the window
To get window width and height, we can use the clientWidth/clientHeight of
document .document Element
documentELement .clientHeight
documentELement .clientWidth
For instance, this button shows the height of your window:
‘alert(document.documentElemer
ientHeight)
A Not window. innerWidth/innerHeight
Browsers also support properties like window. innerWidth/innerHeight . They look like what we want, so
why not to use them instead?
I there exists a scrollbar, and it occupies some space, clientwidth/clientHeight provide the
width/height without it (subtract it) In other words, they return the width/height of the visible part of the
document, available for the content.
window. innerWidth/innerHeight includes the scrollbar.
If there's a scrollbar, and it occupies some space, then these two lines show different values
alert( window. innerwidth ); // full window width
alert( document.documentElement.clientWidth ); // window width minus the scroilbeIn most cases, we need the available window width in order to draw or position something within scrollbars
(if there are any), so we should use documentElement. clientHeight/clientwidth
A ocr e isi
Please note: top-level geometry properties may work a little bit differently when there's no in HTML. Odd things are possible.
important
In modern HTML we should always write DOCTYPE
Width/height of the document
Theoretically, as the root document element is document .documentElement , and it encloses all the content, we
could measure the document's full size as. document . document Element. scrol1Width/scrol Height
But on that element, for the whole page, these properties do not work as intended. In Chrome/Safari/Opera, if
there’s no scroll, then documentElement.scrollHeight may be even less than
documentElement.clientHeight ! Weird, right?
To reliably obtain the full document height, we should take the maximum of these properties:
let scrollHeight = Math.max(
document.body.scrollHeight, document.documentélement.scrollHeight,
document.body.offsetHeight, document.documentElement .offsetHeight,
document .body.clientHeight, document .documentElenent .clientHeight
5
alert(‘Full document height, with scrolled out part: * + scrollHeight);
‘Why so? Better don't ask. These inconsistencies come from ancient times, not a “smart” logic.
Get the current scroll
DOM elements have their current scroll state in their scrollLeft/scrollTop properties.
For document scroll, document .documentElenent.scrollLeft/scrollTop works in most browsers, except
older Webkit-based ones, like Safari (bug 5997), where we should use document. body instead of
document .documentélement
Luckily, we don’t have to remember these peculiarities at all, because the scroll is available in the special
properties, window. pageXoffset/pagevorfset
alert(‘Current scroll from the top: ' + window. pageYOffset) ;
alert(‘Current scroll from the left: ' + window. pagexoffset);
These properties are read-only.@ Also available as window properties scrol1xX and scrollY
For historical reasons, both properties exist, but they are the same:
+ window. pageXOFfset is an alias of window.scrol1X
+ window. pagevoffset is an alias of window.scrollY
Scrolling: scrollTo, scrollBy, scrollIntoView
A Important:
To scroll the page with JavaScript its DOM must be fully built,
For instance, if we try to scroll the page with a script in , it won't work
Regular elements can be scrolled by changing scrol1Top/scroliLeft
We can do the same for the page using document.documentElement.scrollTop/scrollLeft (except Safari,
where document. body. scrollTop/Left should be used instead).
Alternatively, there's a simpler, universal solution: special methods window,scrollBy(xy) and
window scrollTo(pageX,pagey).
+ The method scro11By(x,y) scrolls the page relative to its current position. For instance, scro11By(@,10)
scrolls the page 1@px down.
The button below demonstrates this
window. scrollBy(0,10)
+ The method scrol1To(pageX, pageY) scrolls the page to absolute coordinates, so that the top-left comer of,
the visible part has coordinates (pageX, pageY) relative to the document's top-left corner. It's like setting
scrollLeft/scrollTop
To scroll to the very beginning, we can use scro11To(@,0)
window. scrolITo(0,0)
These methods work for all browsers the same way.
scrollintoView
For completeness, let's cover one more method: elem.scrollintoView(top).
The call to elem.scrollIntoView(top) scrolls the page to make elem visible. It has one argument:
+ If top=true (that’s the default), then the page will be scrolled to make elem appear on the top of the
window. The upper edge of the element will be aligned with the window top.
+ If top=false, then the page scrolls to make elem appear at the bottom. The bottom edge of the element
will be aligned with the window bottom.
The button below scrolls the page to position itself at the window top:this. scroliintoView()
And this button scrolls the page to position itself at the bottom:
this.scroliintoView(false)
Forbid the scrolling
Sometimes we need to make the document “unscrollable". For instance, when we need to cover the page with a
large message requiring immediate attention, and we want the visitor to interact with that message, not with the
document.
‘To make the document unscrollable, it's enough to set document .body. style. overflow = "hidden". The
page will “freeze” at its current scroll position.
Try it
document.body.style.overflow = ‘hidden’
‘document. bodystyle.overfiow =
The first button freezes the scroll, while the second one releases it
‘We can use the same technique to freeze the scroll for other elements, not just for document. body .
The drawback of the method is that the scrollbar disappears, If it occupied some space, then that space is now
free and the content “jumps" to fill.
That looks a bit odd, but can be worked around if we compare clientWidth before and after the freeze. IFit
increased (the scrollbar disappeared), then add padding to document.body in place of the scrollbar to keep
the content width the same.
Summary
Geometry:
+ Width/height of the visible part of the document (content area width/height
document .documentElenent .clientwidth/clientHeight
+ Width/height of the whole document, with the scrolled out part:
let scrollHeight = Math.max(
document. body.scrollHeight, document .documentElement.scrollHeight,
document.body.offsetHeight, document .documentElement.offsetHeight,
document. body.clientHeight, document .documentElement.clientHeight
5
Scrolling:
+ Read the current scroll: window. pageVOFFset/pagexOFfset
+ Change the current scroll:
+ window. scrol1To(pageX, pageY) ~ absolute coordinates,+ window. scrol1By(x,y) — scroll relative the current place,
+ elem.scrol1IntoView(top) ~ scroll to make elem visible (alian with the top/bottom of the window)
Music Theory - From Beginner To Expert - The Ultimate Step-By-Step Guide To Understanding and Learning Music Theory Effortlessly (Essential Learning Tools For Musicians Book 1)
Music Theory - From Beginner To Expert - The Ultimate Step-By-Step Guide To Understanding and Learning Music Theory Effortlessly (Essential Learning Tools For Musicians Book 1)