两个function如何合并成一个function

两个如何合并成一个?

function rightClick(){


    

    index -= imgCount;

    

    

    if(index < 0)

    {

        index = 0;

        return;

    

    }

    document.getElementById("img1").setAttribute('src', imgs[(index)%3]);

    document.getElementById("img2").setAttribute('src', imgs[(index + 1)%3]);

    document.getElementById("img3").setAttribute('src', imgs[(index+2)%3]);

}

function leftClick(){

    index += imgCount;

    if(index > imgs.length){

        index = imgs.length;

        return;

    }

    document.getElementById("img1").setAttribute('src', imgs[(index)%3]);

    document.getElementById("img2").setAttribute('src', imgs[(index +1)%3]);

    document.getElementById("img3").setAttribute('src', imgs[(index+2)%3]);

    

}


芜湖不芜
浏览 888回答 3
3回答

摇曳的蔷薇

/**&nbsp;* type left或者right&nbsp;*&nbsp;&nbsp;* @param {any} type&nbsp;&nbsp;* @returns&nbsp;&nbsp;*/function setImage(type){&nbsp; &nbsp; if(type=='left'){&nbsp; &nbsp; &nbsp; &nbsp; index += imgCount;&nbsp; &nbsp; &nbsp; &nbsp; if (index > imgs.length) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index = imgs.length;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }else if(type=='right'){&nbsp; &nbsp; &nbsp; &nbsp; index -= imgCount;&nbsp; &nbsp; &nbsp; &nbsp; if (index < 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; document.getElementById("img1").setAttribute('src', imgs[(index) % 3]);&nbsp; &nbsp; document.getElementById("img2").setAttribute('src', imgs[(index + 1) % 3]);&nbsp; &nbsp; document.getElementById("img3").setAttribute('src', imgs[(index + 2) % 3]);}setImage('left')或者setImage('right')

阿晨1998

条件判断呗function leftClick(){&nbsp; &nbsp;// 至于判断是左边右边元素 可以通过event 或者某些属性什么的&nbsp; &nbsp; if(点击的是左边元素){&nbsp; &nbsp; &nbsp;//左边的代码执行&nbsp; &nbsp; }&nbsp; &nbsp; if(点击的是右边元素){&nbsp; &nbsp; &nbsp;//右边的代码执行&nbsp; &nbsp; }&nbsp; &nbsp; document.getElementById("img1").setAttribute('src', imgs[(index)%3]);&nbsp; &nbsp; document.getElementById("img2").setAttribute('src', imgs[(index +1)%3]);&nbsp; &nbsp; document.getElementById("img3").setAttribute('src', imgs[(index+2)%3]);&nbsp; &nbsp;&nbsp;}

婷婷同学_

function的话是可以增加参数的,hhh发现这个点之后很多方法都可以整合到一个function里面(当然啦,前提还是要根据业务划分啦)这里面你的两个click其实做的事情是差不多,完全可以在调用的时候传个参用于标记是leftClick还是rightClick.综上:function click(flag){&nbsp; &nbsp; // flag传入1则是leftClick&nbsp; &nbsp; if (flag) {&nbsp; &nbsp; &nbsp; &nbsp; index += imgCount;&nbsp; &nbsp; &nbsp; &nbsp; if(index > imgs.length){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index = imgs.length;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp;// flag传入0则是rightClick&nbsp; &nbsp; &nbsp; &nbsp;index -= imgCount;&nbsp; &nbsp; &nbsp; &nbsp; if(index < 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; document.getElementById("img1").setAttribute('src', imgs[(index)%3]);&nbsp; &nbsp; document.getElementById("img2").setAttribute('src', imgs[(index +1)%3]);&nbsp; &nbsp; document.getElementById("img3").setAttribute('src', imgs[(index+2)%3]);&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript