我的复选框有一个iOS7
样式开关。但是我需要给处理程序显示一个框阴影。
检查时如何添加框阴影,我的代码如下
var elems = Array.prototype.slice.call(document.querySelectorAll('.js-switch'));
elems.forEach(function (html) {
var switchery = new Switchery(html, {color: '#fff', jackColor: '#00aeef', jackSecondaryColor: ''});
html.onchange = function () {
var medicineId = html.name;
var checked = html.checked;
var form = $('form').get(0);
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
url: "{{\App\Helpers\serverName()}}/change-medicine-alarm?medicineId=" + medicineId + "&alarm=" + checked,
type: "post",
data: new FormData(form),
dataType: "json",
contentType: false,
cache: false,
processData: false
}).done(function (res) {
var information = res;
console.log(information);
});
最佳答案
我进行了很多搜索,最后我决定阅读Javascript主文件。
我将, boxShadow : null
添加到默认数组中,然后
我将this.jack.style.boxShadow = (this.options.boxShadow !== this.options.boxShadow) ? this.options.boxShadow : '-6px 0px 9px 1px #dcdcdc';
添加到Switchery.prototype.setPosition
然后将this.jack.style.boxShadow = this.options.boxShadow;
添加到Switchery.prototype.colorize函数中。最后调用我的脚本。你可以在片段中找到我的描述
var defaults = {
color : '#64bd63'
, secondaryColor : '#dfdfdf'
, jackColor : '#fff'
, jackSecondaryColor: null
, className : 'switchery'
, disabled : false
, disabledOpacity : 0.5
, speed : '0.4s'
, size : 'default'
, boxShadow : null
};
Switchery.prototype.setPosition = function (clicked) {
var checked = this.isChecked()
, switcher = this.switcher
, jack = this.jack;
if (clicked && checked) checked = false;
else if (clicked && !checked) checked = true;
if (checked === true) {
this.element.checked = true;
if (window.getComputedStyle) jack.style.left = parseInt(window.getComputedStyle(switcher).width) - parseInt(window.getComputedStyle(jack).width) + 'px';
else jack.style.left = parseInt(switcher.currentStyle['width']) - parseInt(jack.currentStyle['width']) + 'px';
if (this.options.color) this.colorize();
this.setSpeed();
} else {
jack.style.left = 0;
this.element.checked = false;
this.switcher.style.boxShadow = 'inset 0 0 0 0 ' + this.options.secondaryColor;
this.switcher.style.borderColor = this.options.secondaryColor;
this.switcher.style.backgroundColor = (this.options.secondaryColor !== defaults.secondaryColor) ? this.options.secondaryColor : '#fff';
this.jack.style.backgroundColor = (this.options.jackSecondaryColor !== this.options.jackColor) ? this.options.jackSecondaryColor : this.options.jackColor;
this.jack.style.boxShadow = (this.options.boxShadow !== this.options.boxShadow) ? this.options.boxShadow : '-6px 0px 9px 1px #dcdcdc';
this.setSpeed();
}
};
Switchery.prototype.colorize = function() {
var switcherHeight = this.switcher.offsetHeight / 2;
this.switcher.style.backgroundColor = this.options.color;
this.switcher.style.borderColor = this.options.color;
this.switcher.style.boxShadow = 'inset 0 0 0 ' + switcherHeight + 'px ' + this.options.color;
this.jack.style.backgroundColor = this.options.jackColor;
this.jack.style.boxShadow = this.options.boxShadow;
};
//Wherever you want to call the plugin you need to call like bellow
var switchery = new Switchery(html, {color: '#fff', jackColor: '#00aeef', boxShadow:'-6px 0px 9px 1px #dcdcdc'});