Hello developers, today in this blog you'll learn to create an Image Slider using HTML, CSS & JavaScript.
Image slider or slideshow commonly shows one large image. The images get manually forward and backward, allowing you to click on the forward and backward button.
In this blog Image Slider, there are five images but only a single image will appear at the front. There are buttons on the left and right side of the image which works manually by clicking on either side of the buttons. These buttons slide these images forward and backward one by one on a button click. By clicking on the button you can forward or backward the images as you want. This forward and backward function is performed by using JavaScript.
The source code of this Image Slider is given below, if you want the source code of this program, you can copy it. You can use this Image Slider with your creativity and can take this project to the next level.
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've to make a file with a .html extension.
<!DOCTYPE html>
<html>
<head>
<title>Custom Select || Learningrobo</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<div class="slider-container">
<div class="slide active" style="background-image: url('https://cdn.pixabay.com/photo/2016/10/22/17/46/mountains-1761292__340.jpg')"></div>
<div class="slide" style="background-image: url('https://cdn.pixabay.com/photo/2016/11/29/13/49/christmas-baubles-1869989__340.jpg')"></div>
<div class="slide" style="background-image: url('https://cdn.pixabay.com/photo/2017/12/20/16/49/christmas-3030279__340.jpg')"></div>
<div class="slide" style="background-image: url('https://cdn.pixabay.com/photo/2017/08/07/22/10/church-2608425__340.jpg')"></div>
<div class="slide" style="background-image: url('https://cdn.pixabay.com/photo/2017/01/06/23/03/sunrise-1959227__340.jpg')"></div>
<button class="arrow left-arrow" id="left">❮</button>
<button class="arrow right-arrow" id="right">❯</button>
</div>
<div class="credit">Made with <span style="color:tomato;font-size:20px;">❤</span> by <a href="https://www.learningrobo.com/">Learning Robo</a></div>
<script src="script.js"></script>
</body>
</html>
CSS provides style to an HTML page. To form the page attractive create a CSS file with the name style.css and remember that you've got to make a file with a .css extension.
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.
We hope you would like this Image Slider using HTML, CSS & JavaScript.
Thank you for reading our blog. If you face any problem in creating this 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.
Image slider or slideshow commonly shows one large image. The images get manually forward and backward, allowing you to click on the forward and backward button.
In this blog Image Slider, there are five images but only a single image will appear at the front. There are buttons on the left and right side of the image which works manually by clicking on either side of the buttons. These buttons slide these images forward and backward one by one on a button click. By clicking on the button you can forward or backward the images as you want. This forward and backward function is performed by using JavaScript.
The source code of this Image Slider is given below, if you want the source code of this program, you can copy it. You can use this Image Slider with your creativity and can take this project to the next level.
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've to make a file with a .html extension.
@import url('https://fonts.googleapis.com/css?family=Muli&display=swap');
* {
box-sizing: border-box;
}
body {
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
margin: 0;
}
body::after {
background-image: inherit;
background-position: center center;
background-size: cover;
content: '';
position: absolute;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
z-index: -2;
transition: 0.4s ease;
}
body::before {
background-color: rgba(0, 0, 0, 0.7);
content: '';
position: absolute;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
z-index: -1;
}
.slider-container {
box-shadow: 0 3px 6px rgba(0,0,0,0.16), 0 3px 6px rgba(0,0,0,0.23);
position: relative;
overflow: hidden;
height: 70vh;
width: 70vw;
}
.slide {
background-position: center center;
background-size: cover;
position: absolute;
top: -15vh;
left: -15vw;
height: 100vh;
width: 100vw;
opacity: 0;
transition: 0.4s ease;
z-index: 1;
}
.slide.active {
opacity: 1;
}
.arrow {
background-color: transparent;
border: 2px solid #fff;
color: #fff;
cursor: pointer;
font-size: 30px;
padding: 20px;
position: fixed;
top: 50%;
transform: translateY(-50%);
}
.arrow:focus {
outline: 0;
}
.left-arrow {
left: calc(15vw - 65px);
}
.right-arrow {
right: calc(15vw - 65px);
}
.credit a{
text-decoration: none;
color: #000;
font-weight: 800;
color: #fff;
}
.credit {
position: fixed;
bottom:20px;
left:40%;
font-family: Verdana, Geneva, Tahoma, sans-serif;
margin: 10px;
color: #fff;
}
const body = document.body;
const slides = document.querySelectorAll('.slide');
const leftBtn = document.getElementById('left');
const rightBtn = document.getElementById('right');
let activeSlide = 0;
leftBtn.addEventListener('click', () => {
activeSlide--;
if(activeSlide < 0) {
activeSlide = slides.length - 1;
}
setBgToBody();
setActiveSlide();
});
rightBtn.addEventListener('click', () => {
activeSlide++;
if(activeSlide > slides.length - 1) {
activeSlide = 0;
}
setBgToBody();
setActiveSlide();
});
setBgToBody();
function setBgToBody() {
body.style.backgroundImage = slides[activeSlide].style.backgroundImage;
}
function setActiveSlide() {
slides.forEach(slide => {
slide.classList.remove('active');
});
slides[activeSlide].classList.add('active');
}
Thank you for reading our blog. If you face any problem in creating this 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.
Post a Comment
Thank you
Learning robo team