jQueryで要素の高さを取得する - height(), innerHeight(), outerHeight()の違い

2024-04-02

jQueryで要素のレンダリングされた高さを取得する方法

概要

方法

height() メソッド

最も簡単な方法は height() メソッドを使用することです。これは、要素のコンテンツの高さ(パディングやボーダーを含む)を取得します。

// 要素の高さを取得
const height = $(element).height();

// 例
const element = $('.my-element');
const height = element.height();

console.log(height); // 100px
// 要素の内側の高さを取得
const innerHeight = $(element).innerHeight();

// 例
const element = $('.my-element');
const innerHeight = element.innerHeight();

console.log(innerHeight); // 80px
// 要素の外側の高さを取得
const outerHeight = $(element).outerHeight();

// 例
const element = $('.my-element');
const outerHeight = element.outerHeight();

console.log(outerHeight); // 120px

offset().height プロパティは、要素のコンテンツの高さ(パディング、ボーダー、マージンを含む)と、要素のスクロールバーの高さ(存在する場合)を取得します。

// 要素のオフセット高さを取得
const offsetHeight = $(element).offset().height;

// 例
const element = $('.my-element');
const offsetHeight = element.offset().height;

console.log(offsetHeight); // 120px

css() メソッドを使用して height プロパティを取得することもできます。

// 要素のCSS高さを取得
const height = $(element).css('height');

// 例
const element = $('.my-element');
const height = element.css('height');

console.log(height); // 100px

注意点

  • 要素がまだレンダリングされていない場合、これらのメソッドは正確な高さを返さない可能性があります。
  • 要素に display: none スタイルが設定されている場合、これらのメソッドは 0 を返します。
  • 要素に position: absolute または position: fixed スタイルが設定されている場合、これらのメソッドは要素のオフセット高さを返します。

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>
  <div class="my-element">
    <h1>タイトル</h1>
    <p>これはサンプルテキストです。</p>
  </div>

  <script>
    // 1. height() メソッド
    const height = $('.my-element').height();
    console.log('height:', height); // 100px

    // 2. innerHeight() メソッド
    const innerHeight = $('.my-element').innerHeight();
    console.log('innerHeight:', innerHeight); // 80px

    // 3. outerHeight() メソッド
    const outerHeight = $('.my-element').outerHeight();
    console.log('outerHeight:', outerHeight); // 120px

    // 4. offset().height プロパティ
    const offsetHeight = $('.my-element').offset().height;
    console.log('offsetHeight:', offsetHeight); // 120px

    // 5. css() メソッド
    const cssHeight = $('.my-element').css('height');
    console.log('cssHeight:', cssHeight); // 100px
  </script>
</body>
</html>

このコードを実行すると、コンソールに以下の出力が表示されます。

height: 100px
innerHeight: 80px
outerHeight: 120px
offsetHeight: 120px
cssHeight: 100px

各方法の詳細

// 要素の高さを取得
const height = $(element).height();

// 例
const element = $('.my-element');
const height = element.height();

console.log(height); // 100px
// 要素の内側の高さを取得
const innerHeight = $(element).innerHeight();

// 例
const element = $('.my-element');
const innerHeight = element.innerHeight();

console.log(innerHeight); // 80px
// 要素の外側の高さを取得
const outerHeight = $(element).outerHeight();

// 例
const element = $('.my-element');
const outerHeight = element.outerHeight();

console.log(outerHeight); // 120px
// 要素のオフセット高さを取得
const offsetHeight = $(element).offset().height;

// 例
const element = $('.my-element');
const offsetHeight = element.offset().height;

console.log(offsetHeight); // 120px
// 要素のCSS高さを取得
const height = $(element).css('height');

// 例
const element = $('.my-element');
const height = element.css('height');

console.log(height); // 100px

応用例

  • 要素の高さをに応じてレイアウトを変更する
  • スクロールバーの高さを設定する
  • 要素が画面内に収まるかどうかを確認する



jQueryで要素のレンダリングされた高さを取得するその他の方法

getBoundingClientRect() メソッドは、要素の座標とサイズに関する情報を取得します。この情報を使用して、要素のレンダリングされた高さを計算することができます。

// 要素のレンダリングされた高さを取得
function getHeight(element) {
  const rect = element.getBoundingClientRect();
  return rect.bottom - rect.top;
}

// 例
const element = $('.my-element');
const height = getHeight(element);

console.log(height); // 120px
// 要素の外側の高さを取得 (スクロールバーを含む)
const outerHeight = $(element).outerHeight(true);

// 例
const element = $('.my-element');
const outerHeight = element.outerHeight(true);

console.log(outerHeight); // 120px

position().topposition().bottom プロパティを使用して、要素のオフセットの上下位置を取得することができます。これらの値を使用して、要素のレンダリングされた高さを計算することができます。

// 要素のレンダリングされた高さを取得
function getHeight(element) {
  const position = element.position();
  return position.bottom - position.top;
}

// 例
const element = $('.my-element');
const height = getHeight(element);

console.log(height); // 120px

.height() メソッドと .css() メソッドを組み合わせて、要素のコンテンツの高さ(パディングやボーダーを含む)と、要素のスクロールバーの高さ(存在する場合)を取得することができます。

// 要素のレンダリングされた高さを取得
function getHeight(element) {
  const height = element.height();
  const scrollbarHeight = element.css('overflow-y') === 'scroll' ? element.prop('scrollHeight') - height : 0;
  return height + scrollbarHeight;
}

// 例
const element = $('.my-element');
const height = getHeight(element);

console.log(height); // 120px

javascript jquery css


HTMLとCSSでWebページをもっと自由に!要素の幅設定テクニック集

このプロパティは、要素の幅を親要素の幅の100%から100ピクセル引いた値に設定します。つまり、親要素の幅が常に100%になるように、要素の幅が自動的に調整されるのです。以下の例のように、width プロパティに calc(100% - 100px) を指定します。...


JavaScript, jQuery, DOMでリストをソートする

このチュートリアルでは、jQueryを使用してWebページのリスト項目をアルファベット順にソートする方法を説明します。必要なもの基本的なHTMLとCSSの知識jQueryライブラリの基本的な知識手順HTMLでリストを作成するまず、ソートしたい項目を含むリストを作成する必要があります。以下は例です。...


JavaScriptで「Hello」が「Hello, Hello World! How are you, Hello?」に何回出現するかを調べる2つの方法

このチュートリアルでは、JavaScript で正規表現を使用して、ある文字列(検索文字列)が別の文字列(対象文字列)内に何回出現するかを数える方法を説明します。 2 つの主要な方法を紹介します。match() メソッドを使用するそれぞれの方法について、詳細な説明とコード例、そして長所と短所を比較します。...


CSSセレクタ:>、、:nth-child()、:not()、::deep()、JavaScript、その他

説明: 親要素の直下の子要素のみを選択します。例:この例では、.parent要素の直下にある. child要素のみスタイルが適用されます。説明: 親要素の子要素の中で、特定の位置にある要素を選択します。説明: 指定された条件に合致しない要素を選択します。...


【保存版】Flexboxで簡単レイアウト!最後のアイテムを末尾に配置する方法

方法 1: justify-content: flex-end を使用する最も一般的な方法は、親コンテナに justify-content: flex-end プロパティを設定することです。これにより、すべてのフレックスアイテムがコンテナの左側(水平方向の場合)または上部(垂直方向の場合)に揃えられ、最後のアイテムが自動的に右端または下端に配置されます。...