bootstrap-switch(开关插件)

最近在项目开发上遇到一个需要开关的地方,后来找了下angular-switch.js发觉样例太少,而且还需要翻墙去找,对于我这种英文菜鸟来说太麻烦了,所以选了个bootstrap-switch.js插件,发觉效果还可以。下面简单介绍下bootstrap-switch的使用。


需导入的文件

<script type="text/javascript" src="<%=request.getContextPath()%>/js/bootstrap-switch-master/dist/js/bootstrap-switch.min.js"></script>	
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/js/bootstrap-switch-master/dist/css/bootstrap3/bootstrap-switch.min.css" />
<pre name="code" class="html"><link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/js/bootstrap-switch-master/docs/js/jquery.min.js" />

 bootstrap文件可以到这里去下载: 
http://www.bootstrap-switch.org/ 

html:

<input name="status" type="checkbox" data-size="small">

通过js来实现开关的初始化

$('[name="status"]').bootstrapSwitch({
		onText:"启动",
		offText:"停止",
		onColor:"success",
		offColor:"info",
		size:"small",
		onSwitchChange:function(event,state){
			if(state==true){
				$(this).val("1");
			}else{
				$(this).val("2");
			}
		}
	})

当然也可以直接将属性写在html中,下面罗列bootstrap-switch的属性及应用

bootstrap-switch属性
js属性名html属性名类型描述取值范围默认值
statecheckedBoolean选中状态true、falsetrue
sizedata-sizeString开关大小null、mini、small、normal、largenull
animatedata-animateBoolean动画效果true、falsetrue
disableddisabledBoolean禁用开关ture、falsefalse
readonlyreadonlyBoolean开关状态只读,不能修改true、falsefalse
indeterminatedata-indeterminateBoolean模态true、falsefalse
inversedata-inverseBoolean颠倒开关顺序true、falsefalse
radioAllOffdata-radio-all-offBoolean允许单选按钮被用户取消选中true、falsefalse
onColordata-on-colorString左侧开关颜色primary、info、success、warning、danger、defaultprimary
offColordata-off-colorString右侧开关颜色primary、info、success、warning、danger、defaultdefault
onTextdata-on-textString左侧开关显示文本StringON
offTextdata-off-textString右侧开关显示文本StringOFF
labelTextdata-label-textString开关中间显示文本String&nbsp;
handleWidthdata-handle-widthString|Number开关左右2侧的宽度String|Numberauto
labelWidthdata-label-widthString|Number开关中间的宽度String|Numberauto
baseClassdata-base-classString开关基础样式Stringbootstrap-switch
wrapperClassdata-wrapper-classString | Array元素样式容器String | Arraywrapper
onInit function初始化开关Functionfunction(event,state){}
onSwitchChange function当开关状态改变时触发Functionfunction(event,state){}


覆盖全局默认值:

$.fn.bootstrapSwitch.defaults.size = 'large';
$.fn.bootstrapSwitch.defaults.onColor = 'success';

示例图:



  • 11
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 22
    评论
评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值