forked from bjtqti/study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckbox.js
41 lines (36 loc) · 828 Bytes
/
checkbox.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
function Checkbox (options){
var config = {
element:null,
checked:false,
onClick:function(){}
};
for(var k in config){
if(config.hasOwnProperty(k)){
this[k] = options[k]!==undefined?options[k]:config[k];
}
}
if(this.element===null){
return false;
}
this.checked ? this.on() : this.off();
this.bindEvent();
}
Checkbox.prototype.bindEvent = function(){
var it = this;
this.element.addEventListener('click',function(){
it.handleToggle();
});
}
Checkbox.prototype.handleToggle = function(){
var isChecked = !this.checked;
isChecked ? this.on() : this.off();
this.onClick(isChecked);
}
Checkbox.prototype.on = function(){
this.element.children[0].className = 'on';
this.checked = true;
}
Checkbox.prototype.off = function(){
this.element.children[0].className = 'off';
this.checked = false;
}