[html]<!-- Слайдер -->
<div class="SL" style="width:100%;max-width:300px;text-align:center;position:relative;margin:auto;">
<button class="sl-arrow left"><</button>
<img class="sl-image" src="" style="width:100%;height:auto;" />
<button class="sl-arrow right">></button>
<div class="sl-dots"></div>
<button class="sl-autoplay-toggle">▶</button>
</div>
<script>
(function() {
const arraySL = [
'https://forumstatic.ru/files/0013/44/56/61036.png',
'https://forumstatic.ru/files/0013/44/56/73446.png',
'https://forumstatic.ru/files/0013/44/56/54143.png',
'https://forumstatic.ru/files/0013/44/56/21344.png',
'https://forumstatic.ru/files/0013/44/56/87837.png'
];
const slider = document.querySelector('.SL');
const imgElement = slider.querySelector('.sl-image');
const leftBtn = slider.querySelector('.sl-arrow.left');
const rightBtn = slider.querySelector('.sl-arrow.right');
const dotsContainer = slider.querySelector('.sl-dots');
const autoplayBtn = slider.querySelector('.sl-autoplay-toggle');
let currentIndex = 0;
let autoSlide = null;
let isAutoplay = false;
let startX = 0;
let endX = 0;
// Создание дот-индикатора
arraySL.forEach((_, i) => {
const dot = document.createElement('span');
dot.className = 'sl-dot';
dot.addEventListener('click', () => {
currentIndex = i;
updateImage();
});
dotsContainer.appendChild(dot);
});
function updateDots() {
dotsContainer.querySelectorAll('.sl-dot').forEach((dot, i) => {
dot.style.opacity = i === currentIndex ? '1' : '0.5';
});
}
function updateImage() {
imgElement.style.opacity = 0;
setTimeout(() => {
imgElement.src = arraySL[currentIndex];
imgElement.style.opacity = 1;
updateDots();
}, 200);
}
// Кнопки
leftBtn.addEventListener('click', () => {
currentIndex = (currentIndex - 1 + arraySL.length) % arraySL.length;
updateImage();
});
rightBtn.addEventListener('click', () => {
currentIndex = (currentIndex + 1) % arraySL.length;
updateImage();
});
// Автопрокрутка
function startAutoplay() {
if (!isAutoplay) {
autoSlide = setInterval(() => {
currentIndex = (currentIndex + 1) % arraySL.length;
updateImage();
}, 3000);
autoplayBtn.textContent = '⏸';
isAutoplay = true;
}
}
function stopAutoplay() {
clearInterval(autoSlide);
autoplayBtn.textContent = '▶';
isAutoplay = false;
}
autoplayBtn.addEventListener('click', () => {
if (isAutoplay) stopAutoplay();
else startAutoplay();
});
// Свайпы для мобильных
slider.addEventListener('touchstart', e => {
startX = e.touches[0].clientX;
});
slider.addEventListener('touchend', e => {
endX = e.changedTouches[0].clientX;
if (startX - endX > 50) rightBtn.click();
if (endX - startX > 50) leftBtn.click();
});
// Инициализация
updateImage();
})();
</script>
<style>
.SL {
position: relative;
width: 100%;
max-width: 300px;
margin: auto;
text-align: center;
}
.sl-arrow {
position: absolute;
top: 50%;
transform: translateY(-50%);
width: 32px;
height: 32px;
border-radius: 50%;
background: rgba(0,0,0,0.4);
color: white;
border: none;
font-size: 18px;
font-weight: bold;
line-height: 32px;
text-align: center;
cursor: pointer;
z-index: 1;
opacity: 0;
transition: opacity 0.3s ease;
user-select: none;
}
.sl-arrow.left { left: 10px; }
.sl-arrow.right { right: 10px; }
.SL:hover .sl-arrow { opacity: 1; }
.sl-arrow:hover { background: rgba(0,0,0,0.7); }
.sl-image {
width: 100%;
height: auto;
transition: opacity 0.5s ease;
display: block;
}
.sl-dots {
text-align: center;
margin-top: 5px;
}
.sl-dot {
display: inline-block;
width: 8px;
height: 8px;
border-radius: 50%;
background: rgba(0,0,0,0.5);
margin: 0 3px;
cursor: pointer;
transition: opacity 0.3s;
}
.sl-autoplay-toggle {
position: absolute;
top: 5px;
right: 5px;
background: rgba(0,0,0,0.4);
color: white;
border: none;
width: 28px;
height: 28px;
border-radius: 50%;
cursor: pointer;
font-size: 16px;
line-height: 28px;
text-align: center;
z-index: 2;
user-select: none;
}
.sl-autoplay-toggle:hover { background: rgba(0,0,0,0.7); }
</style>
[/html]
@Laurita
🛠️Что добавлено и улучшено:
✅Добавлены стрелочки прокрутки (для десктопного режима - при наведении, для мобильного - постоянно).
✅Автопрокрутка: с кнопкой включения/выключения в верхнем правом углу.
✅Плавная анимация: transition: opacity 0.5s ease.
✅Дот-индикатор: под картинкой, кликабельный.
✅На мобильных: перелистывание кнопками или свайпами (пальцем).
✅Адаптивность: max-width: 300px; width: 100%.
📝Код: полностью обернут в IIFE, let/const используются корректно.