2017-04-24 16 views
1

Reactでページの読み込み時にドキュメント全体の高さを調べるにはどうすればよいですか?したがって、jQueryでは次のようになります。Reactでドキュメントの高さを取得

$(document).height(); 

ありがとうございました!

+1

document.documentElement.offsetHeight – Karthik

+0

http://stackoverflow.com/questions/1145850/how-to-get-height-of-entire-document-with-javascript –

+0

ReactはDOMユーティリティライブラリではありませんが、それは機能を提供していません。あなたは、Reactなしで行うのと全く同じ方法で身長を取得します。 –

答えて

1

ページの読み込み時のドキュメントの高さを見つけることは、私の意見ではあまりにもトリッキーです! しかし、ページが読み込まれた後にそれを見つけるのは、もっと簡単な作業です。 したがって、 "componentDidMount()"関数でそれを探してみてください!あなたは、使用のいずれかを行うことができます

  • document.documentElement.offsetHeight
  • $(document).height()

(真実もリアクトで内蔵しているのjQueryのバージョン、あなたが実際に高さを得るために第二の方法を使用できるように)例えば

import React, {Component} from 'react'; 

export default class YourClass extends Component { 
    constructor(props){ 
     super(props); 
     // ... 
    } 

    componentDidMount() { 
     console.log("document-height",document.documentElement.offsetHeight); 
     console.log("jQuery-document-height",$(document).height()); 
    } 

    render(){ 
     // ... 
    } 

} 
+0

Reactで$(document).height()を使用すると$が定義されていません。 –

関連する問題