Readermode Switcher
by Kevin Pliester
HTML
<div class="container">
<label class="readermode-switch-label">
<input type="checkbox" name="test" value="1" class="readermode-switch-input" />
<div class="readermode-switch -on" data-selector="test">
<span class="readermode-switch-on">Ja</span>
<span class="readermode-switch-off">Nein</span>
<div class="readermode-switch-slider"></div>
</div>
</label>
</div>
SCSS
.container {
width: auto;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%);
}
.readermode-switch-label {
position: relative;
}
.readermode-switch-input {
position: absolute;
z-index: 0;
top: 5px;
left: 5px;
}
.readermode-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;
}
}
.readermode-switch span {
display: inline-block;
line-height: 30px;
padding: 0 10px;
z-index: 2;
color: #333;
}
.readermode-switch span.readermode-switch-on {
color: #fff;
}
.readermode-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;
}
.readermode-switch.-on div {
border-color: #0d84e3;
bottom: 2px;
top: 2px;
left: 50%;
right: 2px;
color: #fff;
}
JavaScript
(function($) {
function handleReadermodeSwitcher() {
var switcher = $('.readermode-switch');
var switchInput = switcher.parent().find('.readermode-switch-input');
if ($("body").hasClass("readermode")) {
switchInput.prop('checked', true);
} else {
switchInput.removeAttr('checked');
}
}
$(function() {
handleReadermodeSwitcher();
$(document).on('click', '.readermode-switch', function(e) {
$(this).toggleClass('-on');
$("body").toggleClass("readermode");
handleReadermodeSwitcher();
e.preventDefault();
});
});
})(jQuery);