In my slider, I have added a .images class along with buttons for previous and next.
To set the colors, I have used JavaScript to define an array like this:
let colors = ['red', 'green',];
Currently, clicking the next-button displays the red color. This is done using the following function:
function nextSlide() {
container.style.backgroundColor = colors[0];
I am looking to achieve a functionality where clicking the next button always displays the next color from the defined array. On the other hand, when the previous-button is clicked, the slider should show the previous color in the array.
You can see the complete source code below:
const nextBtn = document.querySelector('.nextBtn');
const prevBtn = document.querySelector('.prevBtn');
const container = document.querySelector('.images');
let colors = ['red', 'blue',];
nextBtn.addEventListener('click', nextSlide);
prevBtn.addEventListener('click', prevSlide);
function nextSlide() {
container.style.backgroundColor = colors[0];
}
function prevSlide() {
container.style.backgroundColor = colors[1];
}
body {
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background-color: lightblue;
}
.images {
background-color: #4047c9;
flex: 0 0 80%;
min-height: 70vh;
border-radius: 10px;
position: relative;
}
.btn {
display: inline-block;
background: white;
color: black;
padding: 10px;
border: none;
cursor: pointer;
}
.prevBtn {
position: absolute;
top: 50%;
left: 0;
transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
}
.nextBtn {
position: absolute;
top: 50%;
right: 0;
transform: translate(50%, -50%);
-webkit-transform: translate(50%, -50%);
-moz-transform: translate(50%, -50%);
-ms-transform: translate(50%, -50%);
}
.btn:active {
background-color: grey;
color: white;
}
.btn:hover {
background-color: grey;
color: white;
}
<div class="images">
<button type="button" class="btn prevBtn">Prev Button</button>
<button type="button" class="btn nextBtn">Next Button</button>
</div>