I have created a unique slider feature where images rotate either 180deg or 360deg before sliding. However, I've encountered an issue that I can't seem to figure out. The slider works perfectly until the image reaches its initial position. At this point, even though the rotateY style is being added to the images (confirmed in the console), they still do not rotate as expected. Can anyone provide insight into this strange behavior?
const container = document.querySelectorAll('.image-container')
const nextBtn = document.querySelector('.next')
let counter = 0
nextBtn.addEventListener('click', moveSlide)
function moveSlide() {
counter++
n =(counter%2) * 180 +180
if(counter===6){
console.log(counter)
image = document.querySelector(`.image-container:nth-child( ${counter}) img `)
console.log(image)
image.style.transform = `rotateY(${n}deg)`
container.forEach((image, index) => {
let width = image.getBoundingClientRect().width
console.log(width)
image.style.transform = `translate(0%) rotateY(0deg)`
counter=0
return
})
}else{
console.log({counter})
console.log({n})
image = document.querySelector(`.image-container:nth-child( ${counter}) img `)
console.log(image)
image.style.transform = `rotateY(${n}deg)`
container.forEach((image, index) => {
let width = image.getBoundingClientRect().width
setTimeout(() => {
image.style.transform = `translate(-${(counter)*width}px`
}, 450)
})
}
}
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
display: flex;
height: 100vh;
overflow: hidden;
align-items: center;
align-content: center;
justify-content: center;
}
.wrapper {
display: flex;
flex-direction: column;
}
.slider-container {
height: 50vh;
width: 300px;
display: flex;
margin: auto;
flex-direction: row;
/* overflow: hidden; */
overflow: auto;
}
.image-container,
.image-container img {
display: block;
width: 300px;
transition: transform 450ms ease;
}
.btn-container {
display: flex;
align-items: center;
justify-content: center;
flex-direction: row;
gap: 5px;
}
.btn-container .btn {
width: 15vw;
padding: 5px;
}
<div class="wrapper">
<div class="slider-container">
<div class="image-container">
<img src="https://picsum.photos/id/237/300/200" alt="">
</div>
<div class="image-container">
<img src="https://picsum.photos/seed/picsum/300/200" alt="">
</div>
<div class="image-container">
<img src="https://picsum.photos/300/200?grayscale" alt="">
</div>
<div class="image-container">
<img src="https://picsum.photos/300/200/?blur" alt="">
</div>
<div class="image-container">
<img src="https://picsum.photos/id/870/300/200?grayscale&blur=2" alt="">
</div>
<div class="image-container">
<img src="https://picsum.photos/id/1/300/200" alt="">
</div>
</div>
<div class="btn-container">
<button class="btn prev">Previous</button>
<button class="btn next">Next</button>
</div>
</div>