您的位置:首页 > Web前端 > BootStrap

基于Bootstrap的switch button

2014-06-06 14:18 148 查看

背景

本来是很简单的问题,由于那堵无形的墙,使得一些资料难以获取,再加上百度搜出来的代码基本都不能运行,然后就有点棘手了。。。

Switch Button

官网:http://www.bootstrap-switch.org/

快速上手

Include the dependencies: jQuery, Bootstrap and Bootstrap Switch CSS + Javascript.
[...]
<link href="bootstrap.css" rel="stylesheet">
<link href="bootstrap-switch.css" rel="stylesheet">
<script src="jquery.js"></script>
<script src="bootstrap-switch.js"></script>
[...]


Add your checkbox.
<input type="checkbox" name="my-checkbox" checked>


Initialize Bootstrap Switch.
$("[name='my-checkbox']").bootstrapSwitch();


Enjoy.


相关设置

如无意外的话这个按钮是可以滑动了。下面是一些参数、回调函数以及响应事件。

NameAttributeTypeDescriptionValuesDefault
statecheckedBooleanThe checkbox statetrue, falsetrue
sizedata-sizeStringThe checkbox statenull, 'mini', 'small', 'normal', 'large'null
animatedata-animateBooleanAnimate the switchtrue, falsetrue
disableddisabledBooleanDisable statetrue, falsefalse
readonlyreadonlyBooleanReadonly statetrue, falsefalse
indeterminateBooleanindeterminate statetrue, falsefalse
onColordata-on-colorStringColor of the left side of the switch'primary', 'info', 'success', 'warning', 'danger', 'default''primary'
offColordata-off-colorStringColor of the right side of the switch'primary', 'info', 'success', 'warning', 'danger', 'default''default'
onTextdata-on-textStringText of the left side of the switchString'ON'
offTextdata-off-textStringText of the right side of the switchString'OFF'
labelTextdata-label-textStringText of the center handle of the switchString' '
baseClassdata-base-classStringGlobal class prefixString'bootstrap-switch'
wrapperClassdata-wrapper-classString | ArrayContainer element class(es)String | Array'wrapper'
onInitFunctionCallback function to execute on initializationFunction
function(event, state) {}

onSwitchChangeFunctionCallback function to execute on switch state changeFunction
function(event, state) {}

[/code]
onText/offText可以不只是文字

$(function () {
$("[name='my-checkbox']").bootstrapSwitch({
onText:'<span class="glyphicon glyphicon-ok"></span>',
offText:'<span class="glyphicon glyphicon-remove"></span>'
});
})
这样的话,样式就是下面这样的了





状态事件监听:

$('input[name="my-checkbox"]').on('switchChange.bootstrapSwitch', function(event, state) {
console.log(this); // DOM element
console.log(event); // jQuery event
console.log(state); // true | false
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: