2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

jQueryの便利な使い方①~resize~

Last updated at Posted at 2019-04-02

今回は、コーディングの強い味方、jQueryの便利な使い方を見ていきます。
jQueryの環境構築は思っているものとさせて頂きます。

resize

resizeはjQueryのメソッドです。
役目としては、ウィンドウのサイズが変わった時に、そのサイズを取得するというものです。
何のことだか、という感じですよね。

使い方は、

hello.html

<script>

jQuery("window").resize(function(){

//widthやheightの値を得る処理
    var h = $('window').height();
    var w = $('window').width();

//上記の変数を代入して、htmlコードやcssコードと連動させる
  

});

</script>

という感じです。
resize()の中身は関数です。
また、取得対象は常に「window」になります。

例えば、position:fixedのleftとtopを調整し、画面の真ん中にモーダルを配置したい時、transformなどを利用すると思いますが、jQueryのresizeでも簡単にできちゃいます。

モーダルのwidthとheightを取得して、windowのwidthとheightからそれらを引き、2で割るとちょうど真ん中に配置されます。

画面がリサイズされるのに連動して、常に真ん中の位置をモーダルがキープできるので便利です。

ぜひ、使ってみてくださいね。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?