
Pure JS slider code
Before We Start
A slider is one of the main components of a website, It contain pictures and a user interface element within a website. The most important one and the first section that the user sees when he/she enters a website. simple pure JavaScript image slider
How to create Simple Pure JS slider code . So if you are a person who wishes to develop a website, you have to know how to create a slider properly.
Pure JS slider code
We are here to solve your problem. In this article we discuss how to create a slider. Before we start please read the below articles which easier you to understand the process, if you are new to development.
Step 1 – Add HTML
It’s too easy and simple. Just copy and paste below code in your HTML editor between <body> </body> tag.
<body>
<div id="slideshow">
<div class="control">
<button id="previous"><</button>
<button id="next">></button>
</div>
<ul id="slides">
<li class="slideActive">Slider 1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
</ul>
<div class="pager">
<ul>
<li id="first">Slider 1</li>
<li id="second">Slider 2</li>
<li id="third">Slider 3</li>
<li id="fourth">Slider 4</li>
<li id="fifth">Slider 5</li>
<li id="sixth">Slider 6</li>
</ul>
</div>
<div class="progressbar">
<div class="full">
<div class="progress"></div>
</div>
</div>
</div>
</body>
Step 2 – Add CSS
Copy and paste below code in your HTML editor between <style></style> tag.
<style>
#slideshow {
line-height: 1;
font-family: Consolas, "Andale Mono", "Lucida Console",
"Lucida Sans Typewriter", Monaco, "Courier New", monospace;
position: relative;
height: 100vh;
width: 100vw;
}
.control {
width: 100%;
height: calc(100% - 36px);
position: absolute;
top: 36px;
margin: 0;
z-index: 99;
display: block;
}
.control > button {
width: 16.7%;
height: calc(100% - 36px);
opacity: 1;
color: #212121;
background-color: transparent;
border: 0;
cursor: pointer;
font-size: 50vh;
}
#previous {
float: left;
}
#next {
float: right;
}
#slides {
padding: 0;
margin: 0;
background: #000;
overflow: hidden;
}
#slides > li {
position: absolute;
clear: both;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: 100%;
height: 100%;
margin: 0 auto;
overflow: hidden;
opacity: 0;
overflow: hidden;
font-size: 10vh;
line-height: 100vh;
text-align: center;
color: #212121;
}
#slides li:first-child {
background: #f44336;
}
#slides li:nth-child(2) {
background: #e91e63;
}
#slides li:nth-child(3) {
background: #9c27b0;
}
#slides li:nth-child(4) {
background: #673ab7;
}
#slides li:nth-child(5) {
background: #3f51b5;
}
#slides li:last-child {
background: #2196f3;
}
#slides li.slideActive {
z-index: 2;
opacity: 1;
transition: 1s;
}
#first {
background: #f44336;
}
#second {
background: #e91e63;
}
#third {
background: #9c27b0;
}
#fourth {
background: #673ab7;
}
#fifth {
background: #3f51b5;
}
#sixth {
background: #2196f3;
}
.pager ul {
display: table;
width: 100%;
position: absolute;
z-index: 999;
}
.pager ul li {
display: table-cell;
line-height: 36px;
text-align: center;
color: #fff;
text-decoration: none;
cursor: pointer;
}
.progressbar {
position: absolute;
bottom: 0;
width: 100%;
height: 36px;
background: none;
z-index: 3;
}
.progress {
width: 0;
height: 36px;
-webkit-animation: progress 10s infinite;
background: #ffeb3b;
}
@-webkit-keyframes progress {
0% {
width: 0%;
}
100% {
width: 100%;
}
}
</style>
Step 2 – Add JavaScript
Copy and paste below code in your HTML editor between <script></script> tag.
<script>
var slide = 0,
slides = document.querySelectorAll("#slides > li"),
numSlides = slides.length,
//Functions!!
currentSlide = function () {
var itemToShow = Math.abs(slide % numSlides);
[].forEach.call(slides, function (el) {
el.classList.remove("slideActive");
});
slides[itemToShow].classList.add("slideActive");
resetProgress();
resetInterval();
},
next = function () {
slide++;
currentSlide();
},
prev = function () {
slide--;
currentSlide();
},
resetProgress = function () {
var elm = document.querySelector(".progressbar"),
newone = elm.cloneNode(true),
x = elm.parentNode.replaceChild(newone, elm);
},
resetslide = function () {
var elm = document.querySelector("#slides > li"),
newone = elm.cloneNode(true),
x = elm.parentNode.replaceChild(newone, elm);
},
resetInterval = function () {
clearInterval(autonext);
autonext = setInterval(function () {
slide++;
currentSlide();
}, 10000);
},
autonext = setInterval(function () {
next();
}, 10000);
//Buttons
document.querySelector("#first").addEventListener(
"click",
function () {
slide = 0;
currentSlide();
},
false
);
document.querySelector("#second").addEventListener(
"click",
function () {
slide = 1;
currentSlide();
},
false
);
document.querySelector("#third").addEventListener(
"click",
function () {
slide = 2;
currentSlide();
},
false
);
document.querySelector("#fourth").addEventListener(
"click",
function () {
slide = 3;
currentSlide();
},
false
);
document.querySelector("#fifth").addEventListener(
"click",
function () {
slide = 4;
currentSlide();
},
false
);
document.querySelector("#sixth").addEventListener(
"click",
function () {
slide = 5;
currentSlide();
},
false
);
document.querySelector("#next").addEventListener(
"click",
function () {
next();
},
false
);
document.querySelector("#previous").addEventListener(
"click",
function () {
prev();
},
false
);
</script>
At the end we will have something like this. You can change fonts, colors, backgrounds and all things that you want. Enjoy it.

Video Tutorial
Please watch this video to better understand this tutorial and don’t forget to subscribe to our channel.
Help others to find out about this article on Social Media sites. If you have any doubt or any problem, don’t hesitate to contact us. Thereafter we will be able to help you and also make sure you bookmark our site on your browser.