v2.5.2
Giriş yap

javascript ile if else yapısı

azizyildiz
348 defa görüntülendi

Selam arkadaşlar js ile web sitemin genişliğine göre css class ekleyip çıkartma yapacak kod lazım yardımcı olabilirmisiniz

yerdemli
919 gün önce

Bu codepen iş görecektir. Silinir milinir kodları buraya da ekliyorum.

<div class="test">
  <h3>Hello World</h3>
  <p>The class of <code>blue</code> is added when I am wider than 600px and is removed when I am 600px or less in width.</p>
</div>
body {
  font: normal 16px/1.5em sans-serif; 
  color: #111;
}

code {
  background: #ccc;
}

.test {
  background: #fffccc;
  border: solid 3px rgba(0,0,0,0.1);
  margin: 20px;
  padding: 40px 20px;
}

.test.blue {
  background: dodgerBlue;
}

.test.green {
  background: green;
}
jQuery(document).ready(function($) {
  var alterClass = function() {
    var ww = document.body.clientWidth;
    if (ww < 600) {
      $('.test').removeClass('blue');
    } else if (ww >= 601) {
      $('.test').addClass('blue');
    };
  };
  $(window).resize(function(){
    alterClass();
  });
  //Fire it when the page first loads:
  alterClass();
});