あなたが求めていることは、まったく難しいことではありません。必要なのは、1 つの優れた JavaScript 関数と、HTML コードへの簡単な小さな変更だけです。
<div>
まず、 HTML に簡単な変更を加えて、「コンテナー」に ID を与えます。
<div class="container" id="container">
I want this container to be the height of the users screen resolution.
</div>
次に、それを参照する JavaScript 変数を定義します。
var container = document.getElementById("container");
次に、JavaScript を使用して画面のサイズを取得するために常に使用しているこの便利な関数を使用します。
function resize() {
// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
if (typeof window.innerWidth != 'undefined') {
viewportwidth = window.innerWidth,
viewportheight = window.innerHeight
}
// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
viewportwidth = document.documentElement.clientWidth,
viewportheight = document.documentElement.clientHeight
}
// older versions of IE
else {
viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
viewportheight = document.getElementsByTagName('body')[0].clientHeight
}
container.style.height = viewportheight+"px";
}
関数を入れたことに注意してくださいcontainer.style.height = viewportheight+"px";
。これは、 が呼び出されるたびresize();
にブラウザの寸法を更新し、それらの寸法をコンテナに再適用することを意味し<div>
ます。
resize();
この HTML を使用して、ページのサイズが変更されるたびに、またページが最初に読み込まれるときに、ボディで関数を呼び出します。
<body onload="resize()" onresize="resize()">
この関数は、コンテナー<div>
をページ全体の高さにサイズ変更します。これに問題がある場合、または質問がある場合はお知らせください。