<h1>Jquery Toggle Button</h1>
<div class="toggle-btn active">
<input type="checkbox" checked class="cb-value" />
<span class="round-btn"></span>
</div>
<div class="toggle-btn">
<input type="checkbox" class="cb-value" />
<span class="round-btn"></span>
</div>
<!-- follow me template -->
<div class="made-with-love">
Made with
<i>♥</i> by
<a target="_blank" href="https://codepen.io/nikhil8krishnan">Nikhil Krishnan</a>
</div>
@import url(https://fonts.googleapis.com/css?family=Ubuntu);
body{
text-align: center;
padding: 10% 50px;
background-color: #2c3e50;
}
h1{
font-family: 'Ubuntu', sans-serif;
color: #fff;
font-size: 40px;
font-weight: normal;
margin-bottom: 50px;
}
.toggle-btn{
width: 80px;
height: 40px;
margin: 10px;
border-radius: 50px;
display: inline-block;
position: relative;
background : url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAyklEQVQ4T42TaxHCQAyENw5wAhLACVUAUkABOCkSwEkdhNmbpHNckzv689L98toIAKjqGcAFwElEFr5ln6ruAMwA7iLyFBM/TPDuQSrxwf6fCKBoX2UMIYGYkg8BLOnVg2RiAEexGaQQq4w9e9klcxGLLAUwgDAcihlYAR1IvZA1sz/+AAaQjXhTQQVoe2Yo3E7UQiT2ijeQdojRtClOfVKvMVyVpU594kZK9zzySWTlcNqZY9tjCsUds00+A57z1e35xzlzJjee8xf0HYp+cOZQUQAAAABJRU5ErkJggg==') no-repeat 50px center #e74c3c;
cursor: pointer;
-webkit-transition: background-color .40s ease-in-out;
-moz-transition: background-color .40s ease-in-out;
-o-transition: background-color .40s ease-in-out;
transition: background-color .40s ease-in-out;
cursor:pointer;
&.active{
background : url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAmUlEQVQ4T6WT0RWDMAhFeZs4ipu0mawZpaO4yevBc6hUIWLNd+4NeQDk5sE/PMkZwFvZywKSTxF5iUgH0C4JHGyF97IggFVSqyCFga0CvQSg70Mdwd8QSSr4sGBMcgavAgdvwQCtApvA2uKr1x7Pu++06ItrF5LXPB/CP4M0kKTwYRIDyRAOR9lJTuF0F0hOAJbKopVHOZN9ACS0UgowIx8ZAAAAAElFTkSuQmCC') no-repeat 10px center #2ecc71;
.round-btn{
left: 45px;
}
}
.round-btn{
width: 30px;
height: 30px;
background-color: #fff;
border-radius: 50%;
display: inline-block;
position: absolute;
left: 5px;
top: 50%;
margin-top: -15px;
-webkit-transition: all .30s ease-in-out;
-moz-transition: all .30s ease-in-out;
-o-transition: all .30s ease-in-out;
transition: all .30s ease-in-out;
}
.cb-value{
position: absolute;
left:0;
right:0;
width: 100%;
height: 100%;
opacity: 0;
z-index: 9;
cursor:pointer;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
}
}
//follow me template
.made-with-love{
position: fixed;
left: 0;
width: 100%;
bottom: 10px;
text-align: center;
font-size: 10px;
z-index: 9999;
font-family: arial;
color: #fff;
i{
font-style: normal;
color: #F50057;
font-size: 14px;
position: relative;
top: 2px;
}
a{
color: #fff;
text-decoration: none;
&:hover{
text-decoration: underline;
}
}
}
View Compiled
//$('input.cb-value').prop("checked", true);
$('.cb-value').click(function() {
var mainParent = $(this).parent('.toggle-btn');
if($(mainParent).find('input.cb-value').is(':checked')) {
$(mainParent).addClass('active');
} else {
$(mainParent).removeClass('active');
}
})
This Pen doesn't use any external CSS resources.