按钮图标

jQuery简洁实用滑动按钮插件

阿里云

这是一款非常实用的 jQuery 滑动按钮插件。该滑动按钮插件可以将 input 元素转换为滑动按钮样式。插件中使用 javascript 来控制滑动按钮的滑动,样式使用纯 CSS 来制作。

使用方法:

首先要在页面中引入 jQuery 和 jquery.onoff.css 及 jquery.onoff.js 文件。然后可以使用两种方式调用该滑动按钮插件:

也想出现在这里?联系我们
创客主机

With AMD:

  1. define([ 'jquery', 'plugins/jquery.onoff' ], function( $ ) {
  2.   $('input[type="checkbox"]').onoff();
  3. });

With Script Tags:

  1. <script src="jquery.js"></script>
  2. <script src="plugins/jquery.onoff.min.js"></script>
  3. <script>
  4. jQuery(function($) {
  5.   $('input[type="checkbox"]').onoff();
  6. });
  7. </script>

参数:

  1. OnOff.defaults = {
  2.   // The event namespace
  3.   // Should always be non-empty
  4.   // Used to bind jQuery events without collisions
  5.   namespace: '.onoff',
  6.  
  7.   // The class added to the checkbox
  8.   className: 'onoffswitch-checkbox'
  9. };

事件:

All methods can be called widget-style.

option():

Returns: If getting, the option value; if setting, the jQuery collection for chaining.

option()方法可以通过无参数调用返回所以得选项:

  1. var options = $input.onoff('option');

通过单个参数可以获取单个选项:

  1. var eventNamespace = $input.onoff('option', 'namespace');

可以通过键和值或键值对对象来设置选项:

  1.  $input.onoff('option', 'namespace', 'newspace');
  2.     $input.onoff('option', {
  3.       namespace: 'newspace',
  4.       className: 'newclass'
  5.     });

disable():

将换单按钮对象设置为不可用并取消所有的事件。

  1. $input.onoff('disable');          
  2.     isDisabled()

Returns: Boolean

返回当前的滑动按钮状态是否是不可用状态。

  1. var disabled = $input.onoff('isDisabled');          
  2.     enable()

Returns: jQuery for chaining

使滑动按钮对象可用,并重新为它绑定事件。

  1. $input.onoff('enable');          
  2.     wrap()

Returns: jQuery for chaining

Ensures the HTML for the toggle switch is correct.This method only adds any missing HTML.

  1. $input.onoff('wrap');          
  2.     unwrap()

Returns: jQuery for chaining

Removes HTML related to OnOff, leaving only the checkbox.

  1. $input.onoff('unwrap');          
  2.     destroy()

Returns: jQuery for chaining

销毁滑动按钮对象并移除数据,但是不会调用 unwrap()方法。

  1. $input.onoff('destroy');          
  2.     instance()

Returns: OnOff

返回 OnOff 实例。

  1. var instance = $input.onoff('instance');

应用举例:

添加需要的 input 元素

  1. <input type="checkbox" />

However, you can also start with the generated HTML to avoid FOUC:

  1. <div class="onoffswitch">
  2.   <input type="checkbox" class="onoffswitch-checkbox" id="myonoffswitch" />
  3.   <label class="onoffswitch-label" for="myonoffswitch">
  4.     <span class="onoffswitch-inner"></span>
  5.     <span class="onoffswitch-switch"></span>
  6.   </label>
  7. </div>

使用下面的方法初始化滑动按钮插件:

  1. $('input[type=checkbox]').onoff();

jQuery 简洁实用滑动按钮插件

已有 427 人购买
查看演示升级 VIP立刻购买

演示地址 下载地址
收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!