PHPackages                             gaomingcode/switchery - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. gaomingcode/switchery

ActiveLibrary

gaomingcode/switchery
=====================

iOS 7 style switches for your checkboxes.

0.8.2(4y ago)01.9k—5.3%MITJavaScript

Since Jun 3Pushed 4y agoCompare

[ Source](https://github.com/gaomingcode/switchery)[ Packagist](https://packagist.org/packages/gaomingcode/switchery)[ Docs](http://abpetkov.github.io/switchery/)[ RSS](/packages/gaomingcode-switchery/feed)WikiDiscussions master Synced today

READMEChangelog (1)DependenciesVersions (2)Used By (0)

Switchery
=========

[](#switchery)

[![GitHub Version](https://camo.githubusercontent.com/479dbc475d3327f42091ffea5888585687bdce13594df57197068adaf348bb22/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f67616f6d696e67636f64652f7377697463686572792e737667)](https://github.com/gaomingcode/switchery)[![Packagist Downloads](https://camo.githubusercontent.com/e693d21da93d16d2f13d036e5a33de96c3a6e5e2216876f0f367eca2ce94e36b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f67616f6d696e67636f64652f737769746368657279)](https://github.com/gaomingcode/switchery)[![Github License](https://camo.githubusercontent.com/1773f107e6c7891fe20fe790908fccdf62036e3327ffd03a93dbd3570f0196f0/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f67616f6d696e67636f64652f737769746368657279)](https://github.com/gaomingcode/switchery)

Installation
------------

[](#installation)

### Composer

[](#composer)

```
composer require gaomingcode/switchery

```

ReadMe from Origin
------------------

[](#readme-from-origin)

[![Switchery](https://camo.githubusercontent.com/9f64d1a8f8c210117436656d7b53b6298b599f605de97eae4ed4dba029bfe7f1/687474703a2f2f692e696d6775722e636f6d2f784a414d334a712e706e67)](https://camo.githubusercontent.com/9f64d1a8f8c210117436656d7b53b6298b599f605de97eae4ed4dba029bfe7f1/687474703a2f2f692e696d6775722e636f6d2f784a414d334a712e706e67)

Description
-----------

[](#description)

Switchery is a simple component that helps you turn your default HTML checkbox inputs into beautiful iOS 7 style switches in just few simple steps. You can easily customize switches, so that they match your design perfectly.

Supported by all modern browsers: Chrome, Firefox, Opera, Safari, IE8+

[![Preview](https://camo.githubusercontent.com/18464aa13b183c683ec9b000a5517e53dccbafa66e39d049e8a8fda59e627a1d/687474703a2f2f692e696d6775722e636f6d2f3050637554624f2e6a7067)](https://camo.githubusercontent.com/18464aa13b183c683ec9b000a5517e53dccbafa66e39d049e8a8fda59e627a1d/687474703a2f2f692e696d6775722e636f6d2f3050637554624f2e6a7067)

[Live Preview](http://abpetkov.github.io/switchery/)

Installation
------------

[](#installation-1)

##### Standalone:

[](#standalone)

```

```

##### Component:

[](#component)

```
$ component install abpetkov/switchery
```

##### Bower:

[](#bower)

```
$ bower install switchery
```

##### Rails

[](#rails)

To use Switchery in your rails app, add this to your Gemfile:

```
gem 'switchery-rails'

```

Or go to [Switchery Rails gem page](https://rubygems.org/gems/switchery-rails) for more info, documentation and instructions.

##### Angular JS

[](#angular-js)

For thorough installation and usage instructions on how to use Switchery with Angular JS, check out this repo: [servergrove/NgSwitchery](https://github.com/servergrove/NgSwitchery)

##### Meteor

[](#meteor)

You can install Switchery to your Meteor.js app via:

```
$ meteor add abpetkov:switchery
```

[Switchery on Atmosphere](https://atmospherejs.com/abpetkov/switchery)

Usage
-----

[](#usage)

```
var elem = document.querySelector('.js-switch');
var init = new Switchery(elem);
```

Use the above for the standalone version.

Settings and Defaults
---------------------

[](#settings-and-defaults)

```
defaults = {
    color             : '#64bd63'
  , secondaryColor    : '#dfdfdf'
  , jackColor         : '#fff'
  , jackSecondaryColor: null
  , className         : 'switchery'
  , disabled          : false
  , disabledOpacity   : 0.5
  , speed             : '0.4s'
  , size              : 'default'
};
```

- `color` : color of the switch element (HEX or RGB value)
- `secondaryColor` : secondary color for background color and border, when the switch is off
- `jackColor` : default color of the jack/handle element
- `jackSecondaryColor` : color of unchecked jack/handle element
- `className` : class name for the switch element (by default styled in switchery.css)
- `disabled` : enable or disable click events and changing the state of the switch (boolean value)
- `disabledOpacity` : opacity of the switch when it's disabled (0 to 1)
- `speed` : length of time that the transition will take, ex. '0.4s', '1s', '2.2s' (Note: transition speed of the handle is twice shorter)
- `size` : size of the switch element (small or large)

API
---

[](#api)

##### .destroy()

[](#destroy)

Unbinding all event handlers attached to the switch element to prepare the object for garbage collection.

##### .enable()

[](#enable)

Enable disabled switch by re-adding event handlers and changing the opacity to 1.

##### .disable()

[](#disable)

Disable switch by unbinding attached events and changing opacity to `disabledOpacity` value.

##### .isDisabled()

[](#isdisabled)

Check if switch is currently disabled by checking the `readonly` and `disabled` attributes on the checkbox and the `disabled` option set via JS. If any of those are present, the returned value is `true`.

Examples
--------

[](#examples)

##### Checked

[](#checked)

Only thing you need is to add a `checked` attribute to your checkbox input. Simple as that.

```

```

##### Multiple switches

[](#multiple-switches)

You can add as many switches as you like, as long as their corresponding checkboxes have the same class. Select them and make new instance of the Switchery class for every of them.

```
var elems = Array.prototype.slice.call(document.querySelectorAll('.js-switch'));

elems.forEach(function(html) {
  var switchery = new Switchery(html);
});
```

[![Multiple](https://camo.githubusercontent.com/15c6502bc78e4aaf26fdc54317640a7fb7e42a376da35ce509f9da30d317064a/687474703a2f2f692e696d6775722e636f6d2f497034787934732e6a7067)](https://camo.githubusercontent.com/15c6502bc78e4aaf26fdc54317640a7fb7e42a376da35ce509f9da30d317064a/687474703a2f2f692e696d6775722e636f6d2f497034787934732e6a7067)

##### Multiple calls

[](#multiple-calls)

You can filter out existing elements that have already been called by looking for `data-switchery="true"`.

##### Disabled

[](#disabled)

Use the `disabled` option to make your switch active or inactive.

```
var switchery = new Switchery(elem, { disabled: true });
```

Customize the default opacity of the disabled switch, using the `disabledOpacity` option.

```
var switchery = new Switchery(elem, { disabled: true, disabledOpacity: 0.75 });
```

Adding `disabled` or `readonly` attribute to the native input element will result in the switch being disabled as well.

##### Colored

[](#colored)

You can change the primary(on) and secondary(off) color of the switch to fit your design perfectly. Accomplish this, changing the `color` and `secondaryColor` options. The jack colors are also customizable via the `jackColor` and the `jackSecondaryColor` options. Below is a good example of what you can accomplish using those.

```
var switchery = new Switchery(elem, { color: '#7c8bc7', jackColor: '#9decff' });
```

[![JackColor](https://camo.githubusercontent.com/ef748e1532826ab879a0ba10d9dc24c9bc46b30e61ad23693e87718e8fcd1b71/687474703a2f2f692e696d6775722e636f6d2f377a74583239652e706e67)](https://camo.githubusercontent.com/ef748e1532826ab879a0ba10d9dc24c9bc46b30e61ad23693e87718e8fcd1b71/687474703a2f2f692e696d6775722e636f6d2f377a74583239652e706e67)

or

```
var switchery = new Switchery(elem, { color: '#faab43', secondaryColor: '#fC73d0', jackColor: '#fcf45e', jackSecondaryColor: '#c8ff77' });
```

[![JackSecondaryColor](https://camo.githubusercontent.com/7499812b2566687609d900d963c95e1ffe90f9ba82664b099a3d76cb71dfe9e3/687474703a2f2f692e696d6775722e636f6d2f4b5330483861632e706e67)](https://camo.githubusercontent.com/7499812b2566687609d900d963c95e1ffe90f9ba82664b099a3d76cb71dfe9e3/687474703a2f2f692e696d6775722e636f6d2f4b5330483861632e706e67)

Any other changes regarding colors you want to make, should take place in `switchery.css`.

##### Sizes

[](#sizes)

Since version 0.7.0 you can change the sizes of the switch element via `size`. Giving it a value of `small` or `large` will result in adding `switchery-small` or `switchery-large` classes respectively, which will change the switch size.

Not using this property will render the default sized switch element.

```
var switchery = new Switchery(elem, { size: 'small' });
// ... or
var switchery = new Switchery(elem, { size: 'large' });
```

[![SwitchSizes](https://camo.githubusercontent.com/4d17566c8feb1cc8d70b1aa6e8310c4e9f69902ae4718cd01c0f0cf21c116069/687474703a2f2f692e696d6775722e636f6d2f54566c677678372e706e67)](https://camo.githubusercontent.com/4d17566c8feb1cc8d70b1aa6e8310c4e9f69902ae4718cd01c0f0cf21c116069/687474703a2f2f692e696d6775722e636f6d2f54566c677678372e706e67)

##### Checking state

[](#checking-state)

In many cases, you'll need to have the current state of the checkbox, checked or not. I'll demostrate how to do this in the two most common situations - getting the state on click and on change.

On click:

```
var clickCheckbox = document.querySelector('.js-check-click')
  , clickButton = document.querySelector('.js-check-click-button');

clickButton.addEventListener('click', function() {
  alert(clickCheckbox.checked);
});
```

On change:

```
var changeCheckbox = document.querySelector('.js-check-change');

changeCheckbox.onchange = function() {
  alert(changeCheckbox.checked);
};
```

##### Legacy browsers

[](#legacy-browsers)

If you are an adventurer and like to support legacy browsers, like IE8 and IE7, apply your favourite fix for rounded corners and box shadows and try a slightly different approach.

```
var elems = document.querySelectorAll('.js-switch');

for (var i = 0; i
