64 lines
2.4 KiB
JavaScript
Executable File
64 lines
2.4 KiB
JavaScript
Executable File
!function ($, UI) {
|
|
"use strict";
|
|
var dom;
|
|
var switchs = function ($element, onClick) {
|
|
var _this = this;
|
|
dom = $element;
|
|
if ($element.length > 1) {
|
|
$element.each(function () {
|
|
_this._init($(this));
|
|
});
|
|
} else {
|
|
_this._init($element);
|
|
}
|
|
|
|
return this;
|
|
}
|
|
switchs.prototype._init = function ($element) {
|
|
if ($element[0]) {
|
|
if (!$element[0].classList.contains('am-switch-already')) { // 判断元素是否包含类名
|
|
var mini_bool = $element[0].classList.contains('am-switch-mini');
|
|
$element[0].className = "am-switch-already";
|
|
var $switch = $("<div _switch class='switch-checkbox am-switch am-round am-inline-block " + (mini_bool ? 'am-switch-mini' : '') + "'></div>");
|
|
if ($element.is(':checked')) $switch.addClass('am-active');
|
|
$switch.append('<div class="am-switch-handle"></div>')
|
|
$element.wrap($switch);
|
|
}
|
|
}
|
|
};
|
|
switchs.prototype._on = function () {
|
|
$(document).on('click', '.am-switch', function () {
|
|
var $switch = $(this);
|
|
var $checkbox = $switch.find("input[type='checkbox']");
|
|
if ($checkbox.attr('data-loading') !== undefined) {
|
|
if (!$switch.hasClass('loading')) {
|
|
$switch.addClass('loading');
|
|
}
|
|
}
|
|
var state = $checkbox.is(':checked');
|
|
$switch.css({
|
|
'transition-duration': '0.2s'
|
|
});
|
|
$checkbox.prop("checked", !state).trigger('change');
|
|
$switch[state ? 'removeClass' : 'addClass']('am-active');
|
|
});
|
|
};
|
|
// 添加一个新的方法解开开关禁止点击的状态
|
|
switchs.prototype.toggleSwitchOpen = function (val) {
|
|
$(dom).parent().parent().removeClass('loading');
|
|
};
|
|
// 添加一个新的方法来切换开关状态
|
|
switchs.prototype.toggleSwitch = function (val) {
|
|
$(dom)
|
|
.prop("checked", val)
|
|
.parent().parent().toggleClass('am-active', val);
|
|
};
|
|
$.fn.extend({
|
|
'switch': function () {
|
|
return new switchs(this);
|
|
}
|
|
});
|
|
UI.ready(function (context) {
|
|
$('input[data-am-switch]', context).switch()._on();
|
|
});
|
|
}(jQuery, jQuery.AMUI); |