jQuery a+Switch 开关代码

依赖插件

jquery.min.js
jquery.actual.min.js https://github.com/dreamerslab/jquery.actual

 <input type="checkbox" name="" class="a-switch" data-on-name="启用" data-off-name="禁止" checked/>
input.a-switch {
	display: none;
}
.a-switch-container {
	display: inline-block;
	position: relative;
	height: 35px;
	border: 1px solid #46cd93;
	vertical-align: middle;
	cursor: pointer;
	overflow: hidden;
}
.a-switch-wrapper {
	height: 100%;
	font-size: 0;
	white-space: nowrap;
	-webkit-transition: margin-left .5s;
    transition: margin-left .5s;
}
.a-switch-handle-on,
.a-switch-handle-label,
.a-switch-handle-off {
	display: inline-block;
	padding: 0 12px;
	height: 100%;
	line-height: 33px;
	font-size: 14px;
	text-align: center;
	vertical-align: middle;
}
.a-switch-handle-on {
	background-color: #f2f2f3;
	color: #fff;
}
.a-switch-handle-label {
	background-color: #fff;
}
.a-switch-handle-off {
	background-color: #f5f5f5;
}
.a-switch-disabled {
	cursor: default;
}
.a-switch-disabled .a-switch-handle-on,
.a-switch-disabled .a-switch-handle-off {
    opacity: .5;
}
.a-switch-container.a-switch-sm {
	height: 27px;
}
.a-switch-container.a-switch-sm .a-switch-handle-on,
.a-switch-container.a-switch-sm .a-switch-handle-label,
.a-switch-container.a-switch-sm .a-switch-handle-off {
	padding: 0 6px;
	line-height: 25px;
	font-size: 12px;
}
jQuery.extend({
    aSwitch: function() {
    	$('input.a-switch').each(function () {
    		var _input = $(this),
    			name = _input.attr('name'),
    			onName = _input.attr('data-on-name'),
    			offName = _input.attr('data-off-name'),
    			onValue = _input.attr('data-on-value'),
    			offValue = _input.attr('data-off-value'),
				size = _input.attr('data-size'),
				callback = _input.attr('data-callback') ? _input.attr('data-callback').split('|') : '',
	    		checked = _input.prop('checked'),
	    		disabled = _input.prop('disabled');

	    	onValue = onValue ? onValue : 1;
	    	offValue = offValue ? offValue : 0;

	    	var _switch = $('\
					<div class="a-switch-container' + (disabled == true ? ' a-switch-disabled ' : ' ') + (checked == true ? 'a-switch-on' : 'a-switch-off') + (size ? ' a-switch-' + size : '') + '">\
						<div class="a-switch-wrapper">\
							<span class="a-switch-handle-on">' + (onName ? onName : 'ON') + '</span>\
							<span class="a-switch-handle-label"></span>\
							<span class="a-switch-handle-off">' + (offName ? offName : 'OFF') + '</span>\
							<input type="hidden" name="' + name + '" value="' + (checked == true ? onValue : offValue) + '"/>\
						</div>\
					</div>\
		    	');

            _input.before(_switch).remove();

	    	var onWidth = _switch.find('.a-switch-handle-on').actual('outerWidth', {absolute: true}),
	    		offWidth = _switch.find('.a-switch-handle-off').actual('outerWidth', {absolute: true}),
	    		spanWidth = onWidth >= offWidth ? onWidth : offWidth;

            _switch.css('width', spanWidth * 2 + 2).find('.a-switch-handle-on, .a-switch-handle-label, .a-switch-handle-off').css('width', spanWidth);

			if (checked == false) {
				_switch.find('.a-switch-wrapper').css({'margin-left': -spanWidth + 'px'})
			}

            _switch.on('click', function () {
	    		var _this = $(this),
					value = _this.hasClass('a-switch-on') ? offValue : onValue,
					checked =  _this.hasClass('a-switch-on') ? false : true,
					res = true;

				if ($.isArray(callback)) {
					res = eval(callback[0] + '(_this, callback, name, value, checked)');
				}

				if (res == true) {
	    			if (_this.hasClass('a-switch-on')) {
						_this.removeClass('a-switch-on').addClass('a-switch-off').find('.a-switch-wrapper').css('margin-left', -spanWidth + 'px').find('input[name="' + name + '"]').val(offValue);
					} else if (_this.hasClass('a-switch-off')) {
						_this.removeClass('a-switch-off').addClass('a-switch-on').find('.a-switch-wrapper').css('margin-left', '0px').find('input[name="' + name + '"]').val(onValue);
					}
				}
	    	})
    	})
    }
})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

A哎呀妈呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值