Switch image on button press for gallery. | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

Switch image on button press for gallery.

The image is not changing. The images wont show up as they are local files so will have too switch them to show in Sololearn. https://code.sololearn.com/WoI1BlzxKtQ8/#html

26th Jun 2020, 10:20 PM
Cam
Cam - avatar
1 Resposta
+ 5
Your functions are incrementing/decrementing picPos beyond the bounds of the pictures array. I substituted a couple of picture addresses in the pictures array and changed the static img src so that it matched the address at pictures[0] and then changed your functions so that when either end of the array was reached picPos was set to the value at the opposite end of the array depending on which button was clicked. This effectively will just loop through the pictures in the array. function picSwitchForward(){ if(picPos >= pictures.length-1) picPos = 0; else picPos++; changePic(picPos); } function picSwitchBack(){ if(picPos <= 0) picPos = pictures.length - 1; else picPos--; changePic(picPos); } function changePic(picPos) { document.getElementById("picture").src = pictures[picPos]; }
26th Jun 2020, 11:37 PM
ChaoticDawg
ChaoticDawg - avatar