//Splide is a lightweight, flexible and accessible slider/carousel written in TypeScript.
//No dependencies, no Lighthouse errors.
//Get it on:
https://splidejs.com/
const slider = function () {
let currentSlide = 0;
const maxSlide = slides.length;
/////////////functions
//create dots
const createDots = function () {
slides.forEach(function (_s, i) {
dotContainer.insertAdjacentHTML(
"beforeend",
`<button class="dots__dot" data-slide="${i}"></button>`
);
});
};
//activate dot
const activateDot = function (slide) {
const allDots = document.querySelectorAll(".dots__dot");
allDots.forEach((dot) => dot.classList.remove("dots__dot--active"));
//select dot based on data set attribute
document
.querySelector(`.dots__dot[data-slide="${slide}"]`)
.classList.add("dots__dot--active");
};
//default slide
const goToSlide = function (slide) {
slides.forEach(function (s, index) {
s.style.transform = `translateX(${100 * (index - slide)}%)`;
});
};
//going next slide
const nextSlide = function () {
if (currentSlide === maxSlide - 1) {
currentSlide = 0;
} else {
currentSlide++;
}
goToSlide(currentSlide);
activateDot(currentSlide);
};
//going prev slide
const prevSlide = function () {
if (currentSlide === 0) {
currentSlide = maxSlide - 1;
} else {
currentSlide--;
}
goToSlide(currentSlide);
activateDot(currentSlide);
};
const init = function () {
goToSlide(0);
createDots();
activateDot(0);
};
init();
//Event handlers
btnRight.addEventListener("click", nextSlide);
btnLeft.addEventListener("click", prevSlide);
//short circuiting
document.addEventListener("keydown", function (e) {
e.key === "ArrowRight" && nextSlide();
e.key === "ArrowLeft" && prevSlide();
});
dotContainer.addEventListener("click", function (e) {
if (e.target.classList.contains("dots__dot")) {
const { slide } = e.target.dataset;
goToSlide(slide);
activateDot(slide);
}
});
};
slider();
var slide = new Array("foret-peuplier.jpg", "paysage-montagne.jpg", "chemin-automne.jpg", "prairie-alpes.jpg");
var numero = 0;
function ChangeSlide(sens) {
numero = numero + sens;
if (numero < 0)
numero = slide.length - 1;
if (numero > slide.length - 1)
numero = 0;
document.getElementById("slide").src = slide[numero];
}