Simple Vanilla Javascript Slider
Simple Vanilla Javascript Slider
window.onload = function(){
//setInterval(getSlides, 4000);
// Next btn
console.log(start_slide);
});
// Prev btn
const prevBtn = document.querySelector('.previous');
prevBtn.addEventListener('click', function(){
// Give the current slide an inactive class
slides[start_slide--].className = "inactive";
if(start_slide >= 0){
// Reset slides current inactive state
slides[0].className = "slide1 active";
slides[1].className = "slide2 active";
slides[2].className = "slide3 active";
// Reset slide count if we reach the end of the array
start_slide = 0;
}
console.log(start_slide);
});
};
body {
background: #2b2b2b;
height: 100vh;
margin: 0;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
font-family: Helvetica neue, roboto;
}
h1 {
color: #bdbdbd;
font-weight: 300;
}
#slider-wrapper{
position:relative;
margin:0 auto;
max-width:510px;
border:2px solid #bdbdbd;
border-radius:5px;
box-shadow:0 0 5px #fff;
overflow:hidden;
max-height:355px;
}
.active{
display:block;
}
.inactive{
display:none;
a {
text-decoration: none;
display: inline-block;
padding: 8px 16px;
}
a:hover {
background-color: #ddd;
opacity:0.8;
transition:opacity 1s, background 1s;
color: black;
}
.previous {
position:absolute;
left:0px;
top:50%;
background-color: #f1f1f1;
color: black;
border-radius:5px;
}
.next {
position:absolute;
right:0px;
top:50%;
background-color: #f1f1f1;
color: black;
border-radius:5px;
}
<div>
<h1>JavaScript Slider</h1>
<div id="slider-wrapper">
<div class="slide1">
<h3>Slide 1 text</h3>
<img src="https://www.tatnews.org/wp-content/uploads/2015/09/Ko-Chang-
500x300.jpg" alt="">
</div>
<div class="slide2">
<h3>Slide 2 text</h3>
<img src="http://thailatinamerica.net/mexico/images/Ko-Samui-500x300.jpg"
alt="">
</div>
<div class="slide3">
<h3>Slide 3 text</h3>
<img src="http://pedratour.com/wp-content/uploads/2017/03/IMG_3253-
500x300.jpg" alt="">
</div>
</div>
</div>