-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathjquery.toggleswitch.js
48 lines (47 loc) · 2.03 KB
/
jquery.toggleswitch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
Title: Toggle Switch
URL: http://github.com/jamiebicknell/Toggle-Switch
Author: Jamie Bicknell
Twitter: @jamiebicknell
Thanks: @steve228uk for prop/var mods and plugin suggestion
*/
(function ($) {
'use strict';
$.fn.toggleSwitch = function (options) {
var settings = $.extend({
onClick: function () { return true; },
onChangeOn: function () { return true; },
onChangeOff: function () { return true; }
}, options);
$(this).each(function () {
var obj = $(this), status = obj.is(':checked') ? '' : ' off';
if (!obj.parent('div.switch').length) {
obj.wrap('<div class="switch"></div>');
obj.parent('div.switch').prepend('<span class="switched' + status + '" />').prepend('<div class="overlay" />');
}
obj.parent('div.switch').add($('label[for=' + obj.prop('id') + ']')).click(function (e) {
e.preventDefault();
if (!obj.prop('disabled')) {
var value, check;
settings.onClick.call(obj);
if ($(this).is('label')) {
value = $('#' + $(this).prop('for')).prev('span.switched');
check = $('#' + $(this).prop('for'));
} else {
value = $(this).children('span.switched');
check = $(this).children('input[type=checkbox]');
}
if (value.is('.off')) {
value.stop().animate({left: 0}, 150, 'linear').removeClass('off');
check.prop('checked', 'checked');
settings.onChangeOn.call(obj);
} else {
value.stop().animate({left: -21}, 150, 'linear').addClass('off');
check.prop('checked', '');
settings.onChangeOff.call(obj);
}
}
});
});
};
}(jQuery));