创建逼真 切换/推送/滑动开关 jQuery rsCheckboxSwitch

  • 源码大小:38.87KB
  • 所需积分:1积分
  • 源码编号:19JP-3360
  • 浏览次数:686次
  • 最后更新:2023年06月04日
  • 所属栏目:表单
我要下载
加入收藏
本站默认解压密码:19jp.com 或 19jp_com

简介

rsCheckboxSwitch是一个jQuery插件,可帮助开发人员在网页上创建逼真、动画化和键盘可访问的滑动/切换/推送开关

该插件支持一组不同的HTML标记,包括复选框、按钮和DIV元素。它还包括许多选项,用于自定义switch元素的外观、感觉和行为。

目录:

  • 安装和安装
  • 选项
  • API方法
  • 沉降器和沉降器
  • 自定义样式

如何使用它:

1.在HTML文档中下载并加载rsCheckboxSwitch插件的文件。

  1. <link rel="stylesheet" href="/path/to/rsCheckboxSwitch.css" />
  2. <script src="/path/to/cdn/jquery.slim.min.js"></script>
  3. <script src="/path/to/jquery.rsCheckboxSwitch.js"></script>

2.将一个元素转换为开关。

  1. <!-- Checkbox -->
  2. <input class="demo" type="checkbox" />
  3.  
  4. <!-- Div Button -->
  5. <div class="demo">
  6. Press me
  7. </div>
  8.  
  9. <!-- Button -->
  10. <button class="demo">
  11. Press me
  12. </button>
  1. $('.demo').rsCheckboxSwitch({
  2. // ...
  3. });

3.设置开关样式。默认值:“滑动”。滑动开关只显示一半(ON或OFF),而另一半隐藏。这意味着,开关必须比容器长才能夹住开关。拨动开关或按钮开关的尺寸总是相同的。切换开关不进行滑动,而是保持在相同的位置,并可以设置动画,从“打开”转换为“关闭”(反之亦然)。

  • “滑动”
  • '切换'
  • “推动”
  1. $('.demo').rsCheckboxSwitch({
  2. type: "push",
  3. });

4.所有可能的自定义开关的插件选项。

  1. $('.demo').rsCheckboxSwitch({
  2.  
  3. // slidingType is only meaningful if type is 'sliding'
  4. slidingType: {
  5.  
  6. // Classes for the outer container.
  7. // Add:
  8. // - corners-rounded for full rounded corners;
  9. // - corners-halfrounded for 50% rounded corners;
  10. // - corners-quarterrounded for 25% rounded corners;
  11. // - corners-sharp for no rounded corners;
  12. // If none of the above three are specified, corners-rounded is used.
  13. outerClass: 'checkboxswitch-outer sliding corners-rounded',
  14.  
  15. // Classes for the element that slides inside the outer element.
  16. sliderClass: 'switch',
  17.  
  18. // Classes for the handle usually located in the middle of the slider element.
  19. handleClass: 'handleflat',
  20.  
  21. // Classes for the handle when mouse is applied to it. Type: string.
  22. pushdownClass: 'down',
  23.  
  24. // Classes used during the time user is moving the switch with the mouse.
  25. draggingClass: 'switch-dragging',
  26.  
  27. // Classes applied to the topmost element for vertical sliders (when horizontal is false).
  28. verticalClass: 'vert',
  29.  
  30. // Classes applied to the topmost element when the slider is flipped
  31. flippedClass: 'flipped',
  32.  
  33. // Determines the location of the on and off.
  34. // When flipped is false, the switch is ON--OFF (for horizontal switches) or ON on the top and OFF on the bottom (for vertical switches)
  35. // When flipped is true, the switch is OFF--ON (for horizontal switches) or OFF on the top and ON on the bottom (for vertical switches)
  36. flipped: false,
  37.  
  38. // Determines the switch orientation, either horizontal or vertical.
  39. horizontal: true
  40. },
  41.  
  42. // toggleType is only meaningful if type is 'toggle'
  43. toggleType: {
  44.  
  45. // Classes for the container. Type: string.
  46. // Add:
  47. // - corners-rounded for full rounded corners;
  48. // - corners-halfrounded for 50% rounded corners;
  49. // - corners-quarterrounded for 25% rounded corners;
  50. // - corners-sharp for no rounded corners;
  51. // If none of the above four are specified, corners-rounded is used.
  52. outerClass: 'checkboxswitch-outer toggle corners-rounded',
  53.  
  54. // Determines whether a span child is used to display 0 or 1 (you can change CSS to display something else).
  55. showOnOff: true,
  56.  
  57. // Class added to the span created when showOnOff is true.
  58. onOffClass: 'onoff',
  59.  
  60. // Specifies the text caption that appears in toggle switches.
  61. // If null, then uses the text from the markup.
  62. // If the plugin is not bounded to an <input type=checkbox> element, this caption is appended to the existing markup text (if any).
  63. // Type: String.
  64. caption: null,
  65.  
  66. // Each class represents a frame in the animation that runs from OFF to ON position.
  67. // Type: array of String.
  68. // The first class is used for the OFF image, the last for the ON image.
  69. // Optional frames in the middle can be used to create a more realistic animation.
  70. // For ON to OFF animations, the plug-in simply iterates from the last to the first class.
  71. frameClasses: ['frm1', 'frm2', 'frm3', 'frm4', 'frm5']
  72. },
  73.  
  74. // pushType is only meaningful if type is 'push'
  75. pushType: {
  76.  
  77. // Classes for the container.
  78. // Add:
  79. // - corners-rounded for full rounded corners;
  80. // - corners-halfrounded for 50% rounded corners;
  81. // - corners-quarterrounded for 25% rounded corners;
  82. // - corners-sharp for no rounded corners;
  83. // If none of the above four are specified, corners-rounded is used.
  84. outerClass: 'checkboxswitch-outer toggle push corners-rounded',
  85.  
  86. // Determines whether a span child is used to display 0 or 1 (you can change CSS to display something else).
  87. showOnOff: true,
  88.  
  89. // Class added to the span created when showOnOff is true.
  90. onOffClass: 'onoff',
  91.  
  92. // Specifies the text caption that appears in toggle switches.
  93. // If null, then uses the text from the markup.
  94. // If the plugin is not bounded to an <input type=checkbox> element, this caption is appended to the existing markup text (if any).
  95. caption: null,
  96.  
  97. // Each class represents a frame in the animation that runs from OFF to ON position.
  98. // Type: array of String.
  99. // The first class is used for the OFF image, the last for the ON image. Optional frames in the middle can be used to create a more realistic animation.
  100. // For ON to OFF animations, the plug-in simply iterates from the last to the first class.
  101. frameClasses: ['frm1', 'frm2', 'frm3', 'frm4']
  102. },
  103.  
  104. keyboard: {
  105.  
  106. // Allowed keys on focusable switches.
  107. // Type: String array.
  108. // Key events are ignored for non focusable switches.
  109. // A switch is focusable when the associated markup is an <input type="checkox"> with no disabled attribute, or any HTML element with a tabindex attribute.
  110. allowed: ['enter', 'esc', 'space'],
  111.  
  112. // Determines whether pressing Esc key rollsback to the value the switch had before gaining focus (the committed value).
  113. // Type: boolean or string.
  114. // Rolling back has no effect when the current value is the same as the committed value.
  115. // true - Pressing Esc on a focusable switch rollsback value. Change events are not fired. Note that 'esc' should be present in the allowed property array (see above).
  116. // false - Ignores Esc keystrokes.
  117. // 'trueWithEvents' - Pressing Esc on a focusable switch rollsback value.
  118. // If it rolls back to On, then OnChange and OnChangeOn are fired.
  119. // If it rolls back to Off, then OnChange and OnChangeOff are fired.
  120. rollbackOnEsc: true,
  121.  
  122. // Determines whether the value toggles on Enter or Space.
  123. // true - Pressing Enter or Space changes value. Note that 'enter' or 'space' should be present in the allowed property array (see above).
  124. // false - Ignores Enter or Space keystrokes.
  125. toggleOnEnter: true,
  126.  
  127. // Determines whether the value is saved when switch loses focus. Type: boolean.
  128. // true - The current value is commited when switch loses focus.
  129. // false - Value is not commited.
  130. commitOnBlur: false
  131. },
  132.  
  133. // Classes set to the outer div when the switch has been changed.
  134. changedClass: 'changed',
  135.  
  136. // Classes set to the outer div when the switch is disabled.
  137. disabledClass: 'disabled',
  138.  
  139. // Determines whether the control is editable.
  140. // If the plugin is associated with a disabled <input type="checkbox" disabled>, then enabled is set to false.
  141. enabled: true,
  142.  
  143. // Handle animation in milliseconds. Type: positive integer number.
  144. // For Sliding switches, specifies the time it takes to move from one side to the other.
  145. // For Toggle or Push switches, specifies the time it takes to change from one position to another.
  146. speed: 75,
  147.  
  148. // Event fired when switch changes to either On or Off.
  149. // Type: function (event, $elem, value)
  150. onChange: null,
  151.  
  152. // Event fired when switch changes to On position.
  153. // Type: function (event, $elem)
  154. // If the markup is an <input type="checkbox"> then an attribute checked="checked" is added.
  155. // If other markup is used, then an attribute data-checked="checked" is added.
  156. onChangeOn: null,
  157.  
  158. // Event fired when switch changes to OFF position.
  159. // Type: function (event, $elem)
  160. // If the markup is an <input type="checkbox"> then the attribute checked="checked" is removed.
  161. // If other markup is used, then the attribute data-checked="checked" is removed.
  162. onChangeOff: null
  163.  
  164. });

5.API方法。

  1. // refresh$e.rsCheckboxSwitch('refresh')
  2. $e.rsCheckboxSwitch('refresh', false)
  3. $e.rsCheckboxSwitch('refresh', true)
  4.  
  5. // toggle
  6. $e.rsCheckboxSwitch('toggle')
  7.  
  8. // set on/off states without animations
  9. $e.rsCheckboxSwitch('on')
  10. // OnChange and onChangeOn are never fired
  11. $e.rsCheckboxSwitch('on', false)
  12. OnChange and onChangeOn are always fired
  13. $e.rsCheckboxSwitch('on', true)
  14. $e.rsCheckboxSwitch('off')
  15. $e.rsCheckboxSwitch('off', false)
  16. $e.rsCheckboxSwitch('off', true)
  17.  
  18. // set on/off states with animations
  19. $e.rsCheckboxSwitch('onAnim')
  20. $e.rsCheckboxSwitch('onAnim', false)
  21. $e.rsCheckboxSwitch('onAnim', true)
  22. $e.rsCheckboxSwitch('offAnim')
  23. $e.rsCheckboxSwitch('offAnim', false)
  24. $e.rsCheckboxSwitch('offAnim', true)
  25.  
  26. // Rollsback the switch to the original value
  27. $e.rsCheckboxSwitch('rollback')
  28. // Change events are never fired
  29. $e.rsCheckboxSwitch('rollback', false)
  30. // Change events are always fired
  31. $e.rsCheckboxSwitch('rollback', true)
  32.  
  33. // Notifies the plugin that current value is the "new" original one.
  34. $e.rsCheckboxSwitch('commit')
  35.  
  36. // Unbinds all events and completely removes the plugin from the page.
  37. $e.rsCheckboxSwitch('destroy')

6.沉降器和沉降器。

  1. // Returns the current onChange event function handler
  2. var onChangeFunction = $e.rsCheckboxSwitch('option', 'onChange');
  3.  
  4. // Returns the current onChangeOn event function handler
  5. var onChangeOnFunction = $e.rsCheckboxSwitch('option', 'onChangeOn');
  6.  
  7. // Returns the current onChangeOff event function handler
  8. var onChangeOffFunction = $e.rsCheckboxSwitch('option', 'onChangeOff');
  9. // Returns true if switched was changed, false otherwise.
  10. // "Changed" means the current value is different from the current commited value.
  11. var changed = $e.rsCheckboxSwitch('option', 'changed');
  12.  
  13. // Returns switch current value.
  14. var isOn = $e.rsCheckboxSwitch('option', 'value');
  15.  
  16. // Retuns true if switch can be changed by the user, false otherwise.
  17. var isEnabled = $e.rsCheckboxSwitch('option', 'enabled');
  18.  
  19. // Sets the onChange event function handler
  20. $e.rsCheckboxSwitch('option', 'onChange', function (event, $elem, value) { alert('value changed to ' + value); } );
  21.  
  22. // Sets the onChangeOn event function handler
  23. $e.rsCheckboxSwitch('option', 'onChangeOn', function (event, $elem) { alert('value changed to true'); } )
  24.  
  25. // Sets the onChangeOff event function handler
  26. $e.rsCheckboxSwitch('option', 'onChangeOff', function (event, $elem) { alert('value changed to false'); } )
  27. // Enables or disables the switch control.
  28. $e.rsCheckboxSwitch('option', 'enabled', true);
  29. $e.rsCheckboxSwitch('option', 'enabled', false);

7.覆盖Less中的默认变量。

  1. // recommended #454545 for dark layout and #f2f2f2 for light layout
  2. @background: #454545;
  3.  
  4. // recommended 12% for dark layout and 18% for light layout
  5. @deltaContrast: 12%;
  6.  
  7. @text: #bab026;
  8.  
  9. // Allowed units are: px, pt (for fixed layout) or em, rem, vw, vh, vmin, vmax (for elastic layout).
  10. // To try other sizes, you only need to change this line. All the rest will resize according to this @refScale
  11. // 1em corresponds to 16px in most browsers, which is the default font size.
  12. // However, some users might define another default font size for accessibility reasons.
  13. // To convert px to em, do em = px/16. Example: 8px = .5em
  14. @refScale: 5vw;
  15.  
  16. @fromGradientOuter: darken(@background, @deltaContrast);
  17. @toGradientOuter: lighten(@background, @deltaContrast);
  18.  
  19. @fromGradientInner: darken(@toGradientOuter, 10%);
  20. @toGradientInner: darken(@fromGradientOuter, 2%);
  21.  
  22. @borderColor: darken(@fromGradientOuter, lightness(@background)/10 + 3%);
  23.  
  24. @sliderMaxSize: @refScale*6;
  25. @sliderMinSize: @refScale*1.3;
  26. @sliderHandleSize: @refScale*2;
  27.  
  28. @textoff: @fromGradientOuter;
  29. @texton: desaturate(@text, lightness(@background)/1.8);

预览截图