项目作者: jamiebicknell

项目描述 :
jQuery plugin for pure image, checkbox based, toggle switches
高级语言: HTML
项目地址: git://github.com/jamiebicknell/Toggle-Switch.git
创建时间: 2012-03-14T10:28:01Z
项目社区:https://github.com/jamiebicknell/Toggle-Switch

开源协议:MIT License

下载


Toggle Switch

This is a pure image based toggle switch powered by jQuery, using zero CSS3 properties and as a result it looks and functions the same in all browsers.

It utilises a semi transparent PNG overlay to mask the underneath switch image, so the toggling animation remains within the constraints of the rounded rectangle.

View the working example

Features

  • Compatible in most browsers, including IE6+
  • Retina images for supporting iDevices
  • Uses semi-transparent PNG overlay, so will look the same in all browsers
  • PNG Overlay will need to be updated for use on different backgrounds (PSD Included for both regular and retina images)
  • Switch animates on click of the switch itself or the accompanying <label>

Example Usage

Plugin will automatically generate HTML required, and gracefully fall back to a plain old checkbox if JavaScript is disabled. Downside is that there may be a slight flicker between seeing the checkbox and the toggle switch appearing.

  1. <link href='./toggleswitch.css' rel='stylesheet' type='text/css' />
  2. <!--[if lt IE 8]>
  3. <link href='./toggleswitch.ie.css' rel='stylesheet' type='text/css' />
  4. <![endif]-->
  5. <!-- Switch is ON -->
  6. <label for='opt1'>Option 1</label>
  7. <input type='checkbox' name='opt1' id='opt1' value='1' class='toggleswitch' checked='checked' />
  8. <!-- Switch is OFF -->
  9. <label for='opt2'>Option 2</label>
  10. <input type='checkbox' name='opt2' id='opt2' value='1' class='toggleswitch' />
  11. <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js'></script>
  12. <script type='text/javascript' src='./jquery.toggleswitch.js'></script>
  13. <script type='text/javascript'>
  14. jQuery(document).ready(function ($) {
  15. $('.toggleswitch').toggleSwitch();
  16. });
  17. </script>

Alternative HTML Usage

No flicker between the document loading and toggle switch appearing, but there is no graceful fall back if JavaScript is disabled.

  1. <link href='./toggleswitch.css' rel='stylesheet' type='text/css' />
  2. <!--[if lt IE 8]>
  3. <link href='./toggleswitch.ie.css' rel='stylesheet' type='text/css' />
  4. <![endif]-->
  5. <!-- Switch is ON -->
  6. <label for='opt1'>Option 1</label>
  7. <div class='switch'>
  8. <div class='overlay'></div>
  9. <span class='switched'></span>
  10. <input type='checkbox' name='opt1' id='opt1' value='1' class='toggleswitch' checked='checked' />
  11. </div>
  12. <!-- Switch is OFF -->
  13. <label for='opt2'>Option 2</label>
  14. <div class='switch'>
  15. <div class='overlay'></div>
  16. <span class='switched off'></span>
  17. <input type='checkbox' name='opt2' id='opt2' value='1' class='toggleswitch' />
  18. </div>
  19. <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js'></script>
  20. <script type='text/javascript' src='./jquery.toggleswitch.js'></script>
  21. <script type='text/javascript'>
  22. jQuery(document).ready(function ($) {
  23. $('.toggleswitch').toggleSwitch();
  24. });
  25. </script>

Options

You can create functions for onClick, onChangeOn, and onChangeOff.

  1. <script type='text/javascript'>
  2. jQuery(document).ready(function ($) {
  3. $('.toggleswitch').toggleSwitch({
  4. onClick: function () {
  5. console.log('Toggle Switch was clicked');
  6. },
  7. onChangeOn: function () {
  8. console.log('Toggle Switch was changed to the ON position');
  9. },
  10. onChangeOff: function () {
  11. console.log('Toggle Switch was changed to the OFF position');
  12. }
  13. });
  14. });
  15. </script>

License

Toggle Switch is licensed under the MIT license, see LICENSE.md for details.