怎么合并代码里的两个function。

怎么个欸两个function合并成一个

var imgs = ['1.jpg', '2.jpg','3.jpg'];


// 显示图片的数量

var imgCount = 1;

var index = 0;

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]);

    

}


交互式爱情
浏览 323回答 1
1回答

MM们

var imgs = ['1.jpg', '2.jpg','3.jpg'];// 显示图片的数量var index = 0;function keyClick(setT){//setT=1或者-1&nbsp;&nbsp; &nbsp; index += setT;&nbsp; &nbsp; if(index < 0){&nbsp; &nbsp; &nbsp; &nbsp; index = 0;&nbsp; &nbsp; }&nbsp; &nbsp; if(index > imgs.length){&nbsp; &nbsp; &nbsp; &nbsp; index = imgs.length;&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]);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript