ホームページ  >  記事  >  ウェブフロントエンド  >  JSは画面、ブラウザウィンドウのサイズ、Webページの高さと幅を取得します(実装コード)_JavaScriptスキル

JSは画面、ブラウザウィンドウのサイズ、Webページの高さと幅を取得します(実装コード)_JavaScriptスキル

WBOY
WBOYオリジナル
2016-05-16 17:08:43996ブラウズ

Web ページの表示領域の幅: document.body.clientWidth
Web ページの表示領域の高さ: document.body.clientHeight
Web ページの表示領域の幅Web ページ: document.body.offsetWidth (端の幅を含む)
Web ページの表示領域の高さ: document.body.offsetHeight (端の幅を含む)
の全テキスト幅Web ページ本文: document.body.scrollWidth
Web ページ本文の全文の高さ: document.body.scrollHeight
スクロールされる Web ページの高さ: document.body.scrollTop
スクロールされた Web ページの左側Web ページ: document.body.scrollLeft
Web ページの本体部分の上部: window.screenTop
Web ページの本体部分の左側: window.screenLeft
高い画面解像度:window.screen.height
画面解像度の幅: window.screen.width
画面で使用可能な作業領域の高さ: window.screen.availHeight
画面で使用可能な作業領域の幅: window.screen .availWidth


HTML の正確な位置決め:scrollLeft、scrollWidth、clientWidth、offsetWidth
scrollHeight: オブジェクトのスクロールの高さを取得します。
scrollLeft: オブジェクトの左端とウィンドウ内に現在表示されているコンテンツの左端との間の距離を設定または取得します。
scrollTop: オブジェクトの最上端と最上端との間の距離を設定または取得しますウィンドウに表示されているコンテンツの
scrollWidth: オブジェクトのスクロール幅を取得します。
offsetHeight: レイアウトまたは offsetParent プロパティで指定された親座標を基準としたオブジェクトの高さを取得します。
offsetLeft:レイアウトまたは offsetParent プロパティで指定された親座標を基準としたオブジェクトの高さ 左位置の計算
offsetTop: レイアウトまたは offsetTop 属性で指定された親座標を基準とした計算されたオブジェクトの上部位置を取得します
events.clientX ドキュメントを基準とした水平座標
event.clientY ドキュメントを基準とした垂直座標
event.offsetX はコンテナを基準とした水平座標です
event.offsetY はコンテナを基準とした垂直座標ですコンテナ
document.documentElement.scrollTop は垂直スクロールの値です
event.clientX document.documentElement.scrollTop はドキュメントの水平位置に対する相対値です 垂直方向の座標スクロール量

IE と FireFox の違いは次のとおりです。

IE6.0、FF1.06:
clientWidth = 幅のパディング
clientHeight = 高さのパディング
offsetWidth = 幅のパディング境界線
offsetHeight = 高さのパディング境界線

IE5.0/5.5:
clientWidth = 幅 - 境界線
clientHeight = 高さ - 境界線
offsetWidth = 幅
offsetHeight = 高さ

(言及する必要があります: CSS の margin 属性は clientWidth、offsetWidth、clientHeight、および offsetHeight とは何の関係もありません)
Web ページの表示領域の幅: document.body.clientWidth
Web ページの表示領域の高さ: document.body.clientHeight
Web ページの表示領域の幅: document.body.offsetWidth (端の幅を含む)
Web ページの表示領域の高さ: document.body.offsetHeight (端の高さを含む)
Web ページの全文の幅: document.body .scrollWidth
Web ページの全文の高さ: document.body.scrollHeight
スクロールされる Web ページの高さ: document.body.scrollTop
スクロールされる Web ページの左側: document.body.scrollLeft
Web ページのテキスト 上部の部分: window.screenTop
Web ページのテキストの左側の部分: window.screenLeft
画面解像度の高さ: window.screen.height
画面解像度の幅: window.screen.width
使用可能な画面の作業領域の高さ: window.screen.availHeight
使用可能な画面の作業領域の幅: window.screen.availWidth
-------------- ----

技術的なポイント

このセクションのコードでは、主にウィンドウに関する Document オブジェクトのいくつかのプロパティを使用します。これらのプロパティの主な機能と使用方法は次のとおりです。

ウィンドウのサイズを取得するには、ブラウザごとに異なるプロパティとメソッドを使用する必要があります。ウィンドウの実際のサイズを検出するには、IE で Netscape のウィンドウのプロパティを使用する必要があります。 DOM 環境でボディ検出を実行するためにドキュメントの奥深くでウィンドウのサイズを取得したい場合は、要素ではなくルート要素のサイズに注意を払う必要があります。

Window オブジェクトの innerWidth プロパティには、現在のウィンドウの内側の幅が含まれます。 Window オブジェクトの innerHeight プロパティには、現在のウィンドウの内側の高さが含まれます。

Document オブジェクトの body 属性は、HTML ドキュメントのタグに対応します。 Document オブジェクトの documentElement プロパティは、HTML ドキュメントのルート ノードを表します。

document.body.clientHeight は、HTML ドキュメントが配置されているウィンドウの現在の高さを表します。 document.body.clientWidth は、HTML ドキュメントが配置されているウィンドウの現在の幅を表します。

実装コード

コードをコピー コードは次のとおりです:

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional .dtd">


ブラウザ ウィンドウのサイズを変更してください< ; /title><br><meta http-equiv="content-type" content="text/html; charset=gb2312"><br></head><br><body><br> < ;h2 align="center">ブラウザ ウィンドウのサイズを変更してください</h2><hr><br><form action="#" method="get" name="form1" id="form1 "> <br><!--ブラウザ ウィンドウの実際のサイズを表示します--><br>ブラウザ ウィンドウの実際の高さ: <input type="text" name="availHeight" size="4" > ;<br><br>ブラウザ ウィンドウの実際の幅: <input type="text" name="availWidth" size="4"><br><br></form><br><script type="text/javascript"><br><!-- <BR>var winWidth = 0;<BR>var winHeight = 0;<BR>function findDimensions() //関数: 寸法を取得<BR>{<BR><STRONG>//ウィンドウ幅を取得<BR></STRONG>if (window.innerWidth)<BR>winWidth = window.innerWidth;<BR>else if ((document.body) && (document .body.clientWidth))<BR>winWidth = document.body.clientWidth;<BR><STRONG>//ウィンドウの高さを取得します<BR></STRONG>if (window.innerHeight)<BR>winHeight = window.innerHeight; <BR>else if ((document.body) && (document.body.clientHeight))<BR>winHeight = document.body.clientHeight;<BR>//Document の奥深くまで進んで本文を検出し、ウィンドウ サイズを取得します<BR> if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth)<BR>{<BR>winHeight = document.documentElement.clientHeight;<BR>winWidth = document.documentElement.clientWidth;<BR> }<BR>//結果は 2 つのテキスト ボックスに出力されます <BR>document.form1.availHeight.value= winHeight;<BR>document.form1.availWidth.value= winWidth;<BR>}<BR>findDimensions() ;<BR> //関数を呼び出して値を取得します <BR>window.onresize=findDimensions;<BR>//--><BR></script><br></body><br> </html> <br> </div> <br><strong>ソースプログラムの解釈</strong> <p> (1) プログラムはまず、ウィンドウの現在の幅と高さを表示する 2 つのテキスト ボックスを含むフォームを作成します。これらの値は、ウィンドウ サイズの変更に応じて変更されます。 </p> <p> (2) 以降の JavaScript コードでは、ウィンドウの高さと幅の値を保存するために、最初に 2 つの変数 winWidth と winHeight が定義されます。 </p> <p>(3) 次に、関数 findDimensions () で、window.innerHeight と window.innerWidth を使用してウィンドウの高さと幅を取得し、それらを前述の 2 つの変数に保存します。 </p> <p>(4) 次に、Document の奥深くまで進んで本文を検出し、ウィンドウ サイズを取得して、上記の 2 つの変数に格納します。 </p> <p>(5) 関数の最後にフォーム要素に名前でアクセスすると、結果が2つのテキストボックスに出力されます。 </p> <p>(6) JavaScript コードの最後で、findDimensions () 関数を呼び出して操作全体を完了します。 </p></div><div class="nphpQianMsg"><div class="clear"></div></div><div class="nphpQianSheng"><span>声明:</span><div>この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。</div></div></div><div class="nphpSytBox"><span>前の記事:<a class="dBlack" title="JSクリップボードアプリケーションのclipboardData_javascriptスキルの詳細な分析" href="http://m.php.cn/ja/faq/14857.html">JSクリップボードアプリケーションのclipboardData_javascriptスキルの詳細な分析</a></span><span>次の記事:<a class="dBlack" title="JSクリップボードアプリケーションのclipboardData_javascriptスキルの詳細な分析" href="http://m.php.cn/ja/faq/14859.html">JSクリップボードアプリケーションのclipboardData_javascriptスキルの詳細な分析</a></span></div><div class="nphpSytBox2"><div class="nphpZbktTitle"><h2>関連記事</h2><em><a href="http://m.php.cn/ja/article.html" class="bBlack"><i>続きを見る</i><b></b></a></em><div class="clear"></div></div><ul class="nphpXgwzList"><li><b></b><a href="http://m.php.cn/ja/faq/1609.html" title="Bootstrap リスト グループ コンポーネントの詳細な分析" class="aBlack">Bootstrap リスト グループ コンポーネントの詳細な分析</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/ja/faq/1640.html" title="JavaScript関数のカリー化の詳細説明" class="aBlack">JavaScript関数のカリー化の詳細説明</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/ja/faq/1949.html" title="JS パスワードの生成と強度検出の完全な例 (デモ ソース コードのダウンロード付き)" class="aBlack">JS パスワードの生成と強度検出の完全な例 (デモ ソース コードのダウンロード付き)</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/ja/faq/2248.html" title="Angularjs は WeChat UI (weui) を統合します" class="aBlack">Angularjs は WeChat UI (weui) を統合します</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/ja/faq/2351.html" title="JavaScript を使用して繁体字中国語と簡体字中国語をすばやく切り替える方法と、簡体字中国語と繁体字中国語の切り替えをサポートする Web サイトのトリック_javascript スキル" class="aBlack">JavaScript を使用して繁体字中国語と簡体字中国語をすばやく切り替える方法と、簡体字中国語と繁体字中国語の切り替えをサポートする Web サイトのトリック_javascript スキル</a><div class="clear"></div></li></ul></div></div><div class="nphpFoot"><div class="nphpFootBg"><ul class="nphpFootMenu"><li><a href="http://m.php.cn/ja/"><b class="icon1"></b><p>ホームページ</p></a></li><li><a href="http://m.php.cn/ja/course.html"><b class="icon2"></b><p>コース</p></a></li><li><a href="http://m.php.cn/ja/wenda.html"><b class="icon4"></b><p>に質問</p></a></li><li><a href="http://m.php.cn/ja/login"><b class="icon5"></b><p>私の</p></a></li><div class="clear"></div></ul></div></div><div class="nphpYouBox" style="display: none;"><div class="nphpYouBg"><div class="nphpYouTitle"><span onclick="$('.nphpYouBox').hide()"></span><a href="http://m.php.cn/ja/"></a><div class="clear"></div></div><ul class="nphpYouList"><li><a href="http://m.php.cn/ja/"><b class="icon1"></b><span>ホームページ</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/ja/course.html"><b class="icon2"></b><span>コース</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/ja/article.html"><b class="icon3"></b><span>記事</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/ja/wenda.html"><b class="icon4"></b><span>に質問</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/ja/dic.html"><b class="icon6"></b><span>辞書</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/ja/course/type/99.html"><b class="icon7"></b><span>マニュアル</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/ja/xiazai/"><b class="icon8"></b><span>ダウンロード</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/ja/faq/zt" title="特集"><b class="icon12"></b><span>特集</span><div class="clear"></div></a></li><div class="clear"></div></ul></div></div><div class="nphpDing" style="display: none;"><div class="nphpDinglogo"><a href="http://m.php.cn/ja/"></a></div><div class="nphpNavIn1"><div class="swiper-container nphpNavSwiper1"><div class="swiper-wrapper"><div class="swiper-slide"><a href="http://m.php.cn/ja/" >ホームページ</a></div><div class="swiper-slide"><a href="http://m.php.cn/ja/article.html" class="hover">記事</a></div><div class="swiper-slide"><a href="http://m.php.cn/ja/wenda.html" >に質問</a></div><div class="swiper-slide"><a href="http://m.php.cn/ja/course.html" >コース</a></div><div class="swiper-slide"><a href="http://m.php.cn/ja/faq/zt" >特集</a></div><div class="swiper-slide"><a href="http://m.php.cn/ja/xiazai" >ダウンロード</a></div><div class="swiper-slide"><a href="http://m.php.cn/ja/game" >ゲーム</a></div><div class="swiper-slide"><a href="http://m.php.cn/ja/dic.html" >辞書</a></div><div class="clear"></div></div></div><div class="langadivs" ><a href="javascript:;" class="bg4 bglanguage"></a><div class="langadiv" ><a onclick="javascript:setlang('zh-cn');" class="language course-right-orders chooselan " href="javascript:;"><span>简体中文</span><span>(ZH-CN)</span></a><a onclick="javascript:setlang('en');" class="language course-right-orders chooselan " href="javascript:;"><span>English</span><span>(EN)</span></a><a onclick="javascript:setlang('zh-tw');" class="language course-right-orders chooselan " href="javascript:;"><span>繁体中文</span><span>(ZH-TW)</span></a><a onclick="javascript:;" class="language course-right-orders chooselan chooselanguage" href="javascript:;"><span>日本語</span><span>(JA)</span></a><a onclick="javascript:setlang('ko');" class="language course-right-orders chooselan " href="javascript:;"><span>한국어</span><span>(KO)</span></a><a onclick="javascript:setlang('ms');" class="language course-right-orders chooselan " href="javascript:;"><span>Melayu</span><span>(MS)</span></a><a onclick="javascript:setlang('fr');" class="language course-right-orders chooselan " href="javascript:;"><span>Français</span><span>(FR)</span></a><a onclick="javascript:setlang('de');" class="language course-right-orders chooselan " href="javascript:;"><span>Deutsch</span><span>(DE)</span></a></div></div><script> var swiper = new Swiper('.nphpNavSwiper1', { slidesPerView : 'auto', observer: true,//修改swiper自己或子元素时,自动初始化swiper observeParents: true,//修改swiper的父元素时,自动初始化swiper }); </script></div></div><!--顶部导航 end--><script>isLogin = 0;</script><script type="text/javascript" src="/static/layui/layui.js"></script><script type="text/javascript" src="/static/js/global.js?4.9.47"></script></div><script src="https://vdse.bdstatic.com//search-video.v1.min.js"></script><link rel='stylesheet' id='_main-css' href='/static/css/viewer.min.css' type='text/css' media='all'/><script type='text/javascript' src='/static/js/viewer.min.js?1'></script><script type='text/javascript' src='/static/js/jquery-viewer.min.js'></script><script>jQuery.fn.wait = function (func, times, interval) { var _times = times || -1, //100次 _interval = interval || 20, //20毫秒每次 _self = this, _selector = this.selector, //选择器 _iIntervalID; //定时器id if( this.length ){ //如果已经获取到了,就直接执行函数 func && func.call(this); } else { _iIntervalID = setInterval(function() { if(!_times) { //是0就退出 clearInterval(_iIntervalID); } _times <= 0 || _times--; //如果是正数就 -- _self = $(_selector); //再次选择 if( _self.length ) { //判断是否取到 func && func.call(_self); clearInterval(_iIntervalID); } }, _interval); } return this; } $("table.syntaxhighlighter").wait(function() { $('table.syntaxhighlighter').append("<p class='cnblogs_code_footer'><span class='cnblogs_code_footer_icon'></span></p>"); }); $(document).on("click", ".cnblogs_code_footer",function(){ $(this).parents('table.syntaxhighlighter').css('display','inline-table');$(this).hide(); }); $('.nphpQianCont').viewer({navbar:true,title:false,toolbar:false,movable:false,viewed:function(){$('img').click(function(){$('.viewer-close').trigger('click');});}}); </script></body></html>