ui.checkSwitch

What's this?

"ui.checkswitch" is a javascript library and you can implement ios style switching checkbox by using this.

iOS style switching checkbox

Download

Implementation

Here is a checkbox, and this is to be base.

<input type="checkbox" id="checkbox" name="checkbox" value="1">

Then, reading "ui.checkswitch.css", "ui.checkswitch.js" and run ChechSwitch func.

<link rel="stylesheet" href="styles/ui.checkswitch.css">
<script src="scripts/ui.checkswitch.js"></script>
<script>
CheckSwitch('#checkbox');
// a 1st argument is set a target checkbox id
</script>

A checkbox targeted is replaced by HTML following. Text labels and classes are custmizable by using options.

<span class="ui_check_switch">
  <span class="ui_check_switch_inner">
    <span class="ui_check_switch_slider">
      <span class="ui_check_on ui_check_text">ON</span>
      <span class="ui_check_knob"></span>
      <span class="ui_check_off ui_check_text">OFF</span>
    </span>
  </span>
</span>

Options

Custom text label and classes

keydefault
checkOnText'ON'
checkOffText'OFF'
uiClass'ui_check_switch'
uiOnClass'ui_check_switch_on'
uiOffClass'ui_check_switch_off'
uiInnerClass'ui_check_switch_inner'
uiSliderClass'ui_check_switch_slider'
uiTextClass'ui_check_text'
uiKnobClass'ui_check_knob'
uiCheckOffClass'ui_check_off'
uiCheckOnClass 'ui_check_on

source code

CheckSwitch('#checkbox', {
  checkOnText: 'ON',
  checkOffText: 'OFF'
});

APIs

methoddescription
onmake the checkSwitch state on
offmake the checkSwitch state off
getStateget checked or not from the checkbox
bindbind events to the checkSwitch
destroydestroy all events and APIs from the checkSwitch

source code

var chk = CheckSwitch('#checkbox');
chk.on();

Events

eventsdescription
checkSwitch:onFire when the checkSwitch is on
checkSwitch:offFire when the checkSwitch is off

source code

var chk = CheckSwitch('#checkbox');
chk.bind({
  'checkSwitch:on': function(ev) {
    var cs = ev.checkSwitch;
    console.log(cs.name);
  },
  'checkSwitch:off': function(ev) {
    var cs = ev.checkSwitch;
    console.log(cs.name);
  }
});

Capable browsers

*1) Default styles does not support iOS3 or less, operation check has.

*2) There are ugly border radiuses in default styles.

*3) If you want to use in IEs, you would write new styles.

About