WordPress Switcher

Simple Switcher for WordPress

by Kevin Pliester

HTML

<div class="container">

  <label>
    <input type="checkbox" name="test" value="1" class="maschine-switch-input" checked="checked" />
    <div class="maschine-switch -on" data-selector="test">
      <span class="maschine-switch-on">Ja</span>
      <span class="maschine-switch-off">Nein</span>
      <div class="maschine-switch-slider"></div>
    </div>
  </label>

</div>

SCSS

.container {
  width: auto;
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%);
}

label {
  position: relative;
}

.maschine-switch-input {
  position: absolute;
  z-index: 0;
  top: 5px;
  left: 5px;
}

.maschine-switch {
  z-index: 1;
  position: relative;
  max-width: 100px;
  border-width: 1px;
  border-style: solid;
  border-radius: 5px;
  display: inline-block;
  cursor: pointer;
  height: 30px;
  vertical-align: middle;
  background-color: #f8f8f8;
  border-color: #ddd;
  transition: all .2s ease;
  &.-on {
    background-color: #309cf3;
    border-color: #2b9af3;
  }
}

.maschine-switch span {
  display: inline-block;
  line-height: 30px;
  padding: 0 10px;
  z-index: 2;
  color: #333;
}

.maschine-switch span.maschine-switch-on {
  color: #fff;
}

.maschine-switch div {
  position: absolute;
  z-index: 3;
  background-color: #fff;
  bottom: 2px;
  top: 2px;
  left: 2px;
  right: 50%;
  border-width: 1px;
  border-style: solid;
  border-radius: 5px;
  border-color: #ddd;
  transition: all .2s ease;
}

.maschine-switch.-on div {
  border-color: #0d84e3;
  bottom: 2px;
  top: 2px;
  left: 50%;
  right: 2px;
}

JavaScript

jQuery(function($) {
  $('.maschine-switch').on('click', function(event) {
    $(this).toggleClass('-on');
  });
});