ドキュメントの高さと幅を純粋に取得する方法 JavaScript つまり、使用せずに jquery。
私が知っている$(document).height()と$(document).width()、私はこれでやってみたいですJavaScript。
ページの高さと幅を意味しました。
ドキュメントの高さと幅を純粋に取得する方法 JavaScript つまり、使用せずに jquery。
私が知っている$(document).height()と$(document).width()、私はこれでやってみたいですJavaScript。
ページの高さと幅を意味しました。
回答:
var height = document.body.clientHeight;
var width = document.body.clientWidth;
window.innerWidthこれを解決します
http://www.howtocreate.co.uk/tutorials/javascript/browserwindowに示されている最後の例でさえ、Quirksモードでは機能しません。思ったより簡単に見つけることができます。これが解決策のようです(最新のjqueryコードから抽出)。
Math.max(
document.documentElement["clientWidth"],
document.body["scrollWidth"],
document.documentElement["scrollWidth"],
document.body["offsetWidth"],
document.documentElement["offsetWidth"]
);
「高さ」の幅を置き換えるだけで高さを取得できます。
$(document).width()ます。たぶん、あなたは$(window).width()純粋なJSがdocument.documentElement["clientWidth"]現代のブラウザでちょうどそうであるようなものを求めています。
getBoundingClientRect通常はクロスブラウザーで機能し、境界の四角形でサブピクセルの精度が得られるため、使用する必要があります。
elem.getBoundingClientRect()
これはすべてのブラウザ/デバイスで機能するはずです。
function getActualWidth()
{
var actualWidth = window.innerWidth ||
document.documentElement.clientWidth ||
document.body.clientWidth ||
document.body.offsetWidth;
return actualWidth;
}
ドキュメントの幅と高さを簡単に見つける方法は?
HTMLで
<span id="hidden_placer" style="position:absolute;right:0;bottom:0;visibility:hidden;"></span>
JavaScriptで
var c=document.querySelector('#hidden_placer');
var r=c.getBoundingClientRect();
r.right=document width
r.bottom=document height`
必要に応じて、すべてのウィンドウサイズ変更イベントでこれを更新できます。
windowブラウザのアプリケーションウィンドウ全体です。document実際に読み込まれる、表示されているWebページです。
window.innerWidthそして、window.innerHeightスクロールバーを考慮に入れますが、それはあなたが望むものではないかもしれません。
document.documentElement上部スクロールバーのない完全なWebページです。document.documentElement.clientWidthyスクロールバーなしでドキュメントの幅サイズを返します。
document.documentElement.clientHeightxスクロールバーなしでドキュメントの高さサイズを返します。