слайдер с 3 карточками на одном экране
Данный код реализует функциональность слайдера с тремя карточками, где при нажатии кнопки "Вперед" (nextBtn) карточки двигаются вправо, а при нажатии кнопки "Назад" (prevBtn) карточки двигаются влево. Текущая выделенная карточка всегда остается в центре.
Если мы берем отправную точку , нажимаем налево, направо, то все ок. Но если мы нажимаем вперед, потом назад, то все идет прахом. Может подскажете более короткий код вдобавок. Перепробовал уже кучу всего
const prevBtn = document.querySelector('.button-arrwo-left');
const nextBtn = document.querySelector('.button-arrow-right');
const cards = document.querySelector('.professional__cards');
let currentIndex = 1;
const sibling0 = cards.children[0];
const sibling1 = cards.children[1];
const sibling2 = cards.children[2];
const moveToSlideRight = function () {
if (currentIndex === 1) {
sibling0.classList.toggle('next2');
sibling1.classList.toggle('prev');
sibling1.classList.remove('slider-active');
sibling2.classList.toggle('prev-active');
console.log(currentIndex, 'next1');
}
if (currentIndex === 2) {
sibling0.classList.toggle('next2');
sibling0.classList.toggle('next-active');
sibling1.classList.toggle('prev');
sibling1.classList.toggle('next');
sibling2.classList.toggle('prev-active');
sibling2.classList.toggle('prev2');
console.log(currentIndex, 'next2');
}
if (currentIndex === 3) {
sibling0.classList.remove(...removeClases);
sibling1.classList.remove(...removeClases);
sibling1.classList.add('slider-active');
sibling2.classList.remove(...removeClases);
console.log(currentIndex, 'next3');
}
currentIndex >= 3 ? (currentIndex = 1) : currentIndex++;
};
nextBtn.addEventListener('click', moveToSlideRight);
const moveToslideLeft = function () {
if (currentIndex === 1) {
sibling0.classList.toggle('next-active');
sibling1.classList.toggle('next');
sibling1.classList.remove('slider-active');
sibling2.classList.toggle('prev2');
console.log(currentIndex, 'prev1');
}
if (currentIndex === 2) {
sibling0.classList.toggle('next2');
sibling0.classList.toggle('next-active');
sibling1.classList.toggle('next');
sibling1.classList.toggle('prev');
sibling2.classList.toggle('prev-active');
console.log(currentIndex, 'prev2');
}
if (currentIndex === 3) {
sibling0.classList.remove(...removeClases);
sibling1.classList.remove(...removeClases);
sibling1.classList.add('slider-active');
sibling2.classList.remove(...removeClases);
console.log(currentIndex, 'prev3');
}
currentIndex >= 3 ? (currentIndex = 1) : currentIndex++;
};
prevBtn.addEventListener('click', moveToslideLeft);
const removeClases = [
'prev',
'next2',
'next',
'prev2',
'prev-active',
'next-active',
];
.professional__cards {
display: flex;
margin-bottom: 7.64rem;
transition: transform 0.3s ease;
justify-content: space-between;
}
.slider {
background-image: url('/images/professionals/bg-cards.png');
background-repeat: no-repeat;
background-size: cover;
border-radius: 1.875rem 1.875rem 0rem 0rem;
display: flex;
flex-direction: column;
align-items: center;
/* width: 31rem;
height: 22.27569rem; */
/* padding-top: 0.2rem; */
opacity: 0.5;
line-height: 1;
width: 17.81944rem;
height: 12.84338rem;
transition: 1s ease;
}
.images {
width: 9.68444rem;
height: 10.375rem;
padding: 0;
margin: 0;
}
.programmer-text {
font-family: var(--font-second);
font-size: 1.25rem;
font-style: normal;
font-weight: 500;
margin-top: -0.5rem;
}
.professioal-card__arrow {
text-align: center;
display: flex;
justify-content: center;
gap: 3rem;
}
.arrows {
background: transparent;
outline: none;
border: none;
cursor: pointer;
fill: white;
padding: 0;
line-height: 0;
}
.button-arrwo-left {
background-image: url('/images/professionals/arow-left.svg');
background-repeat: no-repeat;
background-size: cover;
transition: ease 0.5s;
width: 4.30156rem;
height: 4.30156rem;
}
.button-arrwo-left:hover {
background-image: url('/images/professionals/arrow-fill-left.svg');
background-repeat: no-repeat;
background-size: cover;
width: 4.30156rem;
height: 4.30156rem;
transition: 0.5s ease;
}
.button-arrow-right {
background-image: url('/images/professionals/arrow-right.svg');
background-repeat: no-repeat;
background-size: cover;
transition: ease 0.5s;
width: 4.30156rem;
height: 4.30156rem;
}
.button-arrow-right:hover {
background-image: url('/images/professionals/arrow-fill-right.svg');
background-repeat: no-repeat;
background-size: cover;
width: 4.30156rem;
height: 4.30156rem;
transition: 0.5s ease;
}
/* #Layer_2_1_ {
width: 4.30156rem;
height: 4.30156rem;
padding: 0;
margin: 0;
line-height: 10;
} */
/* ////////////////////////// */
.slider-active {
transform: scale(1.75);
opacity: 1;
}
.prev {
transform: translateX(-150%);
}
.prev2 {
transform: translateX(-305%);
}
.prev-active {
transform: translateX(-150%) scale(1.75);
opacity: 1;
}
.next-active {
transform: translateX(150%) scale(1.75);
opacity: 1;
}
.next {
transform: translateX(150%);
}
.next2 {
transform: translateX(305%);
}
<div class="professional__cards">
<div class="slider slide-0 card-programmer">
<img
src="/images/professionals/programmer-card.png"
alt="programmer-seat"
class="images programmer__img"
/>
<p class="programmer-text">Programmer</p>
</div>
<div class="slider slider-active card-marketing">
<img
src="/images/professionals/marketing-card.png"
alt="message"
class="images marketing__img"
/>
<p class="programmer-text">Marketing</p>
</div>
<div class="slider slide-2 card-designer">
<img
src="/images/professionals/designer-card.png"
alt="box with arrows"
class="images designer__img"
/>
<p class="programmer-text">Designer</p>
</div>
</div>
<div class="professioal-card__arrow">
<button
type="submit"
class="arrows button-arrwo-left"
></button>
<button
class="arrows button-arrow-right"
type="submit"
></button>
</div>