ホームページ  >  記事  >  ウェブフロントエンド  >  ブラウザと画面の幅と高さの情報を取得するJSコード_JavaScriptスキル

ブラウザと画面の幅と高さの情報を取得するJSコード_JavaScriptスキル

WBOY
WBOYオリジナル
2016-05-16 16:54:031066ブラウズ

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
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 = width

offsetHeight = height

(言及する必要があります: CSS の margin 属性は clientWidth、offsetWidth、clientHeight、および offsetHeight と同じです) 関係ありません)

------------------

技術的なポイント

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

ウィンドウのサイズを取得するには、ブラウザごとに異なるプロパティとメソッドを使用する必要があります。ウィンドウの実際のサイズを検出するには、Netscape でウィンドウのプロパティを使用する必要があります。 IE でドキュメントの奥深くまで進み、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> <body> ;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> ;script type="text/javascript"> <br><– <BR>var winWidth = 0; <BR>var winHeight = 0; //関数: 寸法を取得します<BR> { <BR>//ウィンドウの幅を取得します<BR>if (window.innerWidth) <BR>winWidth = window.innerWidth; <BR>else if ((document.body) && (document.body.clientWidth)) <BR> winWidth = document.body.clientWidth; <BR>//ウィンドウの高さを取得します<BR>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>// 結果は 2 つのテキスト ボックスに出力されます <BR>document.form1.availHeight.value= winHeight; <BR>document.form1.availWidth.value= winWidth; <BR>findDimensions()>// 関数を呼び出して値を取得します<BR> window.onresize=findDimensions ; <BR></script> <br><br> <br> <br><br>(1) プログラムはまず、ウィンドウの現在の幅と高さを表示する 2 つのテキスト ボックスを含むフォームを作成します。これらの値は、ウィンドウ サイズの変更に応じて変化します。 <br><br> (2) 以降の JavaScript コードでは、ウィンドウの高さと幅の値を保存するために、最初に 2 つの変数 winWidth と winHeight が定義されます。 <br><br>(3) 次に、関数 findDimensions () で、window.innerHeight と window.innerWidth を使用してウィンドウの高さと幅を取得し、それらを前述の 2 つの変数に保存します。 <br><br> (4) 次に、Document の奥深くまで進んで本文を検出し、ウィンドウ サイズを取得して、上記の 2 つの変数に格納します。 </div> <br>(5) 関数の最後に、フォーム要素に名前でアクセスすると、結果が 2 つのテキスト ボックスに出力されます。 <br><br> (6) JavaScript コードの最後で、findDimensions () 関数を呼び出して操作全体を完了します。</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="text_javascript スキルの最後にカーソルをフォーカスするための JS 実装コード" href="http://m.php.cn/ja/faq/13511.html">text_javascript スキルの最後にカーソルをフォーカスするための JS 実装コード</a></span><span>次の記事:<a class="dBlack" title="text_javascript スキルの最後にカーソルをフォーカスするための JS 実装コード" href="http://m.php.cn/ja/faq/13513.html">text_javascript スキルの最後にカーソルをフォーカスするための JS 実装コード</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>