Switch button

by Олексій Рачок

HTML

<div class="wrap">
  <div class="button">
    <div class="slider"></div>
  </div>
</div>

CSS

.wrap {
  position: relative;
  width: 250px;
  height: 100px;
  border: solid 4px #F2F2F2;
  margin: 20% auto;
  box-shadow: 0px 1px 5px rgba(0,0,0,0.3);
}

.wrap .button {
  position: relative;
  width: 250px;
  height: 100px;
  cursor: pointer;
  background: #F2F2F2;
  transition: all 0.3s ease-out;
}

.wrap .button.active {
  background: white;
}

.wrap .button .slider {
  position: absolute;
  top: 0;
  left: 0;
  width: 125px;
  height: 100px;
  background: #37465D;
  color: #F2F2F2;
  text-align: center;
  line-height: 96px;
  font-size: 1.4em;
  box-shadow: 4px 0px 8px rgba(0,0,0,0.1);
  text-shadow: 0px 2px 2px rgba(0,0,0,0.4);
  transition: all 0.3s ease-out;
}

.wrap .button .slider:before {
  content: "OFF"
}
.wrap .button.active .slider:before {
  content: "ON"
}

.wrap .button.active .slider {
  left: 125px;
  right: 0;
  background: #9DC02E;
  box-shadow: -4px 0px 8px rgba(0,0,0,0.1);
}

JavaScript

$('.button').on('click', function(){
  $(this).toggleClass('active');
});