LOADING...

Preview

Pen ID
Unlock Campus Themeforest adv

 

Code

Check it


Welcome, switchers. These are standard checkboxes styled to mimick a switch UI of our mobile forefathers. Defying reason, they are even more fun to toggle than a standard checkbox — even while using a mouse. Actually, mostly while using a mouse.



Built using relative units, the size of a switch will be determined by the font-size of its parent container. When I get some time I'll roll this into a Sass/Bourbon mixin and call it a day.

Thanks and credit go to Bandar Raffa and Lea Verou who did most of the hard work. And to @desandro for being awesome and adding Draggabilly.

Questions/ideas/comments can go to @kerns. Happy switching.

CSS
/* Define active switch color */
/* Hide our checkboxes */
.check-switch input[type="checkbox"] {
  position: absolute;
  opacity: 0;
}

label.check-switch {
  clear: both;
  color: #999;
  display: inline-block;
  -webkit-tap-highlight-color: transparent;
}

/* Default Track */
.check-switch input[type="checkbox"] + .track {
  vertical-align: middle;
  width: 1.6588em;
  height: .95em;
  border: 1px solid rgba(0, 0, 0, 0.25);
  border-radius: 999px;
  background-color: rgba(0, 0, 0, 0.1);
  -webkit-transition-duration: .3s;
  -webkit-transition-property: background-color, box-shadow;
  box-shadow: inset 0 0 0 0px rgba(102, 5, 199, 0.75);
  margin: .5em 0 50px 0;
  cursor: pointer;
}

/* Default Track Checked */
.check-switch input[type="checkbox"]:checked + div {
  background-color: #b36afb;
  border: 1px solid rgba(102, 5, 199, 0.85);
  box-shadow: inset 0 0 0 0.5em rgba(179, 106, 251, 0);
}

/* Default Knob */
.check-switch input[type="checkbox"] + div > div {
  left: 0;
  width: .95em;
  height: .95em;
  border-radius: inherit;
  background: #ffffff;
  -webkit-transition-timing-function: cubic-bezier(0.54, 1.85, 0.5, 1);
  -webkit-transition-duration: 0.3s;
  -webkit-transition-property: transform, background-color, box-shadow;
  -moz-transition-timing-function: cubic-bezier(0.54, 1.85, 0.5, 1);
  -moz-transition-duration: 0.3s;
  -moz-transition-property: transform, background-color;
  box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.3), 0px 0px 0 1px rgba(0, 0, 0, 0.2);
  margin-top: 0;
}

/* Default Knob Checked */
.check-switch input[type="checkbox"]:checked + div > div {
  left: 0.699em;
  background-color: #fff;
  box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.3), 0 0 0 1px rgba(89, 4, 174, 0.5);
}

/* Fixes drag performance */
.check-switch .knob.is-dragging {
  -webkit-transition: none;
}

/* Demo page layout, not important */
body {
  -webkit-user-select: none;
  cursor: default;
  font: 22px "Helvetica Neue";
  color: rgba(0, 0, 0, 0.77);
  font-weight: 200;
  padding: 0 3em 5em 3em;
  background: -webkit-linear-gradient(top, #f2fbff 0%, #ffffff 64%) no-repeat;
  background: -moz-linear-gradient(top, #f2fbff 0%, #ffffff 64%) no-repeat;
  background: -ms-linear-gradient(top, #f2fbff 0%, #ffffff 64%) no-repeat;
  background: linear-gradient(to bottom, #f2fbff 0%, #ffffff 64%) no-repeat;
}

h1 {
  font-weight: 100;
  font-size: 4em;
  color: #999;
}

h2 {
  font-weight: 200;
  font-size: 22px;
  color: #8006f9;
}

h3 {
  font-weight: 200;
  font-size: 18px;
  color: rgba(0, 0, 0, 0.77);
  margin-top: 50px;
}

a:link {
  text-decoration: none;
  color: #f06;
}

a:visited {
  text-decoration: none;
  color: #f06;
}

a:hover {
  text-decoration: underline;
}

a:active {
  text-decoration: underline;
}

hr {
  border: 0;
  border-top: 1px solid #efefef;
}

p {
  color: #555;
  max-width: 35em;
  margin-top: 1em;
}

#main {
  clear: both;
  padding: 2em 2em 0;
}

h1 label {
  position: relative;
  top: .20em;
  display: block !important;
  width: 1.8em;
}
JS
// external js
// https://draggabilly.desandro.com/draggabilly.pkgd.js

// use left/top for all positioning
Draggabilly.prototype.positionDrag = Draggabilly.prototype.setLeftTop;

$( function() {
  $('.knob').each( function( i, knob ) {
    var draggie = new Draggabilly( knob, {
      containment: true,
      axis: 'x'
    });
    draggie.on( 'dragMove', onDragMove );
    draggie.on( 'dragEnd', onDragEnd );
  
  });
});

function onDragMove() {
  var $knob = $( this.element );
  var $label = $knob.parents('label');
  // on initial drag move
  if ( !this.hasDragged ) {
    this.movePotential = $knob.parent().width() - $knob.width();
    $label.on( 'click', onLabelClickDragging );
    this.input = $label.find('input')[0];
    this.hasDragged = true;
  }
  
  var isChecked = this.position.x > this.movePotential / 2;
  this.input.checked = isChecked;
}

// disable label clicks when dragging
function onLabelClickDragging( event ) {
  event.preventDefault();
}

function onDragEnd() {
  // clear out Draggabilly's position setting
  this.element.style.left = '';
  delete this.hasDragged;  
  var $knob = $( this.element );
  var $label = $knob.parents('label');
  setTimeout( function() {
    $label.off( 'click', onLabelClickDragging );
  });
}
Host Instantly Drag and Drop Website Builder

 

Description

Animated, touch friendly checkbox toggle.
Term
Mon, 11/27/2017 - 21:50

Related Codes

Pen ID
Pen ID
Pen ID