0
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 3 years have passed since last update.

画像が縦長か横長か自動判別してclassをつける

Last updated at Posted at 2021-02-05

事情があってobject-fitが使えない時に使いました。

//画像が縦向きか横向きかclassをつける
function imgAddClass(imgArr){
  if(imgArr.length > 0){
    for (let i = 0; i < imgArr.length; i++) {
      var e = imgArr[i];
      var width = e.naturalWidth;
      var height = e.naturalHeight;

      if(width < height){
        e.classList.add('tate-img');
      }else{
        e.classList.add('yoko-img');
      }
    }
  }
}

//判定したい要素を指定
var imgs = document.querySelectorAll('li img');

window.addEventListener('load', function(){
  imgAddClass(imgs);
}
0
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
0
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?