forked from mkuklis/slidebox
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslidebox.js
63 lines (51 loc) · 1.5 KB
/
slidebox.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* slidebox - Simple Zepto plugin which turns your checkboxes into iPhone like slideboxes
* Modified by 25th floor
*
* Copyright(c) 2011 Michal Kuklis <[email protected]>
* MIT Licensed
*
* @version 0.2
* @author Michal Kuklis <[email protected]>
* @author Thomas Subera <[email protected]>
* @author Jan Michael Auer <[email protected]>
*
* @see zepto.js framework http://zeptojs.com
*/
(function ($) {
$.fn.slidebox = function (options) {
var TMPL = '<label class="slidebox"><span></span></label>';
// default settings
var settings = {
change : function (el, onOff, key, value) { }, // change callback
speed : 0.2 // toggle speed
};
settings = $.extend(settings, options);
// toggle element
function toggle (check) {
var label = $(this),
checkbox = label.prev(),
checked = typeof check == 'boolean' ? !check : checkbox.attr('checked');
label.toggleClass('onoff', checked);
if (checked) {
checkbox.removeAttr('checked');
} else {
checkbox.attr('checked', true);
}
// execute callback in the context of the checkbox
settings.change.call(checkbox, checkbox, !checked);
}
this.each(function (el) {
var checkbox = $(this),
box = $(TMPL);
checkbox.after(box);
box.on('click', toggle)
.on('swipeLeft', function () { toggle.call(this, false); })
.on('swipeRight', function () { toggle.call(this, true); });
if (!checkbox.attr('checked')) {
box.addClass('onoff');
}
checkbox.hide();
});
}
})(Zepto);