Responsive Rotating Image Slider using HTML, CSS & JavaScript

Responsive Rotating Image Slider using HTML, CSS & JavaScript

Hello developers, today in this blog, you'll learn to create a Responsive Rotating Image Slider using HTML, CSS & JavaScript.

The Image Slider is also called Image Carousel or Slideshow. The Image Slider may be the convenient way to display the changing or sliding images one by one on your website. In the Image Slider, the images will slide one by one manually and also automatically.

In this blog (Responsive Rotating Image Slider), there is an image at the center of the webpage which rotates automatically and continuously. The Rotating Image Slider is made responsive by using the CSS media query property.

The source code of this Responsive Rotating Image Slider is given below, if you want the source code of this program, you can copy it. You can use this Responsive Rotating Image Slider on your project and take this to the subsequent level.

Responsive Rotating Image Slider [Source Code]

To make this website, you would like to make three files: an HTML file, a CSS file & a JavaScript file. First, create an HTML file with the name of index.html and remember, you have to create a file with a .html extension.

<!DOCTYPE html> <html> <head> <title>Responsive Rotating Image Slider</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <div class="container"> <div class="slider"> <div class="slide visible"> <img src="https://cdn.pixabay.com/photo/2021/08/25/20/42/field-6574455__340.jpg" alt="" /> </div> <div class="slide"> <img src="https://cdn.pixabay.com/photo/2017/10/20/10/58/elephant-2870777__340.jpg" alt="" /> </div> <div class="slide"> <img src="https://cdn.pixabay.com/photo/2015/02/20/16/59/deer-643340__340.jpg" alt=""> </div> <div class="slide"> <img src="https://cdn.pixabay.com/photo/2018/05/17/09/18/away-3408119__340.jpg" alt=""> </div> <div class="slide"> <img src="https://cdn.pixabay.com/photo/2018/03/16/20/13/seagull-3232350__340.jpg" alt=""> </div> </div> <div class="credit">Made with <span style="color:tomato">❤</span> by <a href="https://www.learningrobo.com/">Learning Robo</a></div> </div> <script type="text/javascript" src="script.js"></script> </body> </html>
CSS provides style to an HTML page. To make the page responsive and attractive, create a CSS file with the name style.css, and remember that you have to make a file with a .css extension.


@import url('https://fonts.googleapis.com/css?family=Muli&display=swap');

* {
	box-sizing: border-box;
}

body {
	background-color: #000;
	display: flex;
	align-items: center;
	justify-content: center;
	height: 100vh;
	margin: 0;
	perspective: 1000px;
	overflow: hidden;
}

.slider {
	position: relative;
	height: 400px;
	width: 700px;
  	transform-style: preserve-3d;
	animation: rotate 4s linear infinite;
	border: 10px solid #fff;
}


.slide {
	opacity: 0;
	position: absolute;
	top: 0;
	left: 0;
	height: 100%;
	width: 100%;
}

.slide.visible {
	opacity: 1;
}

@keyframes rotate {
	0% {
		transform: rotateY(90deg);
	}
	
	47%, 53% {
		transform: rotateY(270deg);
	}
	
	94%, 100% {
		transform: rotateY(450deg);
	}
}

.slide img {
	object-fit: cover;
	height: 100%;
	width: 100%;
}

@media screen and (max-width: 500px) {
	.slider {
		height: 280px;
		width: 280px;
	}
}
.credit{
    text-align: center;
    color: #fff;
    margin-top: 10px;
    font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif;
}

.credit a{
    text-decoration: none;
    color:#fff;
    font-weight: bold;
}  
JavaScript makes the page work functionally. At last, create a JavaScript file with the name of script.js, and remember that you've got to make a file with a .js extension.


const slides = document.querySelectorAll('.slide');
const slider = document.querySelector('.slider');
let activeSlide = 0;

slider.addEventListener('animationstart', () => {
	setInterval(changeSlide, 2000);
});

function changeSlide() {
	slides[activeSlide].classList.remove('visible');
	
	activeSlide++;
	
	if(activeSlide >= slides.length) {
		activeSlide = 0;
	}
	
	slides[activeSlide].classList.add('visible');
}
We hope you will like this Responsive Rotating Image Slider using HTML, CSS & JavaScript.

Thank you for reading our blog. If you face any problem creating this Responsive Rotating Image Slider using HTML, CSS & JavaScript, then contact us or comment us. We'll try to provide a solution to your problem as soon as possible.

Thank you
Learning robo team

Post a Comment

Thank you
Learning robo team

Post a Comment (0)

Previous Post Next Post
Learning Robo says...
code copied
Welcome