{"id":13601790,"url":"https://github.com/abpetkov/switchery","last_synced_at":"2025-10-11T02:20:05.164Z","repository":{"id":12098318,"uuid":"14686919","full_name":"abpetkov/switchery","owner":"abpetkov","description":"iOS 7 style switches for your checkboxes","archived":false,"fork":false,"pushed_at":"2021-07-12T06:20:26.000Z","size":1821,"stargazers_count":2048,"open_issues_count":78,"forks_count":477,"subscribers_count":66,"default_branch":"master","last_synced_at":"2025-03-27T08:01:54.971Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://abpetkov.github.io/switchery/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/abpetkov.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-11-25T13:57:40.000Z","updated_at":"2025-03-15T20:58:52.000Z","dependencies_parsed_at":"2022-09-06T23:31:16.507Z","dependency_job_id":null,"html_url":"https://github.com/abpetkov/switchery","commit_stats":null,"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abpetkov%2Fswitchery","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abpetkov%2Fswitchery/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abpetkov%2Fswitchery/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abpetkov%2Fswitchery/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/abpetkov","download_url":"https://codeload.github.com/abpetkov/switchery/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246140563,"owners_count":20729802,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T18:01:08.011Z","updated_at":"2025-10-11T02:20:05.073Z","avatar_url":"https://github.com/abpetkov.png","language":"JavaScript","readme":"![Switchery](http://i.imgur.com/xJAM3Jq.png)\n\n## Description\n\nSwitchery 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.\n\nSupported by all modern browsers: Chrome, Firefox, Opera, Safari, IE8+\n\n![Preview](http://i.imgur.com/0PcuTbO.jpg)\n\n[Live Preview](http://abpetkov.github.io/switchery/)\n\n## Installation\n\n##### Standalone:\n\n```html\n\u003clink rel=\"stylesheet\" href=\"dist/switchery.css\" /\u003e\n\u003cscript src=\"dist/switchery.js\"\u003e\u003c/script\u003e\n```\n\n##### Component:\n\n```shell\n$ component install abpetkov/switchery\n```\n\n##### Bower:\n\n```shell\n$ bower install switchery\n```\n\n##### Rails\n\nTo use Switchery in your rails app, add this to your Gemfile:\n\n```rails\ngem 'switchery-rails'\n```\n\nOr go to [Switchery Rails gem page](https://rubygems.org/gems/switchery-rails) for more info, documentation and instructions.\n\n##### Angular JS\n\nFor thorough installation and usage instructions on how to use Switchery with Angular JS, check out this repo: [servergrove/NgSwitchery](https://github.com/servergrove/NgSwitchery)\n\n##### Meteor\n\nYou can install Switchery to your Meteor.js app via:\n\n```shell\n$ meteor add abpetkov:switchery\n```\n\n[Switchery on Atmosphere](https://atmospherejs.com/abpetkov/switchery)\n\n## Usage\n\n```js\nvar elem = document.querySelector('.js-switch');\nvar init = new Switchery(elem);\n```\n\nUse the above for the standalone version.\n\n## Settings and Defaults\n\n```js\ndefaults = {\n    color             : '#64bd63'\n  , secondaryColor    : '#dfdfdf'\n  , jackColor         : '#fff'\n  , jackSecondaryColor: null\n  , className         : 'switchery'\n  , disabled          : false\n  , disabledOpacity   : 0.5\n  , speed             : '0.4s'\n  , size              : 'default'\n};\n```\n\n- `color` : color of the switch element (HEX or RGB value)\n- `secondaryColor` : secondary color for background color and border, when the switch is off\n- `jackColor` : default color of the jack/handle element\n- `jackSecondaryColor` : color of unchecked jack/handle element\n- `className` : class name for the switch element (by default styled in switchery.css)\n- `disabled` : enable or disable click events and changing the state of the switch (boolean value)\n- `disabledOpacity` : opacity of the switch when it's disabled (0 to 1)\n- `speed` : length of time that the transition will take, ex. '0.4s', '1s', '2.2s' (Note: transition speed of the handle is twice shorter)\n- `size` : size of the switch element (small or large)\n\n## API\n\n##### .destroy()\n\nUnbinding all event handlers attached to the switch element to prepare the object for garbage collection.\n\n##### .enable()\n\nEnable disabled switch by re-adding event handlers and changing the opacity to 1.\n\n##### .disable()\n\nDisable switch by unbinding attached events and changing opacity to `disabledOpacity` value.\n\n##### .isDisabled()\n\nCheck 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`.\n\n## Examples\n\n##### Checked\n\nOnly thing you need is to add a `checked` attribute to your checkbox input. Simple as that.\n\n```html\n\u003cinput type=\"checkbox\" class=\"js-switch\" checked /\u003e\n```\n\n##### Multiple switches\n\nYou 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.\n\n```js\nvar elems = Array.prototype.slice.call(document.querySelectorAll('.js-switch'));\n\nelems.forEach(function(html) {\n  var switchery = new Switchery(html);\n});\n```\n\n![Multiple](http://i.imgur.com/Ip4xy4s.jpg)\n\n##### Multiple calls\n\nYou can filter out existing elements that have already been called by looking for `data-switchery=\"true\"`.\n\n##### Disabled\n\nUse the `disabled` option to make your switch active or inactive.\n\n```js\nvar switchery = new Switchery(elem, { disabled: true });\n```\n\nCustomize the default opacity of the disabled switch, using the `disabledOpacity` option.\n\n```js\nvar switchery = new Switchery(elem, { disabled: true, disabledOpacity: 0.75 });\n```\n\nAdding `disabled` or `readonly` attribute to the native input element will result in the switch being disabled as well.\n\n##### Colored\n\nYou 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.\n\n```js\nvar switchery = new Switchery(elem, { color: '#7c8bc7', jackColor: '#9decff' });\n```\n\n![JackColor](http://i.imgur.com/7ztX29e.png)\n\nor\n\n```js\nvar switchery = new Switchery(elem, { color: '#faab43', secondaryColor: '#fC73d0', jackColor: '#fcf45e', jackSecondaryColor: '#c8ff77' });\n```\n\n![JackSecondaryColor](http://i.imgur.com/KS0H8ac.png)\n\nAny other changes regarding colors you want to make, should take place in `switchery.css`.\n\n##### Sizes\n\nSince 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.\n\nNot using this property will render the default sized switch element.\n\n```js\nvar switchery = new Switchery(elem, { size: 'small' });\n// ... or\nvar switchery = new Switchery(elem, { size: 'large' });\n```\n\n![SwitchSizes](http://i.imgur.com/TVlgvx7.png)\n\n##### Checking state\n\nIn 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.\n\nOn click:\n\n```js\nvar clickCheckbox = document.querySelector('.js-check-click')\n  , clickButton = document.querySelector('.js-check-click-button');\n\nclickButton.addEventListener('click', function() {\n  alert(clickCheckbox.checked);\n});\n```\n\nOn change:\n\n```js\nvar changeCheckbox = document.querySelector('.js-check-change');\n\nchangeCheckbox.onchange = function() {\n  alert(changeCheckbox.checked);\n};\n```\n\n##### Legacy browsers\n\nIf 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.\n\n```js\nvar elems = document.querySelectorAll('.js-switch');\n\nfor (var i = 0; i \u003c elems.length; i++) {\n  var switchery = new Switchery(elems[i]);\n}\n```\n\nPersonally I recommend using [CSS3 PIE](http://css3pie.com/). For working example you can check out the demo page.\n\n## Development\n\nIf you've decided to go in development mode and tweak all of this a bit, there are few things you should do.\n\nAfter you clone the repository, do this in your terminal ([NPM](http://npmjs.org/) required):\n\n```shell\n$ npm install\n```\n\nAdd the following code before the rest:\n\n```js\nvar Switchery = require('switchery');\n```\n\nMake sure you're using the `build/build.js` and `build/build.css` files and you're ready.\n\nThere are some useful commands you can use.\n\n`$ make install` - will install Node.js modules, components etc.\n\n`$ make build` - will create a build file\n\n`$ make standalone` - will create a standalone and minified files\n\n## Credits\n\nBig thanks to:\n\n- [Veselin Todorov](https://github.com/vesln)\n\n## Contact\n\nIf you like this component, share your appreciation by following me in [Twitter](https://twitter.com/abpetkov), [GitHub](https://github.com/abpetkov) or [Dribbble](http://dribbble.com/apetkov).\n\n## License\n\nThe MIT License (MIT)\n\nCopyright (c) 2013-2015 Alexander Petkov\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of\nthis software and associated documentation files (the \"Software\"), to deal in\nthe Software without restriction, including without limitation the rights to\nuse, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\nthe Software, and to permit persons to whom the Software is furnished to do so,\nsubject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\nFOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\nCOPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\nIN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\nCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","funding_links":[],"categories":["Development","10. 表单处理","Web 前端","Frameworks / Resources"],"sub_categories":["Programming","10.3 单选框/复选框相关 ###","10.4 单选框/复选框相关","S"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fabpetkov%2Fswitchery","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fabpetkov%2Fswitchery","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fabpetkov%2Fswitchery/lists"}