I’m currently building an image slider while following tutorials online, and I encountered an issue related to button behavior. The slider features two navigation buttons: Previous and Next. In my code, I implemented an animation like this:
sliderContainer.animate([{opacity:‘0.1’}, {opacity:‘1’}], {duration: 1000, fill:‘forwards’});
Although this animation works well, it also unintentionally affects the buttons. To isolate the buttons, I attempted this code snippet:
navButtons.animate([{opacity:‘1’},{opacity:‘1’}],{duration:1, fill:‘forwards’});
Here’s a segment of my code setup:
Previous
Next
const prevBtn = document.querySelector('#prevBtn');
const nextBtn = document.querySelector('#nextBtn');
const sliderContainer = document.querySelector('.slider');
const navButtons = prevBtn && nextBtn;
let currentIndex = 0;
prevBtn.addEventListener('click', goToPrevious);
nextBtn.addEventListener('click', goToNext);
function goToNext(){
sliderContainer.animate([{opacity:'0.1'}, {opacity:'1'}], {duration: 1000, fill:'forwards'});
currentIndex++;
while(currentIndex > 4){
currentIndex = 0;
}
sliderContainer.style.background = `url(images/image${currentIndex}.jpg) center/cover no-repeat`;
}
function goToPrevious(){
sliderContainer.animate([{opacity:'0.1'}, {opacity:'1'}], {duration: 1000, fill:'forwards'});
currentIndex--;
while(currentIndex < 0){
currentIndex = 4;
}
sliderContainer.style.background = `url(images/image${currentIndex}.jpg) center/cover no-repeat`;
}
How can I resolve this issue so that the buttons remain unaffected?