├── jquery.switchButton.css ├── main.css ├── README.md ├── index.html └── jquery.switchButton.js /jquery.switchButton.css: -------------------------------------------------------------------------------- 1 | .switch-button-label { 2 | float: left; 3 | 4 | font-size: 10pt; 5 | cursor: pointer; 6 | } 7 | 8 | .switch-button-label.off { 9 | color: #adadad; 10 | } 11 | 12 | .switch-button-label.on { 13 | color: #0088CC; 14 | } 15 | 16 | .switch-button-background { 17 | float: left; 18 | position: relative; 19 | 20 | background: #ccc; 21 | border: 1px solid #aaa; 22 | 23 | margin: 1px 10px; 24 | 25 | -webkit-border-radius: 4px; 26 | -moz-border-radius: 4px; 27 | border-radius: 4px; 28 | 29 | cursor: pointer; 30 | } 31 | 32 | .switch-button-button { 33 | position: absolute; 34 | 35 | left: -1px; 36 | top : -1px; 37 | 38 | background: #FAFAFA; 39 | border: 1px solid #aaa; 40 | 41 | -webkit-border-radius: 4px; 42 | -moz-border-radius: 4px; 43 | border-radius: 4px; 44 | } 45 | -------------------------------------------------------------------------------- /main.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | padding: 0; 4 | 5 | font-family: "Helvetica Neue", "Helvetica", "Arial", sans-serif; 6 | font-size: 14px; 7 | color: #333; 8 | background-color: #EFEFEF; 9 | } 10 | 11 | .wrapper { 12 | position: relative; 13 | width: 800px; 14 | margin: auto; 15 | padding: 20px 40px; 16 | background: white; 17 | } 18 | 19 | p { 20 | text-align: justify; 21 | max-width: 700px; 22 | } 23 | 24 | h2 { 25 | padding-bottom: 10px; 26 | border-bottom: 1px solid #ccc; 27 | margin-top: 60px; 28 | } 29 | 30 | h3 { 31 | margin-top: 40px; 32 | } 33 | 34 | code, pre { 35 | margin: 0 2px; 36 | padding: 0px 5px; 37 | border: 1px solid #EAEAEA; 38 | background-color: #F8F8F8; 39 | border-radius: 3px; 40 | font-size: 12px; 41 | font-family: Consolas, "Liberation Mono", Courier, monospace; 42 | } 43 | 44 | pre { 45 | max-width: 600px; 46 | padding: 10px; 47 | margin: 20px 0; 48 | } 49 | 50 | .demo::before{ 51 | content: "result"; 52 | display: block; 53 | margin-bottom: 10px; 54 | text-transform: uppercase; 55 | font-size: 10px; 56 | letter-spacing: 1px; 57 | color: #CCC; 58 | border-bottom: 1px solid #DDD; 59 | width: 600px; 60 | padding: 0 20px 5px 0; 61 | } 62 | 63 | .demo { 64 | padding: 20px 0; 65 | } 66 | 67 | .switch-wrapper { 68 | display: inline-block; 69 | position: relative; 70 | top: 3px; 71 | } 72 | 73 | 74 | .slider.demo .switch-button-label { 75 | font-size: 40px; 76 | } 77 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | jQuery-switchButton 2 | =================== 3 | 4 | jQuery iPhone-like switch button meant to be used on a ``````. 5 | 6 | This widget will replace the receiver element with an iPhone-style switch button with two states: "on" and "off". Labels 7 | of the states are customizable, as are their presence and position. The receiver element's "checked" attribute is updated 8 | according to the state of the switch, so that it can be used in a ```
30 | 31 | You can transform this checkbox to a nice-looking switch button by calling ```switchButton()``` on it: 32 | 33 | options = { /* see below */ }; 34 | $("input#great").switchButton(options); 35 | 36 | By default, this will display a button with "ON" and "OFF" labels on each side of the switch. You can control this and other 37 | parameters at initialization or by calling ```switchButton("option", "optionName", value)```. 38 | Here are the available options: 39 | 40 | checked: undefined // State of the switch 41 | 42 | show_labels: true // Should we show the on and off labels? 43 | labels_placement: "both" // Position of the labels: "both", "left" or "right" 44 | on_label: "ON" // Text to be displayed when checked 45 | off_label: "OFF" // Text to be displayed when unchecked 46 | 47 | width: 25 // Width of the button in pixels 48 | height: 11 // Height of the button in pixels 49 | button_width: 12 // Width of the sliding part in pixels 50 | 51 | clear: true // Should we insert a div with style="clear: both;" after the switch button? 52 | clear_after: null // Override the element after which the clearing div should be inserted (null > right after the button) 53 | 54 | 55 | Styling 56 | ------- 57 | 58 | The button and labels are styled with a few lines of CSS in ```jquery.switchButton.css```. 59 | Have a look at this file and fiddle with it to change the look of you switch button! 60 | 61 | Wordpress users 62 | --------------- 63 | 64 | Have a look at [this answer](http://wordpress.stackexchange.com/questions/108257/how-to-load-jquery-easing-script-in-wordpress/108267#108267?newreg=7700a444aabf4aadbd1819e794d1d6c4) on StackExchange to include jQuery easing functions in order to make this plugin work in Wordpress. 65 | 66 | 67 | License 68 | ------- 69 | 70 | Copyright (c) Olivier Lance - Released under MIT License: 71 | 72 | > Permission is hereby granted, free of charge, to any person 73 | > obtaining a copy of this software and associated documentation 74 | > files (the "Software"), to deal in the Software without 75 | > restriction, including without limitation the rights to use, 76 | > copy, modify, merge, publish, distribute, sublicense, and/or sell 77 | > copies of the Software, and to permit persons to whom the 78 | > Software is furnished to do so, subject to the following 79 | > conditions: 80 | > 81 | > The above copyright notice and this permission notice shall be 82 | > included in all copies or substantial portions of the Software. 83 | > 84 | > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 85 | > EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 86 | > OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 87 | > NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 88 | > HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 89 | > WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 90 | > FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 91 | > OTHER DEALINGS IN THE SOFTWARE. 92 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
12 |
13 |
16 | This widget will replace the receiver element with an iPhone-style switch
17 | button with two states: "on" and "off". Labels of the states are customizable,
18 | as are their presence and position. The receiver element's "checked" attribute
19 | is updated according to the state of the switch, so that it can be used in
20 | a <form>, on a checkbox input for instance.
21 |
24 | This widget has been made to answer my specific needs, so there's room for 25 | improvement here and there, but it does the job for me as it is. 26 |
27 | 28 | 29 |32 | Using the following markup: 33 |
34 | 35 |36 | <input type="checkbox" value="1" checked>37 | 38 |
39 | You can simply call switchButton() without any
40 | options on the checkbox element, and you'll get that:
41 |
48 | The widget's elements are floated, so you must wrap the checkbox in its own
49 | <div> if you want to inline it with some
50 | text:
51 |
54 | It is 55 | <div class="switch-wrapper"> 56 | <input type="checkbox" value="1" checked> 57 | </div> 58 | !59 | 60 |
61 | Apply some styles to the wrapping <div> and
62 | everything will look perfect:
63 |
66 | .switch-wrapper {
67 | display: inline-block;
68 | position: relative;
69 | top: 3px;
70 | }
71 |
72 |
73 |
86 | The first thing you might want to do, is changing the labels texts. The
87 | on_label and off_label
88 | options allow you to do that:
89 |
92 | $("input[type=checkbox]").switchButton({
93 | on_label: 'yes',
94 | off_label: 'no'
95 | });
96 |
97 |
107 | The widget will use the checked attribute of the
108 | receiver to determine the initial state of the switch.
109 | However, you can force it to be on or off by specifying it in the options:
110 |
113 | // Force the checked property to false, whatever state it is in initially
114 | $("input[type=checkbox]").switchButton({
115 | checked: false
116 | });
117 |
118 | 125 | Maybe you'd like to display the button without any label at all: 126 |
127 | 128 |
129 | $("input[type=checkbox]").switchButton({
130 | show_labels: false
131 | });
132 |
133 | 138 | Or display them on one side only: 139 |
140 | 141 |
142 | $("input[type=checkbox]").switchButton({
143 | labels_placement: "right"
144 | });
145 |
146 |
151 | $("input[type=checkbox]").switchButton({
152 | labels_placement: "left"
153 | });
154 |
155 | 162 | You can customize the size of the slider and the sliding element: 163 |
164 | 165 |
166 | $("input[type=checkbox]").switchButton({
167 | width: 100,
168 | height: 40,
169 | button_width: 50
170 | });
171 |
172 |
175 |
176 |
177 | $("input[type=checkbox]").switchButton({
178 | width: 100,
179 | height: 40,
180 | button_width: 70
181 | });
182 |
183 |
186 |
187 |
188 | Note that the font-size of the labels for both
189 | examples above have been changed in the CSS.
190 |