-
Notifications
You must be signed in to change notification settings - Fork 2
/
FancyCheckbox.js
121 lines (113 loc) · 3.08 KB
/
FancyCheckbox.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* Iphone styled checkboxes
* @author Ohad Raz http://en.bainternet.info
* @version 0.1
* @copyright 2012 Ohad Raz
*/
;(function ( $, window, document, undefined ) {
var options = {
style: "iphone", //iphone,firerift,
offClass: 'no-fancy', //class to disable elements with this class will not be fancy
};
var methods = {
init : function( settings ) {
if(options) {
$.extend(options,settings);
}
return this.each(function(){
var $elem = $(this);
methods.loadStyledCehckboxes($elem,options);
});
},
loadStyledCehckboxes: function($el, options){
var $ele = $el;
if ($ele.hasClass(options.offClass) || $ele.data('FancyCheckbox'))
return;
$ele.trigger('beforeLoad', $ele);
var thisID = $ele.attr('id');
var setClass = '';
switch(options.style) {
case "iphone":
setClass = "iphone-style";
break;
case "firerift":
setClass = "firerift-style";
break;
}
if($ele[0].checked == true){
setClass = setClass + ' on';
}else{
setClass = setClass + ' off';
}
$ele.addClass('hidden');
$ele.data('FancyCheckbox',true);
var d = $('<div>')
.addClass(setClass)
.attr('rel',thisID)
.html(' ');
switch(options.style) {
case "iphone":
d.on('click',function(){methods.OnClickIphone($ele);});
break;
case "firerift":
d.on('click',function(){methods.OnClickfirerift($ele);});
break;
}
$ele.after(d);
$ele.trigger('afterLoad', $ele);
},
OnClickIphone: function($el) {
$el_Styled = $el.next();
$el.trigger('beforeChangeIphone', $el);
if($el[0].checked) {
methods.toggleOnOff($el,$el_Styled,false,true);
} else {
methods.toggleOnOff($el,$el_Styled,true,true);
}
$el.trigger('afterChangeIphone', $el);
},
OnClickfirerift: function($el) {
$el_Styled = $el.next();
$el.trigger('beforeChangeFirerift', $el);;
if($el[0].checked) {
methods.toggleOnOff($el,$el_Styled,false);
} else {
methods.toggleOnOff($el,$el_Styled,true);
}
$el.trigger('afterChangeFirerift', $el);
},
toggleOnOff: function ($el,$s,onOff,anim){
anim = anim || false;
if (onOff){
if (anim)
$s.animate({backgroundPosition: '0% 100%'});
$el[0].checked = true;
$s.removeClass('off').addClass('on');
}else{
if (anim)
$s.animate({backgroundPosition: '100% 0%'});
$el[0].checked = false;
$s.removeClass('on').addClass('off');
}
//keep original change event
$el.trigger("change", $el);
},
destroy: function(){
$(this).each(function(){
$el = $(this);
$el.next().remove();
$.removeData($el,'FancyCheckbox');
$el.removeClass('hidden');
});
}
};
$.fn.FancyCheckbox = function( method ) {
if ( methods[method] ) {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.tooltip' );
}
};
})( jQuery, window, document );