html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Responsive Photo Gallery Slider</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
padding: 20px;
}
.slider {
position: relative;
width: 100%;
max-width: 800px;
height: 500px;
border-radius: 15px;
overflow: hidden;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
}
.slides {
display: flex;
height: 100%;
transition: transform 0.5s ease-in-out;
}
.slide {
min-width: 100%;
height: 100%;
}
.slide img {
width: 100%;
height: 100%;
object-fit: cover;
}
/* Navigation Arrows */
.nav-arrow {
position: absolute;
top: 50%;
transform: translateY(-50%);
background-color: rgba(0, 0, 0, 0.5);
color: white;
border: none;
font-size: 24px;
padding: 16px;
cursor: pointer;
user-select: none;
z-index: 10;
transition: background-color 0.3s;
}
.nav-arrow:hover {
background-color: rgba(0, 0, 0, 0.8);
}
.prev {
left: 10px;
}
.next {
right: 10px;
}
/* Dots Indicators */
.dots {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
display: flex;
gap: 10px;
z-index: 10;
}
.dot {
width: 12px;
height: 12px;
border-radius: 50%;
background-color: rgba(255, 255, 255, 0.5);
cursor: pointer;
transition: background-color 0.3s;
}
.dot.active {
background-color: white;
}
.dot:hover {
background-color: rgba(255, 255, 255, 0.8);
}
/* Responsive Design */
@media (max-width: 768px) {
.slider {
height: 300px;
max-width: 100%;
}
.nav-arrow {
font-size: 20px;
padding: 12px;
}
.dots {
bottom: 15px;
}
.dot {
width: 10px;
height: 10px;
}
}
@media (max-width: 480px) {
.slider {
height: 250px;
border-radius: 10px;
}
body {
padding: 10px;
}
}
/* Pause on hover */
.slider:hover .slides {
animation-play-state: paused;
}
</style>
</head>
<body>
<div class="slider" id="slider">
<div class="slides" id="slides">
<div class="slide">
<img src="https://picsum.photos/800/600?random=1" alt="Slide 1">
</div>
<div class="slide">
<img src="https://picsum.photos/800/600?random=2" alt="Slide 2">
</div>
<div class="slide">
<img src="https://picsum.photos/800/600?random=3" alt="Slide 3">
</div>
<div class="slide">
<img src="https://picsum.photos/800/600?random=4" alt="Slide 4">
</div>
<div class="slide">
<img src="https://picsum.photos/800/600?random=5" alt="Slide 5">
</div>
</div>
<button class="nav-arrow prev" id="prev">‹</button>
<button class="nav-arrow next" id="next">›</button>
<div class="dots" id="dots"></div>
</div>
<script>
class PhotoSlider {
constructor() {
this.slider = document.getElementById('slider');
this.slides = document.getElementById('slides');
this.prevBtn = document.getElementById('prev');
this.nextBtn = document.getElementById('next');
this.dotsContainer = document.getElementById('dots');
this.slidesArray = document.querySelectorAll('.slide');
this.currentIndex = 0;
this.slideCount = this.slidesArray.length;
this.autoSlideInterval = null;
this.autoSlideDelay = 4000; // 4 seconds
this.isTransitioning = false;
this.init();
}
init() {
this.createDots();
this.updateSlider();
this.bindEvents();
this.startAutoSlide();
}
createDots() {
this.dotsContainer.innerHTML = '';
for (let i = 0; i < this.slideCount; i++) {
const dot = document.createElement('span');
dot.classList.add('dot');
if (i === 0) dot.classList.add('active');
dot.addEventListener('click', () => this.goToSlide(i));
this.dotsContainer.appendChild(dot);
}
this.dots = document.querySelectorAll('.dot');
}
bindEvents() {
this.prevBtn.addEventListener('click', () => this.prevSlide());
this.nextBtn.addEventListener('click', () => this.nextSlide());
this.slider.addEventListener('mouseenter', () => this.pauseAutoSlide());
this.slider.addEventListener('mouseleave', () => this.startAutoSlide());
}
updateSlider() {
const translateX = -this.currentIndex * 100;
this.slides.style.transform = `translateX(${translateX}%)`;
this.updateDots();
}
updateDots() {
this.dots.forEach((dot, index) => {
dot.classList.toggle('active', index === this.currentIndex);
});
}
nextSlide() {
if (this.isTransitioning) return;
this.currentIndex = (this.currentIndex + 1) % this.slideCount;
this.updateSlider();
}
prevSlide() {
if (this.isTransitioning) return;
this.currentIndex = this.currentIndex === 0 ? this.slideCount - 1 : this.currentIndex - 1;
this.updateSlider();
}
goToSlide(index) {
if (this.isTransitioning || index === this.currentIndex) return;
this.currentIndex = index;
this.updateSlider();
}
startAutoSlide() {
this.pauseAutoSlide();
this.autoSlideInterval = setInterval(() => this.nextSlide(), this.autoSlideDelay);
}
pauseAutoSlide() {
if (this.autoSlideInterval) {
clearInterval(this.autoSlideInterval);
this.autoSlideInterval = null;
}
}
// Handle transition end for smoother control (optional enhancement)
/*
this.slides.addEventListener('transitionend', () => {
this.isTransitioning = false;
});
// Uncomment and set isTransitioning = true in next/prev/goTo if desired
*/
}
// Initialize the slider when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
new PhotoSlider();
});
</script>
</body>
</html>
Copy-paste into an HTML file and open in a browser to test. Fully functional out-of-the-box! 🚀