<html>
<div class="center">
<div class="slider" data-pos="0">
<div class="slider__slides">
<div class="slider__slide"></div>
<div class="slider__slide"></div>
<div class="slider__slide"></div>
<div class="slider__slide"></div>
</div>
<div class="slider__dots">
<a href="#" class="slider__indicator"></a>
<a href="#" class="slider__dot" data-pos="0"></a>
<a href="#" class="slider__dot" data-pos="1"></a>
<a href="#" class="slider__dot" data-pos="2"></a>
<a href="#" class="slider__dot" data-pos="3"></a>
</div>
</div>
</div>
</html>
<style>
.slider {
position: relative;
width: 100%;
height: 100%;
overflow: hidden;
}
.slider__slides {
position: relative;
width: 400%;
height: 100%;
transition: transform 0.3s cubic-bezier(0.51, 0.92, 0.24, 1);
will-change: transform;
}
.slider[data-pos="0"] .slider__slides {
transform: translateX(0%);
}
.slider[data-pos="1"] .slider__slides {
transform: translateX(-25%);
}
.slider[data-pos="2"] .slider__slides {
transform: translateX(-50%);
}
.slider[data-pos="3"] .slider__slides {
transform: translateX(-75%);
}
.slider__slide {
float: left;
width: 25%;
height: 100%;
}
.slider__dots {
display: flex;
position: absolute;
bottom: 1.5em;
left: 50%;
transform: translateX(-50%);
}
.slider__dot, .slider__indicator {
display: block;
margin: 0 0.5em;
width: 1em;
height: 1em;
background: rgba(255, 255, 255, 0.5);
border-radius: 100px;
}
.slider__indicator {
position: absolute;
background: white;
width: auto;
}
.slider__indicator--left {
transition: left 0.3s cubic-bezier(0.51, 0.92, 0.24, 1.15), right 0.3s 0.1s cubic-bezier(0.51, 0.92, 0.24, 1.15);
}
.slider__indicator--right {
transition: left 0.3s 0.1s cubic-bezier(0.51, 0.92, 0.24, 1.15), right 0.3s cubic-bezier(0.51, 0.92, 0.24, 1.15);
}
.slider[data-pos="0"] .slider__indicator {
left: 0em;
right: 6em;
}
.slider[data-pos="1"] .slider__indicator {
left: 2em;
right: 4em;
}
.slider[data-pos="2"] .slider__indicator {
left: 4em;
right: 2em;
}
.slider[data-pos="3"] .slider__indicator {
left: 6em;
right: 0em;
}
.center {
display: flex;
height: 100vh;
justify-content: center;
align-items: center;
background: #333;
}
.slider {
max-width: 600px;
max-height: 400px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.8);
}
.slider__slide:nth-child(1) {
background: #309954;
}
.slider__slide:nth-child(2) {
background: #ffbd3c;
}
.slider__slide:nth-child(3) {
background: #f8593e;
}
.slider__slide:nth-child(4) {
background: #4086fa;
}
</style>
<script>
let dots = 4;
let sliderElem = document.querySelector(".slider");
let dotElems = sliderElem.querySelectorAll(".slider__dot");
let indicatorElem = sliderElem.querySelector(".slider__indicator");
Array.prototype.forEach.call(dotElems, (dotElem) => {
dotElem.addEventListener("click", (e) => {
let currentPos = parseInt(sliderElem.getAttribute("data-pos"));
let newPos = parseInt(dotElem.getAttribute("data-pos"));
let newDirection = newPos > currentPos ? "right" : "left";
let currentDirection = newPos < currentPos ? "right" : "left";
indicatorElem.classList.remove(`slider__indicator--${currentDirection}`);
indicatorElem.classList.add(`slider__indicator--${newDirection}`);
sliderElem.setAttribute("data-pos", newPos);
});
});
</script>