简体   繁体   English

如何缩短长重复路径并合并语句

[英]How to shorten long repeating path's and combine statements

for a website I use tabs with 9 different looks, these are used in different situations. 对于一个网站,我使用9种不同外观的标签,这些标签用于不同的情况。 It all works fine but I use a lot of functions with path's and statements that could be a lot shorter I think. 一切都很好,但是我认为路径和语句使用了很多函数,我认为它们可能会短很多。 An example: 一个例子:

function tabAon() {
    tabAvoor=tabAvoor+1;
    tabDrawer=tabDrawer+1;
    tabBvoor=0;
    tabCvoor=0;
    window.document.getElementById("tabA").style.textDecoration="none"; 
    window.document.getElementById("tabB").style.backgroundPosition="-87px -228px";
    window.document.getElementById("tabB").style.color="#898f92";
    window.document.getElementById("tabC").style.backgroundPosition="-190px -228px";
    window.document.getElementById("tabC").style.color="#898f92";
    window.document.getElementById("tabA").style.cursor="pointer";
    if (tabDrawer==0) {
        window.document.getElementById("header").MozTransform="translateY(0px)";
        window.document.getElementById("header").msTransform="translateY(0px)";
        window.document.getElementById("header").OTransform="translateY(0px)";
        window.document.getElementById("header").WebkitTransform="translateY(0px)";
        window.document.getElementById("header").transform="translateY(0px)";
        window.document.getElementById("tabA").style.color="#898f92";
        window.document.getElementById("tabA").style.backgroundPosition="0px 0px";
    }
    if (tabDrawer==1) {
        window.document.getElementById("header").style.MozTransform="translateY(36px)";
        window.document.getElementById("header").style.msTransform="translateY(36px)";
        window.document.getElementById("header").style.OTransform="translateY(36px)";
        window.document.getElementById("header").style.WebkitTransform="translateY(36px)";
        window.document.getElementById("header").style.transform="translateY(36px)";
        window.document.getElementById("tabAout").style.visibility="visible";
        window.document.getElementById("tabBout").style.visibility="hidden";
        window.document.getElementById("tabCout").style.visibility="hidden";
        window.document.getElementById("tabA").style.color="#000000";
        window.document.getElementById("tabA").style.backgroundPosition="0px -114px";
    }
    if (tabAvoor==2) tabAvoor=1;
    if (tabDrawer==2 && tabBvoor==0 && tabCvoor==0) tabDrawer=0;
    if (tabDrawer==2 && tabBvoor==1 || tabCvoor==1) tabDrawer=1;
}

What about using a JS framework? 那么使用JS框架呢? Eg: jQuery 例如: jQuery

Example: 例:

 $('#tabBout').show();

Instead of window.document.getElementById("tabAout").style.visibility="visible"; 而不是window.document.getElementById(“ tabAout”)。style.visibility =“ visible”;

You can shorten your code a little by defining a variable for your different IDs: 您可以通过为不同的ID定义变量来稍微缩短代码:

var tabA = window.document.getElementById("tabA");
// window.document.getElementById("tabA").style.textDecoration="none";
tabA.style.textDecoration="none";

For a start, you should try using some version of jQuery's super cool "$()" function in order to shorten all those "window.document.getElementById" , ie.: 首先,您应该尝试使用某些版本的jQuery超酷的“ $()”函数,以缩短所有这些“ window.document.getElementById”,即:

function $(id)
{
   return window.document.getElementById(id);
}

Then you can just do this: 然后,您可以执行以下操作:

$("header").style.MozTransform="translateY(36px)";

(btw: there are many other nice things you can do with $(), ie. return entire arrays of objects... the version I've wrote here is just an example) (顺便说一句:$()还可以做很多其他的事情,即返回对象的整个数组……我在这里写的版本只是一个例子)

And you can reduce your code a lot by using some functions, ie.: 而且,您可以使用一些功能来减少代码量,例如:

function set_transforms(id,pos)
{
   $(id).MozTransform="translateY("+pos+"px)";
   $(id).msTransform="translateY("+pos+"px)";
   $(id).OTransform="translateY("+pos+"px)";
   $(id).WebkitTransform="translateY("+pos+"px)";
   $(id).transform="translateY("+pos+"px)";
}

So you can replace all this: 因此,您可以替换所有这些:

window.document.getElementById("header").style.MozTransform="translateY(36px)";
        window.document.getElementById("header").style.msTransform="translateY(36px)";
        window.document.getElementById("header").style.OTransform="translateY(36px)";
        window.document.getElementById("header").style.WebkitTransform="translateY(36px)";
        window.document.getElementById("header").style.transform="translateY(36px)";

with just this: 只是这样:

set_transforms("header",36);

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM