3 回答

TA貢獻(xiàn)1793條經(jīng)驗(yàn) 獲得超6個(gè)贊
/**
* type left或者right
*
* @param {any} type
* @returns
*/
function setImage(type){
if(type=='left'){
index += imgCount;
if (index > imgs.length) {
index = imgs.length;
return;
}
}else if(type=='right'){
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]);
}
setImage('left')或者setImage('right')

TA貢獻(xiàn)2037條經(jīng)驗(yàn) 獲得超6個(gè)贊
條件判斷唄
function leftClick(){ // 至于判斷是左邊右邊元素 可以通過(guò)event 或者某些屬性什么的
if(點(diǎn)擊的是左邊元素){
//左邊的代碼執(zhí)行
}
if(點(diǎn)擊的是右邊元素){
//右邊的代碼執(zhí)行
}
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]);
}

TA貢獻(xiàn)1844條經(jīng)驗(yàn) 獲得超8個(gè)贊
function的話(huà)是可以增加參數(shù)的,hhh發(fā)現(xiàn)這個(gè)點(diǎn)之后很多方法都可以整合到一個(gè)function里面(當(dāng)然啦,前提還是要根據(jù)業(yè)務(wù)劃分啦)
這里面你的兩個(gè)click其實(shí)做的事情是差不多,完全可以在調(diào)用的時(shí)候傳個(gè)參用于標(biāo)記是leftClick還是rightClick.
綜上:
function click(flag){
// flag傳入1則是leftClick
if (flag) {
index += imgCount;
if(index > imgs.length){
index = imgs.length;
return;
}
}else{ // flag傳入0則是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]);
}
添加回答
舉報(bào)