iPhone Flip Switch
Demo
Are you interested in HTML5 Web Components? Check X-Switch - a similar toggle switch experiment, wrapped as a custom tag.
Checkboxes
Toggle Buttons
A Few Disabled Buttons
Radios
Mutually Exclusive Buttons
A Few Disabled Mutually Exclusive Buttons
Localized "ON" and "OFF" States
Register the Class
<script src="AcidJs.ToggleSwitch/lib/jQuery/jquery-1.10.1.min.js"></script> <script src="AcidJs.ToggleSwitch/classes/ToggleSwitch.js"></script>
Usage and Code Insight
(function() { window.switcher = new AcidJs.ToggleSwitch({ // (String) [optional] path to the AcidJs.ToggleSwitch/ folder; default: "" appRoot: "pages/javascript-iphone-toggle-buttons/example/" }); window.switcher.render({ type: "checkbox", name: "switcher-01", defaultCheckedNode: [1, 2], boundingBox: $("#switcher-01"), items: [{ value: "en-US", label: "English" },{ value: "de-DE", label: "German" },{ value: "ru-RU", label: "Russian" },{ value: "es-ES", label: "Spanish" }] }); // all properties are set here; check documentation in the code for more insight window.switcher.render({ type: "radio", // (String) "checkbox"|"radio" [optional] default: "checkbox" name: "guitars", // (String) [required] on: "on", // (String) [optional] default: "on" off: "off", // (String) [optional] default: "off" defaultCheckedNode: 0, // (Number for type "radio"|Array for type "checkbox") [optional] default: 0 cssClasses: ["class-a", "class-b", "class-c"], // (Array) [optional] boundingBox: $("#switcher-02"), // (JQueryDomNode) [required] items: [{ value: "B.C. Rich", // (String) optional cssClasses: ["class-d", "class-e", "class-f"], // (Array) optional, label: "B.C. Rich Guitars" // (String) optional },{ value: "ESP", cssClasses: ["class-g", "class-h", "class-i"], label: "ESP Guitars" },{ value: "Fender", cssClasses: ["class-j", "class-k", "class-l"], label: "Ibanez Guitars" },{ value: "Gibson", cssClasses: ["class-m", "class-n", "class-o"], label: "Gibson Guitars" },{ value: "Jackson", cssClasses: ["class-p", "class-q", "class-r"], label: "Jackson Guitars" }] }); window.switcher.render({ type: "checkbox", name: "switcher-03", defaultCheckedNode: [0, 1, 2], boundingBox: $("#switcher-03"), items: [{ value: "HTML5", label: "HTML5" },{ value: "CSS3", label: "CSS3" },{ value: "JavaScript", label: "JavaScript" },{ value: "JSON", label: "JSON" },{ value: "XML", label: "XML" },{ value: "PHP", label: "PHP", enabled: false },{ value: ".NET", label: ".NET", enabled: false }] }); window.switcher.render({ type: "radio", name: "switcher-04", boundingBox: $("#switcher-04"), items: [{ value: "John Steinbeck", label: "John Steinbeck" },{ value: "George Orwell", label: "George Orwell" },{ value: "Clive Barker", label: "Clive Barker", enabled: false },{ value: "Stephen King", label: "Stephen King", enabled: false },{ value: "Anthony Burgess", label: "Anthony Burgess" }] }); window.switcher.render({ type: "checkbox", name: "switcher-05", on: "ein", off: "aus", defaultCheckedNode: [0, 2], boundingBox: $("#switcher-05"), items: [{ value: "en-US", label: "English" },{ value: "de-DE", label: "German" },{ value: "ru-RU", label: "Russian" },{ value: "es-ES", label: "Spanish" }] }); // event bindings $("#switcher-01").on("acidjs-toggle-switch", function(e, data) { window.console.log(e.type, data); }); $("#switcher-02").on("acidjs-toggle-switch", function(e, data) { window.console.log(e.type, data); }); $("#switcher-03").on("acidjs-toggle-switch", function(e, data) { window.console.log(e.type, data); }); $("#switcher-04").on("acidjs-toggle-switch", function(e, data) { window.console.log(e.type, data); }); $("#switcher-05").on("acidjs-toggle-switch", function(e, data) { window.console.log(e.type, data); }); // test the destroy method //window.switcher.destroy($("#switcher-01")); //window.switcher.destroy($("#switcher-02")); //window.switcher.destroy($("#switcher-03")); //window.switcher.destroy($("#switcher-04")); //window.switcher.destroy($("#switcher-05")); })();
/* * @namespace window.AcidJs * @class ToggleSwitch * @javascript * @author Martin Ivanov * @web http://wemakesites.net **/ (function() { "use strict"; /* * @namespace window.AcidJs **/ if(undefined === window.AcidJs) { window.AcidJs = {}; } /* * @class ToggleSwitch * @constructor * @config (Object) * { * appRoot: (String) [optional] path to the AcidJs.ToggleSwitch/ folder; default: "" * } * @return Object **/ function ToggleSwitch(config) { config = config || {}; var appRoot = config.appRoot || ""; this._stylesheet(appRoot + this.URLS.stylesheet); } ToggleSwitch.prototype = { /* * @member MANIFEST * @public * @desc developer information and credits **/ MANIFEST: { version: "1.0", name: "ToggleSwitch", developer: "Martin Ivanov", websites: { page: "http://experiments.wemakesites.net/javascript-iphone-toggle-buttons.html", personal: "http://wemakesites.net", portfolio: "http://acidjs.wemakesites.net", blog: "http://acidmartin.wordpress.com/", rss: "http://feeds.feedburner.com/acidmartin", twitter: "https://twitter.com/#!/wemakesitesnet" }, email: "acid_martin@yahoo.com" }, /* * @member URLS * @public * @desc URLS used in the class **/ URLS: { stylesheet: "AcidJs.ToggleSwitch/styles/ToggleSwitch.css" }, /* * @member EVENTS * @public * @desc custom events **/ EVENTS: [ "acidjs-toggle-switch" ], /* * @member TEMPLATES * @public * @desc HTML templates used in the class **/ TEMPLATES: { item: ['
/* * @namespace window.AcidJs * @class ToggleSwitch * @stylesheet * @author Martin Ivanov * @web http://wemakesites.net **/ .acidjs-toggle-switch, .acidjs-toggle-switch *, .acidjs-toggle-switch *::before, .acidjs-toggle-switch *::after { -webkit-box-sizing: content-box; -moz-box-sizing: content-box; box-sizing: content-box; } :root .acidjs-toggle-switch input, :root .acidjs-toggle-switch b, :root .acidjs-toggle-switch span::before, :root .acidjs-toggle-switch span::after { position: absolute; } :root .acidjs-toggle-switch b, :root .acidjs-toggle-switch span::before, :root .acidjs-toggle-switch span::after { width: 41px; height: 30px; } :root .acidjs-toggle-switch span::before, :root .acidjs-toggle-switch span::after { text-align: center; } :root .acidjs-toggle-switch span::before, :root .acidjs-toggle-switch span::after { color: #8c8c8c; text-transform: uppercase; -webkit-pointer-events: none; -moz-pointer-events: none; pointer-events: none; } :root .acidjs-toggle-switch * { display: inline-block; padding: 0; margin: 0; border: 0; vertical-align: middle; outline: none; font-weight: normal; font-style: normal; font-variant: normal; -webkit-user-select: none; -moz-user-select: none; user-select: none; } :root .acidjs-toggle-switch li { display: block; margin: 4px; } :root .acidjs-toggle-switch { font: bold 16px/30px Arial, Helvetica, Sans-serif; } :root .acidjs-toggle-switch span { position: relative; width: 82px; height: 30px; } :root .acidjs-toggle-switch strong { margin: 0 0 0 4px; font-size: 12px; color: #666; } :root .acidjs-toggle-switch strong:empty { display: none; } :root .acidjs-toggle-switch input:checked ~ strong { color: #333; } :root .acidjs-toggle-switch label { cursor: pointer; } :root .acidjs-toggle-switch input { opacity: 0; } :root .acidjs-toggle-switch b { z-index: 5; border-radius: 2px; box-shadow: 1px 0 1px rgba(0, 0, 0, .25), 1px 0 1px rgba(255, 255, 255, .75) inset; background: #f9f9f9; background: -webkit-linear-gradient(#cfcfcf, #f9f9f9); background: -moz-linear-gradient(#cfcfcf, #f9f9f9); background: -ms-linear-gradient(#cfcfcf, #f9f9f9); background: -o-linear-gradient(#cfcfcf, #f9f9f9); background: linear-gradient(#cfcfcf, #f9f9f9); -moz-transition: all 250ms cubic-bezier(.09, .11, .24, .91); -webkit-transition: all 250ms cubic-bezier(.09, .11, .24, .91); -ms-transition: all 250ms cubic-bezier(.09, .11, .24, .91); -o-transition: all 250ms cubic-bezier(.09, .11, .24, .91); transition: all 250ms cubic-bezier(.09, .11, .24, .91); } :root .acidjs-toggle-switch input:checked ~ span b { -webkit-transform: translatex(100%); -moz-transform: translatex(100%); -ms-transform: translatex(100%); -o-transform: translatex(100%); transform: translatex(100%); } :root .acidjs-toggle-switch span { border: solid 1px #bcbbbb; border-radius: 3px; box-shadow: 0 0 2px #ccc; background: #f3f3f3; background: -webkit-linear-gradient(#c8c8c8, #f3f3f3); background: -moz-linear-gradient(#c8c8c8, #f3f3f3); background: -ms-linear-gradient(#c8c8c8, #f3f3f3); background: -o-linear-gradient(#c8c8c8, #f3f3f3); background: linear-gradient(#c8c8c8, #f3f3f3); } :root .acidjs-toggle-switch span::before { left: 0; content: attr(data-on); color: #fff; text-shadow: 0 -1px #0d2046; } :root .acidjs-toggle-switch span::after { right: 0; content: attr(data-off); } :root .acidjs-toggle-switch input:checked ~ span { border-color: #0f2a4f; background: #5d96ea; background: -webkit-linear-gradient(#1b45bd, #5d96ea); background: -moz-linear-gradient(#1b45bd, #5d96ea); background: -ms-linear-gradient(#1b45bd, #5d96ea); background: -o-linear-gradient(#1b45bd, #5d96ea); background: linear-gradient(#1b45bd, #5d96ea); } :root .acidjs-toggle-switch input:disabled ~ * { opacity: .5; cursor: no-drop; } .acidjs-toggle-switch { display: block !important; }