LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / JavaScript Codes » Pure Vanilla JS Infinite Responsive Slider With Navigation

Pure Vanilla JS Infinite Responsive Slider With Navigation

Pure-Vanilla-JS-Infinite-Responsive-Slider-With-Navigation
There is no doubt that sliders are essential components in a website’s design. When you are limited in space. When you are limited in space but still want to display a lot of information, carousels come in very handy as they are perfect for displaying groups of related or unrelated content.

Here we have a pure vanilla JavaScript snippet that helps you create a simple image slider with basic carousel functionalities such as infinite scrolling, autoplay, responsive and navigation with bullet points.

There are many code snippets available online or on many other blogs and websites, but everyone cannot optimize your blog or website, so you need some optimized code snippets. So now checkout out the code snippet for your blog and website that will give you all features for your desired code. Now grab the ready-to-use code and paste it where you want.

Features:

  1. Light Weight.
  2. Pure JavaScript Code.
  3. Cross Browser.
  4. No External Files.
  5. Fully Customizable.
  6. Responsive.

How To Use Pure Vanilla JS Infinite Auto Play Responsive Slider With Navigation?

There are a few easy and understandable steps to achieve your desired functionality that we are gonna share below. Follow each step perfectly.

CSS:

<style type="text/css">
.intro {
text-align: center;
}
.slider {
color: #444;
position: relative;
width: 80%;
height: 300px;
margin: 0 auto;
box-shadow: 0px 5px 10px rgba(0,0,0,0.3);
}
.slider__viewport {
position: relative;
overflow: hidden;
width: 100%;
height: 100%;
border-radius: 5px;
}
.slider__slides {
position: relative;
width: 100%;
height: 100%;
margin: 0 auto;
padding: 0;
list-style: none;
font-size: 0;
white-space: nowrap;
line-height: 1;
z-index: 10;
}
.slider__button {
font-size: 2rem;
background: none;
border: none;
position: absolute;
top: 50%;
transform: translateY(-50%);
z-index: 20;
}
.slider__button--prev {
left: 10px;
}
.slider__button--next {
right: 10px;
}
.slide-list__item {
position: relative;
display: inline-block;
width: 100%;
height: 100%;
margin: 0;
padding: 0;
transition: left .4s;
background-color: #fff;
}
.slide-list__content-area {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
padding: 20px;
box-sizing: border-box;
font-size: 1rem;
text-align: center;
}
.slider-pager {
position: absolute;
text-align: center;
width: 100%;
bottom: -50px;
}
.slider-pager__item {
border: none;
border-radius: 50%;
width: 2rem;
line-height: 2rem;
padding: 0;
margin: 0 5px;
background-color: #fff;
}

</style>

HTML:

<div class="intro">
<h1>Vanilla JS Responsive Infinite Slider with Navigation</h1>
<p>Pure javascript animation <br> (Inspired by <a href="https://output.jsbin.com/ufoceq/8/">this</a> jQuery example)</p>
</div>
<div id="categorySlider" class="slider">
<div class="slider__viewport">
<ul class="slider__slides slide-list js-slide-list" style ="left: -100%"> 
<li class="slide-list__item js-slide-list-item" data-slide-index="0">
<div class="slide-list__content-area">Custom HTML Slide 1</div>
</li>
<li class="slide-list__item js-slide-list-item" data-slide-index="1">
<div class="slide-list__content-area">Custom HTML Slide 2</div>
<!--<img class="slide-list__image" src="https://placehold.it/600X300/666666">-->
</li>
<li class="slide-list__item js-slide-list-item" data-slide-index="2">
<div class="slide-list__content-area">Custom HTML Slide 3</div>
</li>
<li class="slide-list__item js-slide-list-item" data-slide-index="3">
<div class="slide-list__content-area">Custom HTML Slide 4</div>
<!--<img class="slide-list__image" src="https://placehold.it/600X300/222222">-->
</li>
<li class="slide-list__item js-slide-list-item" data-slide-index="4">
<div class="slide-list__content-area">Custom HTML Slide 5</div>
<!--<img class="slide-list__image" src="https://placehold.it/600X300/222222">-->
</li>
<li class="slide-list__item js-slide-list-item" data-slide-index="5">
<div class="slide-list__content-area">Custom HTML Slide 6</div>
<!--<img class="slide-list__image" src="https://placehold.it/600X300/222222">-->
</li>
</ul>
</div>
<div class="slider__controls">
<button type="button" data-direction="prev" class="js-arrow-button slider__button slider__button--prev "><</button>
<button type="button" data-direction="next " class="js-arrow-button slider__button slider__button--next">></button>
<div class="slider-pager">
<button class="slider-pager__item js-pager-item" type="button" data-slide-index="0">1</button>
<button class="slider-pager__item js-pager-item" type="button" data-slide-index="1">2</button>
<button class="slider-pager__item js-pager-item" type="button" data-slide-index="2">3</button>
<button class="slider-pager__item js-pager-item" type="button" data-slide-index="3">4</button>
<button class="slider-pager__item js-pager-item" type="button" data-slide-index="4">5</button>
<button class="slider-pager__item js-pager-item" type="button" data-slide-index="5">6</button>
</div>
</div>
</div>

JavaScript:

<script type="text/javascript">
var Slider = function ( id ){
this.slider = document.getElementById( id );
this.slideList = this.slider.getElementsByClassName('js-slide-list')[0];
this.slideListItems = this.slider.getElementsByClassName('js-slide-list-item');
this.slideWidth = this.slideListItems[0].offsetWidth;
this.slidesLength = this.slideListItems.length; 
// Means we're at slide 0 (Slide 1)
this.current = 1;
this.direction;
this.animating = false;
};
Slider.prototype = {
constructor : Slider,
init : function(){
this.listenEvents();
this.cloneFirstAndLastItem();
},
listenEvents : function(){
var that = this;
var arrowButtons = this.slider.getElementsByClassName('js-arrow-button');
for (var i = 0; i < arrowButtons.length; i++) {
arrowButtons[i].addEventListener('click', function(){
that.clickArrowButton( this );
});
};
var pagerItems = this.slider.getElementsByClassName('js-pager-item');
for (var i = 0; i < pagerItems.length; i++){
pagerItems[i].addEventListener('click', function(){
that.clickPagerItem( this );
});
};
},
cloneFirstAndLastItem : function(){
var firstSlide = this.slideListItems[0];
var lastSlide = this.slideListItems[ this.slidesLength - 1 ];
var firstSlideClone = firstSlide.cloneNode( true );
var lastSlideClone = lastSlide.cloneNode( true );
// Remove data-slide-index for pager items to choose correct target
firstSlideClone.removeAttribute('data-slide-index');
lastSlideClone.removeAttribute('data-slide-index');
this.slideList.appendChild( firstSlideClone );
this.slideList.insertBefore( lastSlideClone, firstSlide );
},
clickArrowButton : function( el ){
var direction = el.getAttribute('data-direction');
var pos = parseInt( this.slideList.style.left ) || 0;
var newPos; 
// direction will be added to current slide number
this.direction = direction === 'prev' ? -1 : 1;
newPos = pos + ( -1 * 100 * this.direction );
if( !this.animating ) {
this.slideTo(this.slideList, function( progress ){
return Math.pow(progress, 2);
}, pos, newPos, 500);
// Update current slide number
this.current += this.direction;
}
},
clickPagerItem : function( el ){
var slideIndex = el.getAttribute('data-slide-index');
var targetSlide = this.slider.querySelector('.js-slide-list-item[data-slide-index="' + slideIndex +'"]');
var pos = parseInt( this.slideList.style.left ) || 0;
var newPos = Math.round( targetSlide.offsetLeft / targetSlide.offsetWidth ) * 100 * -1;
if( !this.animating && pos !== newPos ){
this.slideTo(this.slideList, function( progress ){
return Math.pow(progress, 2);
}, pos, newPos, 500);
// Update current slide number
this.current = parseInt(slideIndex) + 1;
}
},
slideTo : function( element, deltaFunc, pos, newPos, duration ){
this.animating = true;
this.animate({
delay: 20,
duration: duration || 1000,
deltaFunc: deltaFunc,
step: function( delta ){
var direction = pos > newPos ? 1 : -1
element.style.left = pos + Math.abs(newPos - pos) * delta * direction * -1 + '%';
// PREV
// Direction: -1
// Pos = -600
// newPos = 0
// Ex: Slide 4 (0px) <- Slide 1 (-600px)
//element.style.left = -600 + Math.abs(0 - (-600)) * 0.021 * -1 * -1+ 'px';
// NEXT
// Direction: +1
// Pos = -600
// newPos = -1200
// Next: Slide 1 (-600px) -> Slide 2 (-1200px)
//element.style.left = -600 + Math.abs( -600 - (-1200) ) * 0.021 * 1 * -1 + 'px';
}
}); 
},
animate : function( opts ){
var that = this;
var start = new Date();
var id = setInterval(function(){
var timePassed = new Date - start;
var progress = timePassed / opts.duration;

if( progress > 1 ) {
progress = 1;
}
var delta = opts.deltaFunc( progress );
opts.step( delta );

if( progress === 1 ){
clearInterval( id );
that.animating = false;
that.checkCurrentSlide();
}
}, opts.delay || 10 );
},
checkCurrentSlide : function( ){
var cycle = false;
//this.current += this.direction;
// Are we at the cloned slides? 
cycle = !!( this.current === 0 || this.current > this.slidesLength )
if ( cycle ) {
// update current in order to adapt new slide list
// we'll use current value to relocate slide list
this.current = ( this.current === 0 ) ? this.slidesLength : 1;
// For 4 x 600px slides, 
// left pos will be either -600px (first slide clone -> first slide) // or -2400px (last slide clone -> fourth slide)
this.slideList.style.left = ( -1 * this.current * 100 ) + '%';
} 

}
};
document.addEventListener('DOMContentLoaded', function(){
new Slider('categorySlider').init();
})
// inspired by: https://output.jsbin.com/ufoceq/8/
</script>

Customization:

No need to customize it. Just copy-paste. Rest edit the code as per comments and need. Remember to add JavaScript after HTML code.

Recommended For You:
jQuery And CSS3 Magnifying Glass To Zoom Images

Troubleshooting the Errors:

Do it with concentration and patience. Check your all steps and again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.

Final Words:

That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us If you have any doubts or problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave.

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *