├── README.md ├── index.html ├── jquery.switchButton.css ├── jquery.switchButton.js └── main.css /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 |
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 |