[英]Execute function with given parameters
I was wondering if someone could help me refine this piece of code, in my eyes it is not a nice function to execute are there ways to reduce this function in lines? 我想知道是否有人可以帮助我完善这段代码,在我看来这不是一个好函数,是否有办法减少行数?
This is my function: 这是我的功能:
function changeStyling(input, changeSelector, elementCssChange, pixels) {
if(!reload){
if(changeSelector === '.exit-intent-wrapper' && elementCssChange === 'background') {
this.base64PopupBGImg = null, document.querySelector(changeSelector).style[elementCssChange] = `${input.value}`;
}
pixels ?
document.querySelector(changeSelector).style[elementCssChange] = `${input.value}px` :
document.querySelector(changeSelector).style[elementCssChange] = `${input.value}`;
} else {
pixels[i] ? document.querySelector(changeSelector[i]).style[elementCssChange[i]] = `${input[i].value}px` : document.querySelector(changeSelector[i]).style[elementCssChange[i]] = input[i].value;
for (i = 0; i < arguments.length; i++) {
}
}
} }
And this is how i execute it on reload: 这就是我在重新加载时执行它的方式:
changeStyling(
[
document.querySelector(".exitIntentWidth"),
document.querySelector(".exitIntentHeight"),
document.querySelector(".exitIntentRadius"),
document.querySelector(".exitIntentPaddingAll"),
document.querySelector(".exitIntentPadding"),
document.querySelector(".exitIntentPaddingLeft"),
document.querySelector(".exitIntentPaddingRight"),
document.querySelector(".btnWidth"),
document.querySelector(".btnHeight"),
document.querySelector(".btnRadius"),
document.querySelector(".btnColor"),
document.querySelector(".btnTextColor")
],
[
'.exit-intent-wrapper',
'.exit-intent-wrapper',
'.exit-intent-wrapper',
'.exit-intent-wrapper',
'.exit-intent-inner',
'.exit-intent-inner',
'.exit-intent-inner',
'.button',
'.button',
'.button',
'.button',
'.button'
],
[
'width',
'height',
'borderRadius',
'padding',
'padding',
'paddingLeft',
'paddingRight',
'width',
'height',
'borderRadius',
'backgroundColor',
'color'
],
[ true, true, true, true, true, true, true, true, true, true, false, false ]
);
Does someone know how I can reduce the execute of the function? 有人知道我可以减少该函数的执行吗?
Your code currently uses parallel arrays, which are very confusing and difficult to modify. 您的代码当前使用并行数组,这些数组非常混乱且难以修改。 I recommend either multidimensional arrays or objects. 我建议使用多维数组或对象。
function changeStyling(input) {
if(!reload){
if(input.changeSelector === '.exit-intent-wrapper' && input.elementCssChange === 'background') {
this.base64PopupBGImg = null, document.querySelector(input.changeSelector).style[input.elementCssChange] = `${input.el.value}`;
}
pixels ?
document.querySelector(changeSelector).style[input.elementCssChange] = `${input.el.value}px` :
document.querySelector(changeSelector).style[input.elementCssChange] = `${input.el.value}`;
} else {
pixels[i] ? document.querySelector(input.changeSelector[i]).style[input.elementCssChange[i]] = `${input.el[i].value}px` : document.querySelector(input.changeSelector[i]).style[input.elementCssChange[i]] = input.el[i].value;
for (i = 0; i < arguments.length; i++) {
}
}
changeStyling(
[
{
el:document.querySelector(".exitIntentWidth"),
changeSelector:'.exit-intent-wrapper',
elementCssChange:'width'
//Continue for any necessary modifications
},
{
el:document.querySelector(".exitIntentWidth"),
changeSelector:'.exit-intent-wrapper',
elementCssChange:'width'
//Continue for any necessary modifications
},
]);
Please note, I haven't tested this code, it might not work like expected 请注意,我尚未测试此代码,它可能无法正常工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.