LoginSignup
2

More than 3 years have passed since last update.

御託は良いから、jsで画面幅(width)を取得する方法だけ教えて!

Last updated at Posted at 2019-09-11

つまり、bodyのサイズは画面のサイズだったんだよ!

const width = document.getElementsByTagName('body')[0].clientWidth
console.log(width)
> 1920

ナ、ナンダッテー > ΩΩΩ

補記

bodyborder, margin, paddingなどのstyleがかかっている場合、正確な値が取れないようです。その際はwindow.innerWidthから直接値が拾えるので、こっちの方が良さそうですね~。

const width = window.innerWidth
console.log(width)
> 1920

参考・引用・出典

JavaScriptで特定の要素の幅と高さを取得する方法

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