I'm trying to implement a slider on my landing page. I followed this tutorial on Youtube but only the first two slides work. i slightly modified and only kept three slides. Can anybody give a second glance to my code to see if there is anything I missed? I will really appreciate it.
let items = document.querySelectorAll('.slider .list .item'); //Slider items
//console.log(items.length);
let next = document.getElementById('next');
let prev = document.getElementById('prev');
let thumbnails = document.querySelectorAll('.thumbnail .item'); //Thumbnai items
//console.log(thumbnails.length);
// Config parameter
let countItem = items.length; //The number of items into the series
let itemActive = 0; //The class '.active' counter
//Next Button click event
next.onclick = function() {
itemActive += 1;
if (itemActive >= countItem) {
itemActive = 0;
}
showSlider(); //Run this function to display the slider ont he scrren
}
// Click on thumbnail event
thumbnails.forEach((thumbnail, index) => {
thumbnail.addEventListener('click', () => {
itemActive = index;
showSlider();
});
});
//Run the slider automatically without any click
let refreshInterval = setInterval(() => {
// Execute the next button click automatically every 3s.
next.click();
}, 3000);
function showSlider() {
// Remove item active old
let itemActiveOld = document.querySelector('.slider .list .item.active');
let thumbnailActiveOld = document.querySelector('.thumbnail .item.active');
itemActiveOld.classList.remove('active');
thumbnailActiveOld.classList.remove('active');
// Set class '.active' to new item
items[itemActive].classList.add('active');
thumbnails[itemActive].classList.add('active');
// Clear automatic slider run
clearInterval(refreshInterval);
}
<!-- SLIDER -->
<div class="slider">
<!-- List of items -->
<div class="list">
<div class="item active">
<img src="assets/images/student-1.jpg" alt="">
<div class="content">
<p>Prospective and Returning Students</p>
<h2>Plan your success now!</h2>
<p>Find and apply to the best Higher Education institutions in the Democratic Republic of the Congo.</p>
<a href="schools.html" class="btn">Track Your Application</a>
</div>
</div>
<div class="item">
<img src="assets/images/student-2.jpg" alt="">
<div class="content">
<p>Colleges & Universities</p>
<h2>Join Our Community</h2>
<p>
Register your school to boost your admissions rate. Your prospective students can apply from anywhere in the world.
</p>
<a href="schools.html" class="btn">Visit Our Schools' Directory</a>
</div>
</div>
<div class="item">
<img src="assets/images/student-5.jpg" alt="">
<div class="content">
<p>Education Advisors</p>
<h2>Meet Our Dedicated Advisors</h2>
<p>Our team is made of professional education advisors, ready to assist and set you into your pathway to success.</p>
<a href="advisors.html" class="btn">Schedule a meeting</a>
</div>
</div>
</div>
<!-- Arrow buttons -->
<div class="arrows">
<button id="prev"><</button>
<button id="next">></button>
</div>
<!-- Thumbnail -->
<div class="thumbnail">
<div class="item active">
<img src="assets/images/student-1.jpg" alt="">
<div class="content">Students</div>
</div>
<div class="item">
<img src="assets/images/student-2.jpg" alt="">
<div class="content">Schools</div>
</div>
<div class="item">
<img src="assets/images/student-5.jpg" alt="">
<div class="content">Advisors</div>
</div>
</div>
</div>
<!-- SLIDER -->
The sliders are run using javascript. I've double-checked to see if there is any mistake but could not find where the issue could be.
showSlider()
, which is called when you donext.click()
. So it cancels the automatic repetition.