JSFiddle - React, Tailwind, and code Playground
HTML
<input type="checkbox" class="ios" />
CSS
.ios-ui-select{
background: #dddddd;
border: none !important;
height: 36px;
border: none !important;
background: #dddddd;
-webkit-border-radius: 18px;
border-radius: 18px;
width: 60px;
-webkit-transition: all 0.3s ease-in-out;
-moz-transition: all 0.3s ease-in-out;
-ms-transition: all 0.3s ease-in-out;
-o-transition: all 0.3s ease-in-out;
transition: all 0.3s ease-in-out;
-webkit-box-shadow: none;
box-shadow: none;
cursor: pointer;
position: relative;
display: inline-block;
}
.ios-ui-select.checked{
-webkit-box-shadow: inset 0 0 0 36px #6ddc5f;
box-shadow: inset 0 0 0 36px #6ddc5f;
}
.ios-ui-select.checked .inner{
left: 27px;
}
.ios-ui-select .inner{
width: 30px;
height: 30px;
position: absolute;
top: 3px;
left: 3px;
-webkit-border-radius: 100%;
border-radius: 100%;
background: white;
-webkit-transition: all 350ms cubic-bezier(0, 0.89, 0.44, 1);
-moz-transition: all 350ms cubic-bezier(0, 0.89, 0.44, 1);
-o-transition: all 350ms cubic-bezier(0, 0.89, 0.44, 1);
transition: all 350ms cubic-bezier(0, 0.89, 0.44, 1);
-webkit-box-shadow: 0 1px 2px 0 rgba(0,0,0,0.2),0 3px 4px 0 rgba(0,0,0,0.1);
box-shadow: 0 1px 2px 0 rgba(0,0,0,0.2),0 3px 4px 0 rgba(0,0,0,0.1);
}
/* Alternative Theme */
/* ---------------- */
/*
.ios-ui-select{
border-color: #d1d1d1;
width: 28px;
height: 28px;
-webkit-border-radius: 100%;
border-radius: 100%;
opacity: 1;
-webkit-box-shadow: inset 0 0 0 2px #d6e0e6;
box-shadow: inset 0 0 0 2px #d6e0e6;
width: 22px;
z-index: 2;
text-align: center;
display: inline-block;
cursor: pointer;
height: 22px;
-webkit-border-radius: 18px;
border-radius: 18px;
-webkit-transition: all 350ms cubic-bezier(0, 0.89, 0.44, 1);
-moz-transition: all 350ms cubic-bezier(0, 0.89, 0.44, 1);
-o-transition: all 350ms cubic-bezier(0, 0.89, 0.44, 1);
transition: all 350ms cubic-bezier(0, 0.89, 0.44, 1);
text-indent: -9999px;
display: inline-block;
}
.ios-ui-select.checked {
-webkit-box-shadow: inset 0 0 0 14px...
JavaScript
/**
* iosCheckbox.js
* Version: 1.0.0
* Author: Ron Masas
*/
(function($) {
$.fn.extend({
iosCheckbox: function() {
this.destroy = function(){
$(this).each(function() {
$(this).next('.ios-ui-select').remove();
});
};
if ($(this).attr('data-ios-checkbox') === 'true') {
return;
}
$(this).attr('data-ios-checkbox', 'true');
$(this).each(function() {
/**
* Original checkbox element
*/
var org_checkbox = $(this);
/**
* iOS checkbox div
*/
var ios_checkbox = jQuery("<div>", {
class: 'ios-ui-select'
}).append(jQuery("<div>", {
class: 'inner'
}));
// If the original checkbox is checked, add checked class to the ios checkbox.
if (org_checkbox.is(":checked")) {
ios_checkbox.addClass("checked");
}
// Hide the original checkbox and print the new one.
org_checkbox.hide().after(ios_checkbox);
if (org_checkbox.is(":disabled")){
// In case the original checkbox is disabled don't register the click event.
return ios_checkbox.css('opacity','0.6');
}
// Add click event listener to the ios checkbox
ios_checkbox.click(function() {
// Toggel the check state
ios_checkbox.toggleClass("checked");
// Check if the ios checkbox is checked
if (ios_checkbox.hasClass("checked")) {
// Update state
org_checkbox.prop('checked', true);
...