JavaScript、HTML、jQuery で画面サイズ、現在のウェブページ、ブラウザウィンドウのサイズを取得する方法
JavaScript、HTML、jQuery を使って画面サイズ、現在のウェブページ、ブラウザウィンドウのサイズを取得する方法
ここでは、JavaScript、HTML、jQuery を使ってこれらのサイズを取得する方法を解説します。
画面サイズは、window.screen
オブジェクトを使って取得することができます。
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>画面サイズを取得</title>
</head>
<body>
<script>
// 画面幅
const screenWidth = window.screen.width;
// 画面高
const screenHeight = window.screen.height;
console.log(`画面幅: ${screenWidth}px`);
console.log(`画面高: ${screenHeight}px`);
</script>
</body>
</html>
このコードは、window.screen.width
と window.screen.height
プロパティを使って、画面幅と画面高を取得し、コンソールに出力します。
現在のウェブページのサイズは、document.body
オブジェクトの scrollWidth
と scrollHeight
プロパティを使って取得することができます。
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>現在のウェブページのサイズを取得</title>
</head>
<body>
<script>
// ウェブページ幅
const pageWidth = document.body.scrollWidth;
// ウェブページ高
const pageHeight = document.body.scrollHeight;
console.log(`ウェブページ幅: ${pageWidth}px`);
console.log(`ウェブページ高: ${pageHeight}px`);
</script>
</body>
</html>
このコードは、document.body.scrollWidth
と document.body.scrollHeight
プロパティを使って、ウェブページ幅とウェブページ高を取得し、コンソールに出力します。
ブラウザウィンドウのサイズは、window.innerWidth
と window.innerHeight
プロパティを使って取得することができます。
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>ブラウザウィンドウのサイズを取得</title>
</head>
<body>
<script>
// ブラウザウィンドウ幅
const windowWidth = window.innerWidth;
// ブラウザウィンドウ高
const windowHeight = window.innerHeight;
console.log(`ブラウザウィンドウ幅: ${windowWidth}px`);
console.log(`ブラウザウィンドウ高: ${windowHeight}px`);
</script>
</body>
</html>
jQuery を使うと、これらのサイズをより簡単に取得することができます。
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>jQuery を使ってサイズを取得</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<script>
// 画面幅
const screenWidth = $(window).width();
// 画面高
const screenHeight = $(window).height();
// ウェブページ幅
const pageWidth = $(document).width();
// ウェブページ高
const pageHeight = $(document).height();
// ブラウザウィンドウ幅
const windowWidth = $(window).innerWidth();
// ブラウザウィンドウ高
const windowHeight = $(window).innerHeight();
console.log(`画面幅: ${screenWidth}px`);
console.log(`画面高: ${screenHeight}px`);
console.log(`ウェブページ幅: ${pageWidth}px`);
console.log(`ウェブページ高: ${pageHeight}px`);
console.log(`ブラウザウィンドウ幅: ${windowWidth}px`);
console.log(`ブラウザウィンドウ高: ${windowHeight}px
画面サイズ
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>画面サイズを取得</title>
</head>
<body>
<script>
// 画面幅
const screenWidth = window.screen.width;
// 画面高
const screenHeight = window.screen.height;
console.log(`画面幅: ${screenWidth}px`);
console.log(`画面高: ${screenHeight}px`);
</script>
</body>
</html>
現在のウェブページ
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>現在のウェブページのサイズを取得</title>
</head>
<body>
<script>
// ウェブページ幅
const pageWidth = document.body.scrollWidth;
// ウェブページ高
const pageHeight = document.body.scrollHeight;
console.log(`ウェブページ幅: ${pageWidth}px`);
console.log(`ウェブページ高: ${pageHeight}px`);
</script>
</body>
</html>
ブラウザウィンドウ
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>ブラウザウィンドウのサイズを取得</title>
</head>
<body>
<script>
// ブラウザウィンドウ幅
const windowWidth = window.innerWidth;
// ブラウザウィンドウ高
const windowHeight = window.innerHeight;
console.log(`ブラウザウィンドウ幅: ${windowWidth}px`);
console.log(`ブラウザウィンドウ高: ${windowHeight}px`);
</script>
</body>
</html>
jQuery を使う
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>jQuery を使ってサイズを取得</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<script>
// 画面幅
const screenWidth = $(window).width();
// 画面高
const screenHeight = $(window).height();
// ウェブページ幅
const pageWidth = $(document).width();
// ウェブページ高
const pageHeight = $(document).height();
// ブラウザウィンドウ幅
const windowWidth = $(window).innerWidth();
// ブラウザウィンドウ高
const windowHeight = $(window).innerHeight();
console.log(`画面幅: ${screenWidth}px`);
console.log(`画面高: ${screenHeight}px`);
console.log(`ウェブページ幅: ${pageWidth}px`);
console.log(`ウェブページ高: ${pageHeight}px`);
console.log(`ブラウザウィンドウ幅: ${windowWidth}px`);
console.log(`ブラウザウィンドウ高: ${windowHeight}px`);
</script>
</body>
</html>
画面サイズ、現在のウェブページ、ブラウザウィンドウのサイズを取得するその他の方法
document.documentElement
オブジェクトは、HTML ドキュメント全体の要素を表します。このオブジェクトの clientWidth
と clientHeight
プロパティを使って、ブラウザウィンドウの幅と高さを取得することができます。
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>ブラウザウィンドウのサイズを取得</title>
</head>
<body>
<script>
// ブラウザウィンドウ幅
const windowWidth = document.documentElement.clientWidth;
// ブラウザウィンドウ高
const windowHeight = document.documentElement.clientHeight;
console.log(`ブラウザウィンドウ幅: ${windowWidth}px`);
console.log(`ブラウザウィンドウ高: ${windowHeight}px`);
</script>
</body>
</html>
getBoundingClientRect()
メソッドは、要素のサイズと位置に関する情報を取得することができます。このメソッドを使って、ブラウザウィンドウ、現在のウェブページ、画面のサイズを取得することができます。
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>ブラウザウィンドウ、現在のウェブページ、画面のサイズを取得</title>
</head>
<body>
<script>
// ブラウザウィンドウ
const windowRect = window.getBoundingClientRect();
const windowWidth = windowRect.width;
const windowHeight = windowRect.height;
// 現在のウェブページ
const pageRect = document.body.getBoundingClientRect();
const pageWidth = pageRect.width;
const pageHeight = pageRect.height;
// 画面
const screenRect = screen.getBoundingClientRect();
const screenWidth = screenRect.width;
const screenHeight = screenRect.height;
console.log(`ブラウザウィンドウ幅: ${windowWidth}px`);
console.log(`ブラウザウィンドウ高: ${windowHeight}px`);
console.log(`ウェブページ幅: ${pageWidth}px`);
console.log(`ウェブページ高: ${pageHeight}px`);
console.log(`画面幅: ${screenWidth}px`);
console.log(`画面高: ${screenHeight}px`);
</script>
</body>
</html>
CSS メディアクエリを使って、画面サイズやブラウザウィンドウのサイズに応じてスタイルを変化させることができます。
@media (min-width: 768px) {
/* 画面幅が768px以上の時 */
body {
background-color: red;
}
}
@media (max-width: 767px) {
/* 画面幅が767px以下の時 */
body {
background-color: blue;
}
}
これらの方法は、それぞれ異なる利点と欠点があります。どの方法を使うかは、目的に合わせて選択する必要があります。
補足
- 画面サイズ、現在のウェブページ、ブラウザウィンドウのサイズは、ユーザーがブラウザのズーム機能を使うと変化します。
- 画面サイズ、現在のウェブページ、ブラウザウィンドウのサイズは、JavaScript を使って動的に変更することができます。
javascript html jquery