I put bullet. it’s ok. Each bullet is linked to correct image. That’s ok. but if i want to use bullet to change of image. It isn’t ok. What the issue ? Thanks for your help. I’ve tried to put addEventListenre but it broke my code. and setInterval don’t work to. It is an array where i takes data to build my code. Jsbin console indicate that it cannot read the dots.appendChild( dot) because it is null. But my code runs except maybe the part of function selected. Maybe it is that that i have to update. When i click on left or right the bullet change of position but when i click on bullet directly nothing
here is the html code :
<div id="banner">
<img src="./assets/images/slideshow/slide1.jpg" alt="Banner print-it">
<p>Impressions tous formats <span>en boutique et en ligne</span></p>
<img class="arrow_left" src="./assets/images/arrow_left.png" alt="image fleche gauche">
<img class="arrow_right" src="./assets/images/arrow_right.png" alt="image fleche droite">
<div class="dots">
</div>
</div>
const slides = [
{
"image":"slide1.jpg",
"tagLine":"Impressions tous formats <span>en boutique et en ligne</span>"
},
{
"image":"slide2.jpg",
"tagLine":"Tirages haute définition grand format <span>pour vos bureaux et events</span>"
},
{
"image":"slide3.jpg",
"tagLine":"Grand choix de couleurs <span>de CMJN aux pantones</span>"
},
{
"image":"slide4.png",
"tagLine":"Autocollants <span>avec découpe laser sur mesure</span>"
}
]
const banner = document.getElementById('#banner');
const left = document.querySelector('.arrow_left');
const right = document.querySelector('.arrow_right');
const imageElement = document.querySelector('#banner > img');
const nomElement = document.querySelector('#banner > p');
const numberOfSlide = slides.length;
let i = 0;
/* Create bullet (dot) */
function dots() {
const dots = document.querySelector('.dots');
for (let i = 0; i < numberOfSlide; i++) {
const dot = document.createElement('span');
dot.classList.add('dot');
dots.appendChild(dot);
if (i == 0) {
dots.children[i].classList.add('dot_selected');
}
}
}
dots();
/* Link bullet to image (dot) */
function selected() {
const dot = document.getElementsByClassName('dot');
for (let i = 0; i < dot.length; i++) {
dot[i].classList.remove('dot_selected');
}
dot[i].classList.add('dot_selected');
}
/* Link tagline and image to HTML*/
function showSlide() {
imageElement.src = `./assets/images/slideshow/${slides[i].image}`;
nomElement.innerHTML = slides[i].tagLine;
console.log(imageElement);
console.log(nomElement);
selected();
}
/* Call function to move left */
left.addEventListener("click", function () {
if (i == 0) {
i = numberOfSlide - 1;
}
else {
i--;
}
console.log(left);
showSlide();
});
/* Call function to move right */
right.addEventListener("click", function () {
if (i == numberOfSlide - 1) {
i = 0;
} else {
i++;
}
console.log(right);
showSlide();
console.log(showSlide);
});
interval = setInterval(showSlide, 5000);