Skip to content

Commit

Permalink
Update script.js
Browse files Browse the repository at this point in the history
  • Loading branch information
bradtraversy committed Aug 15, 2021
1 parent 0561e5e commit b1e2d47
Showing 1 changed file with 6 additions and 28 deletions.
34 changes: 6 additions & 28 deletions image-carousel/script.js
Expand Up @@ -4,7 +4,7 @@ const rightBtn = document.getElementById('right')

const img = document.querySelectorAll('#imgs img')

let idx = 1
let idx = 0

let interval = setInterval(run, 2000)

Expand All @@ -15,34 +15,12 @@ function run() {

function changeImage() {
if(idx > img.length - 1) {
idx = img.length - 1

imgs.style.transition = 'all, linear, 1s';
imgs.style.transform = `translateX(${-idx * 500}px)`

setTimeout(function() {
idx = 1;
imgs.style.transition = '';
imgs.style.transform = `translateX(${-500}px)`
},0)
}
else if(idx < 0){
idx = 0

imgs.style.transition = 'all, linear, 1s';
imgs.style.transform = `translateX(${-idx * 500}px)`

setTimeout(function() {
idx = img.length - 2;
imgs.style.transition = '';
imgs.style.transform = `translateX(${-idx * 500}px)`
},0)
}
else{
imgs.style.transition = 'all, linear, 1s';
imgs.style.transform = `translateX(${-idx * 500}px)`
} else if(idx < 0) {
idx = img.length - 1
}


imgs.style.transform = `translateX(${-idx * 500}px)`
}

function resetInterval() {
Expand All @@ -60,4 +38,4 @@ leftBtn.addEventListener('click', () => {
idx--
changeImage()
resetInterval()
})
})

0 comments on commit b1e2d47

Please sign in to comment.