Hello developers, today in this blog, you'll learn to create a Horizontal 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 buttons.
In this blog (Horizontal Image Slider), there are four images but only a single image will appear at the front. There are two buttons at the right bottom 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 Horizontal Image Slider is given below, if you want the source code of this program, you can copy it. You can use this Horizontal Image Slider with your creativity and can take this project to the next level.
Horizontal 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=Roboto:400,300);
html,
body {
height: 100%;
position: relative;
font-family: Roboto;
background-color: #000;
overflow: hidden;
}
.slider-ctr {
width: 700px;
height: 450px;
margin-top: -220px;
margin-left: -350px;
position: absolute;
top: 50%;
left: 50%;
box-sizing: border-box;
border: 10px solid white;
border-radius: 5px;
overflow: hidden;
}
.slider-control {
position: absolute;
bottom: 30px;
right: 30px;
width: 80px;
overflow: hidden;
border-radius: 3px;
box-shadow: 0 3px 3px 3px rgba(0,0,0,.15);
z-index: 99;
}
.control {
width: 50%;
height: 40px;
display: block;
float: left;
text-align: center;
line-height: 40px;
cursor: pointer;
transition: .3s all ease;
background: #fff;
}
.control i {
pointer-events: none;
transition: .3s all ease;
}
.control.disabled{
pointer-events: none;
background: #fff;
}
.control.disabled i{
opacity: .5;
}
.slide {
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
transition: .45s all cubic-bezier(0.65, 0.05, 0.36, 1);
clip-path: inset(0 0 0 0);
}
.slide:before {
content: "";
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
background: rgba(0,0,0,.125);
}
.slide.slide-on {
clip-path: inset(0 100% 0 0);
}
.slide.text-on .title {
transition: .3s all cubic-bezier(0.65, 0.05, 0.36, 1) .45s;
clip-path: inset(0 0 0 0);
}
.slide.text-on .author {
transition: .3s all cubic-bezier(0.65, 0.05, 0.36, 1) .6s;
clip-path: inset(0 0 0 0);
}
.slide img{
display: block;
height: 400px;
}
.slide figcaption {
position: absolute;
top: 30px;
left: 30px;
}
.slide .title {
font-size: 50px;
margin-bottom: 2px;
color: #fff;
transition: .3s all cubic-bezier(0.65, 0.05, 0.36, 1) .45s;
clip-path: inset(0 0 0 100%);
font-weight: 400;
letter-spacing: 10px;
text-transform: uppercase;
position: relative;
}
.slide .author {
font-size: 16px;
color: #fff;
opacity: .8;
transition: .3s all cubic-bezier(0.65, 0.05, 0.36, 1) .45s;
clip-path: inset(0 0 0 100%);
font-weight: 300;
letter-spacing: 3px;
position: relative;
z-index: 9;
}
.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;
}
var sliderControl = document.querySelector(".slider-control");
var slides = document.querySelectorAll(".slide"),
slidesLength = slides.length;
var slidesArr = [].slice.call(slides);
slidesArr = slidesArr.reverse();
var slideCurrent = 0;
sliderControl.addEventListener("click", function(e){
target = e.target;
if(target.classList.contains("next")){
next = e.target,
prev = next.previousElementSibling,
nextSlide = slidesArr[slideCurrent + 1],
slide = slidesArr[slideCurrent];
slide.classList.add("slide-on");
slide.classList.remove("text-on");
nextSlide.classList.add("text-on");
slideCurrent += 1;
if(slideCurrent > 0) {
prev.classList.remove("disabled");
}
if(slideCurrent === slidesLength - 1){
next.classList.add("disabled");
}
}
if(target.classList.contains("prev")){
slideCurrent -= 1;
prev = e.target,
next = prev.nextElementSibling,
prevSlide = slidesArr[slideCurrent + 1],
slide = slidesArr[slideCurrent];
prevSlide.classList.remove("text-on");
slide.classList.remove("slide-on");
slide.classList.add("text-on");
if(slideCurrent === slidesLength - 2){
next.classList.remove("disabled");
}
if(slideCurrent === 0){
prev.classList.add("disabled");
}
}
});
balapaCop("Image Slider", "#999");
Thank you for reading our blog. If you face any problem in creating this Horizontal 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