I have my scroll images section and I want to improve it that there will be dots which will show on what position you are and how many images the scroll section have. I want the dots under the images and they will change their color on other positions. Can someone help me with that, how to do it? I do not know how to do it so if someone can help me with this i will be very glad.
Thank you.
<section class="scroll-images">
<div class="wrapper">
<i id="left" class="fa-solid fa-arrow-left"></i>
<div class="carousel">
<img src="#" alt="" draggable="false">
<img src="#" alt="" draggable="false">
<img src="#" alt="" draggable="false">
<img src="#" alt="" draggable="false">
<img src="#" alt="" draggable="false">
<img src="#" alt="" draggable="false">
</div>
<i id="right" class="fa-solid fa-arrow-right"></i>
</div>
</section>
* {
margin: 0;
padding: 0;
list-style: none;
text-decoration: none;
font-family: "Roboto", sans-serif;
box-sizing: border-box;
-webkit-font-smoothing: antialiased;
scroll-behavior: smooth;
caret-color: #ffb038;
}
.scroll-images {
display: flex;
padding: 80px 35px;
align-items: center;
justify-content: center;
}
.wrapper {
max-width: 1500px;
position: relative;
}
.wrapper i {
top: 50%;
background: #ffb038;
color: #fff;
width: 46px;
height: 46px;
font-size: 1.2rem;
cursor: pointer;
text-align: center;
line-height: 46px;
border-radius: 50%;
position: absolute;
transform: translateY(-50%);
transition: 0.5s;
}
.wrapper i:hover {
background: #fff;
border: 1px solid #000;
color: #000;
}
.wrapper i:first-child {
left: -23px;
display: none;
}
.wrapper i:last-child {
right: -23px;
}
.wrapper .carousel {
font-size: 0px;
cursor: pointer;
overflow: hidden;
white-space: nowrap;
}
.carousel.dragging {
cursor: grab;
scroll-behavior: auto;
}
.carousel.dragging img {
pointer-events: none;
}
.carousel img {
height: 340px;
object-fit: cover;
margin-left: 14px;
width: calc(100% / 3);
}
.carousel img:first-child {
margin-left: 0;
}
@media screen and (max-width: 900px) {
.carousel img {
width: calc(100% / 2);
}
}
>Solution :
To create the dots that show on what position you are in the scroll section, you can add an unordered list element with list items to your HTML markup. You can then use JavaScript to update the class of the list item based on the current position of the scroll images.
Here’s how you can modify your code:
const carousel = document.querySelector('.carousel');
const dotsContainer = document.querySelector('.dot-indicators');
const images = carousel.querySelectorAll('img');
const dotCount = images.length;
let currentIndex = 0;
// Create dot indicators
for (let i = 0; i < dotCount; i++) {
const dot = document.createElement('li');
dot.addEventListener('click', () => {
setCurrentIndex(i);
});
dotsContainer.appendChild(dot);
}
// Set the current index and update dot indicators
function setCurrentIndex(index) {
currentIndex = index;
updateDots();
}
// Update dot indicators
function updateDots() {
const dots = dotsContainer.querySelectorAll('li');
dots.forEach((dot, index) => {
if (index === currentIndex) {
dot.classList.add('active');
} else {
dot.classList.remove('active');
}
});
}
// Move to the next image
function moveNext() {
if (currentIndex < dotCount - 1) {
currentIndex++;
updateDots();
carousel.scrollTo({
left: images[currentIndex].offsetLeft,
behavior: 'smooth'
});
}
}
// Move to the previous image
function movePrevious() {
if (currentIndex > 0) {
currentIndex--;
updateDots();
carousel.scrollTo({
left: images[currentIndex].offsetLeft,
behavior: 'smooth'
});
}
}
// Add event listeners to arrow buttons
document.querySelector('#left').addEventListener('click', movePrevious);
document.querySelector('#right').addEventListener('click', moveNext);
* {
margin: 0;
padding: 0;
list-style: none;
text-decoration: none;
font-family: "Roboto", sans-serif;
box-sizing: border-box;
-webkit-font-smoothing: antialiased;
scroll-behavior: smooth;
caret-color: #ffb038;
}
.scroll-images {
display: flex;
padding: 80px 35px;
align-items: center;
justify-content: center;
}
.wrapper {
max-width: 1500px;
position: relative;
}
.wrapper i {
top: 50%;
background: #ffb038;
color: #fff;
width: 46px;
height: 46px;
font-size: 1.2rem;
cursor: pointer;
text-align: center;
line-height: 46px;
border-radius: 50%;
position: absolute;
transform: translateY(-50%);
transition: 0.5s;
}
.wrapper i:hover {
background: #fff;
border: 1px solid #000;
color: #000;
}
.wrapper i:first-child {
left: -23px;
display: none;
}
.wrapper i:last-child {
right: -23px;
}
.wrapper .carousel {
font-size: 0px;
cursor: pointer;
overflow: hidden;
white-space: nowrap;
}
.carousel.dragging {
cursor: grab;
scroll-behavior: auto;
}
.carousel.dragging img {
pointer-events: none;
}
.carousel img {
height: 340px;
object-fit: cover;
margin-left: 14px;
width: calc(100% / 3);
}
.carousel img:first-child {
margin-left: 0;
}
@media screen and (max-width: 900px) {
.carousel img {
width: calc(100% / 2);
}
}
.dot-indicators {
display: flex;
justify-content: center;
margin-top: 20px;
}
.dot-indicators li {
height: 10px;
width: 10px;
border-radius: 50%;
background-color: #ccc;
margin: 0 5px;
cursor: pointer;
transition: background-color 0.3s ease-in-out;
}
.dot-indicators li.active {
background-color: #ffb038;
}
<section class="scroll-images">
<div class="wrapper">
<i id="left" class="fa-solid fa-arrow-left"></i>
<div class="carousel">
<img src="#" alt="" draggable="false">
<img src="#" alt="" draggable="false">
<img src="#" alt="" draggable="false">
<img src="#" alt="" draggable="false">
<img src="#" alt="" draggable="false">
<img src="#" alt="" draggable="false">
</div>
<i id="right" class="fa-solid fa-arrow-right"></i>
</div>
<ul class="dot-indicators"></ul>
</section>
You can adjust the CSS styles for the dot indicators to fit your design. In the JavaScript code, the setCurrentIndex() function is called when a dot indicator is clicked, and it updates the currentIndex variable and calls the updateDots() function. The updateDots() function updates the class of the dot indicators based on the currentIndex. The moveNext() and movePrevious() functions are called when the arrow buttons are clicked, and they update the currentIndex and scroll the carousel to the next or previous image.