There is no doubt that carousels 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.
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.
Table of Contents
Features:
- Light Weight.
- Pure JavaScript Code.
- Cross Browser.
- No External Files.
- Fully Customizable.
- 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.
HTML:
<div class="slider__wrapper"> <div class="slider"> <div class="slide active"> <h3>Slide One</h3> <p>test</p> </div> <div class="slide"> <h3>Slide Two</h3> <p>test</p> </div> <div class="slide"> <h3>Slide Three</h3> <p>1234</p> </div> </div> <button id="prev" class="btn">Prev</button> <button id="next" class="btn">Next</button> </div>
JavaScript:
<script type="text/javascript"> function slider() { let slides = document.querySelectorAll(".slide"), slider = document.querySelector(".slider"), last = slider.lastElementChild, first = slider.firstElementChild, btn = document.querySelectorAll(".btn"); slider.insertBefore(last, first); btn.forEach(btn => { btn.addEventListener("click", movement); }); setInterval(function() { movement({target:{id:"next"}}); }, 3000); function movement(e) { slider = document.querySelector(".slider"); last = slider.lastElementChild; first = slider.firstElementChild; const activeSlide = document.querySelector(".active"); if (e.target.id === "next") { slider.insertBefore(first, last.nextSibling); activeSlide.classList.remove("active"); activeSlide.nextElementSibling.classList.add("active"); } else { slider.insertBefore(last, first); activeSlide.classList.remove("active"); activeSlide.previousElementSibling.classList.add("active"); } } } slider(); </script>
CSS:
<style type="text/css"> .slider { position: relative; width: 100vw; max-width: 100%; height: 300px; margin: auto; overflow: hidden; } .slide { width: 100%; height: 300px; position: absolute; text-align: center; -webkit-transition: 0.6s ease; transition: 0.6s ease; -webkit-transform: translate(-100%, 0); transform: translate(-100%, 0); } .slide.active { -webkit-transform: translate(0, 0); transform: translate(0, 0); } .slide.active~.slide { -webkit-transform: translate(100%, 0); transform: translate(100%, 0); } .slide { background: #222; color: white; padding: 30px; } button { margin-top: 20px; border: none; border-radius: 0; background: aliceblue; color: #333; padding: 10px; cursor: pointer; } </style>
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.
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.
Be the first to write a comment.