Hello wizards, I hope you are doing great. Today in this blog you’ll learn how to create the "Image Slider" project using HTML, CSS & JavaScript.
Preview
In the above video, you’ve seen the preview of the "Image Slider" project and I hope now you are able to create this type of project. If not, I have provided all the HTML CSS and JavaScript code below.
Image Slider [Source Code]
To get the following HTML, CSS & JS code for the Image Slider project. You need to create three files one is a HTML file, second one is a CSS file and the another one is JS file. After creating these three files then you can copy-paste the given codes on your document.
Remember, you’ve to create a file with .html extension for HTML code, .css extension for CSS code and .js for JavaScript code.
You can also download all source code files from the given download button.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image slider</title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css">
</head>
<body>
<div class="images">
<a href="#" class="btn btn-left"><i class="fa-solid fa-caret-left"></i></a>
<a href="#" class="btn btn-right"><i class="fa-solid fa-caret-right"></i></a>
</div>
<script src="index.js"></script>
</body>
</html>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background-color: black;
}
a {
text-decoration: none;
}
.images {
background: url("imgs/1.png");
width: 900px;
min-height: 600px;
background-position: center;
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
margin: 4rem auto;
border: 8px solid yellow;
border-radius: 5px;
position: relative;
box-shadow: 2px 2px 20px rgba(0, 0, 0, 0.6);
}
.btn-left,
.btn-right {
position: absolute;
background-color: black;
border: 5px solid yellow;
color: yellow;
font-size: 18px;
padding: 10px;
border-radius: 5px;
}
.btn-left {
top: 50%;
left: 0;
transform: translate(-50%, -50%);
}
.btn-right {
top: 50%;
right: 0;
transform: translate(50%, -50%);
}
const leftBtn = document.querySelector(".btn-left");
const rightBtn = document.querySelector(".btn-right");
const buttons = document.querySelector(".btn");
const imageEl = document.querySelector(".images")
let pictures = ["1","2","3","4","5","6"];
let count = 0;
leftBtn.addEventListener("click",left);
rightBtn.addEventListener("click",right);
function right(){
count++;
if(count>pictures.length-1){
count=0;
}
imageEl.style.background = `url("imgs/${pictures[count]}.png")`;
}
function left(){
count--;
if(count<0 count="" imageel.style.background="`url(" imgs="" pictures="" png="" pre="">
0>