Created
July 24, 2022 02:12
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const list = [ | |
"../images/img1.png", | |
"../images/img2.png", | |
"../images/img3.jpeg", | |
]; | |
const img = document.getElementById('main'); | |
const [leftArrow,rightArrow] = document.getElementsByTagName('span'); | |
const div = document.getElementById('photo'); | |
let imgN = 0; | |
document.addEventListener('DOMContentLoaded',function(){ | |
img.src = list[imgN]; | |
const p = document.createElement('p'); | |
p.classList.add('addClass'); | |
div.insertBefore(p,img.nextElementSibling); | |
p.textContent = `${imgN + 1} / ${list.length}`; | |
rightArrow.addEventListener('click',function(){ | |
imgN ++; | |
//ここから追加 | |
if(imgN >= list.length){ | |
imgN = 0; | |
} | |
//ここまで追加 | |
img.src = list[imgN]; | |
p.textContent = `${imgN + 1} / ${list.length}`; | |
}); | |
leftArrow.addEventListener('click',function(){ | |
imgN --; | |
//ここから追加 | |
if(imgN < 0){ | |
imgN = list.length - 1; | |
} | |
//ここまで追加 | |
img.src = list[imgN]; | |
p.textContent = `${imgN + 1} / ${list.length}`; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment