【发布时间】:2023-01-29 21:19:22
【问题描述】:
//array of images
const programmingLanguages = [jsIcon, htmlIcon, cssIcon, csharpIcon];
显示下一张和上一张图像的功能。
function incrementLanguage() { if (languageIndex + 1 === programmingLanguages.length) { setlanguageIndex(0); } else { setlanguageIndex(languageIndex + 1); } } function decrementLanguage() { if (languageIndex === 0) { setlanguageIndex(programmingLanguages.length - 1); } else { setlanguageIndex(languageIndex - 1); }在这里我渲染我的图像问题是当我在 programmingLanguages [0] 上启动程序时它在左侧显示 img 占位符,因为它超出了数组。是否可以添加一个 if 语句来检查值是否低于 0 那么它应该从最后一个索引开始吗?
/*<img id="languageIconSmall" src={programmingLanguages[if(languageIndex -1 < 0 set langugeIndex to programmingLanguage.length? )]} alt=""> </img>*/ // this is the previous image <img id="languageIconSmall" src={programmingLanguages[languageIndex - 1]} alt=""> </img> //this is the current image displayed <img id="languageIcon" src={programmingLanguages[languageIndex]} alt=""></img> //this is the next image <img id="languageIconSmall" src={programmingLanguages[languageIndex + 1]} alt=""> </img>
【问题讨论】:
标签: reactjs arrays image carousel