I want to use a canvas
element to cover the entire viewport of my browser window as a background. I use Javascript to set the width
and height
of the canvas
. A problem arises when the page scrolls and the address bar hides. The address bar scrolls away and leaves a gap on the page. There doesn't seem to be an event
for address bar movement, so I have no way to readjust the size of the canvas
when the address bar disappears. I can benefit from knowing three things:
Is there a way to detect address bar movement/hiding?
Is there a way to consistently get the inner dimensions of the actual viewport across all major browsers?
Is there a way to add an event listener to detect a change in the inner dimensions of the viewport?
EDIT: Here's some code:
// This is how I size my canvas;
function resize(event) {
context.canvas.height = document.documentElement.clientHeight;
context.canvas.width = document.documentElement.clientWidth;
}
window.addEventListener("resize", resize, false); // resize every time window changes
resize(); // Call function once to size the canvas
/* This is the CSS for my canvas */
canvas {
position:fixed;
top:0px;
left:0px;
margin:0;
padding:0;
z-index:-1; /* Forces canvas to background */
}
Edit 2:
I changed my resize function to:
context.canvas.height = window.innerHeight;
context.canvas.width = window.innerWidth;
The canvas does resize, which means that a resize event does fire when the address bar hides, but it does not fire until the address bar is completely hidden, because until it hides completely, no resizing takes place. This means a gap is still shown while scrolling, but is then filled once the address bar is hidden.