JSFiddle - React, Tailwind, and code Playground

by davidmurdoch

HTML

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
The checkbox is: <span id="status"></span> checked<br/><br/>
<div class="checkbox">
  <input id="check" checked type="checkbox" value="1" />
  <label for="check"><span>Check-me</span></label>
</div>
<p>
<a href="#" id="toggle">Toggle (via Javascript)</a> <br/>
<label for="check">Toggle (via HTML)</label>
</p>
<hr />
More info: <a href="http://davidmurdoch.com/2011/08/10/iphone-style-checkbox-using-only-css-no-images/">iPhone Style Checkbox Using Only CSS3 (No Images!)</a>

CSS

h1{
    font-size:1.5em;
    text-align:center;
    margin-bottom:1em;
}
p{
    margin:1em 0;
}

body {
    font-family: "Helvetica Neue", Helvetica, "Lucida Grande", Verdana, Arial, sans-serif, Helvetica;
}

.checkbox {
    margin:0 auto;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -o-user-select: none;
    user-select: none;
    position:relative;
    display: block;
    width:94px;
}    

.checkbox input[type=checkbox] {
    display: none;
}

.checkbox input[type=checkbox] + label {
    display: block;
    cursor:pointer;
    width: 94px;
    overflow: hidden;
    background:#fff;
    -webkit-border-radius:3px;
    -moz-border-radius:3px;
    border-radius:3px;
    background:-webkit-linear-gradient(left, #2d5ba8 50%, #999 50%) no-repeat -20px 0;
    background:-moz-linear-gradient(left, #2d5ba8 50%, #999 50%) no-repeat -20px 0;
    -webkit-transition: background-position 0.3s ease-in-out;
    -moz-transition: background-position 0.3s ease-in-out;
}

.checkbox input[type=checkbox] + label span,
.checkbox input[type=checkbox] + label span:before,
.checkbox input[type=checkbox] + label span:after{
    text-align:center;
    display:block;
    width:54px;
    height:27px;
    line-height:27px;
    position:absolute;
    top:-1px;
    font-weight:bold;
    text-indent: 0;
    -webkit-box-shadow:inset 1px 2px 2px 0 rgba(0,0,0,.2);
    -moz-box-shadow:inset 1px 2px 2px 0 rgba(0,0,0,.2);
    box-shadow:inset 1px 2px 2px 0 rgba(0,0,0,.2);
}
.checkbox input[type=checkbox] + label span{
    text-indent: -9999px;
    top:0;
    position:relative;
    left:0;
    width:37px;
    -webkit-border-radius:3px;
    -moz-border-radius:3px;
    border-radius:3px;
    background:-webkit-linear-gradient(#efefef 1px, #cecece 1px, #fbfbfb);
    background:-moz-linear-gradient(#efefef 1px, #cecece 1px, #fbfbfb);
    -webkit-transition: left 0.3s ease-in-out;
    -moz-transition: left 0.3s ease-in-out;
    border:solid 1px;
   ...

JavaScript

var checkbox = $('input[type="checkbox"]').change(function(){
    $("#status").text(this.checked ? "" : "not");
});

$("#toggle").click(function(e){
    e.preventDefault();
    checkbox.each(function(){this.checked=!this.checked;}).change();
});