javascript 我的旋转木马运行良好,除了子弹javascipt

5sxhfpxr  于 2023-01-16  发布在  Java
关注(0)|答案(1)|浏览(90)

我把子弹。这是确定的。每个子弹链接到正确的图像。这是确定的。但如果我想使用子弹改变图像。这是不确定的。什么问题?谢谢你的帮助。我已经尝试把addEventListenre,但它打破了我的代码。和setInterval不工作。这是一个数组,我需要数据来构建我的代码。
下面是HTML代码:

<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);
0sgqnhkj

0sgqnhkj1#

环绕箭头和点并委派
类似这样的东西(我不能测试,因为你没有张贴你的HTML)

let numberOfSlide = 10
let currentSlide = 0; // don't use i since it is used everywhere as an index counter
const banner = document.getElementById("banner");
const showSlide = () => console.log(currentSlide); // change the i in your showSlide for currentSlide

/* Create bullet (dot) */
document.querySelector('.dots').innerHTML = Array.from({ length: numberOfSlide }) // create an array of numberOfSlide entries
  .map(
    (_, i) => `<span class="dot${i===0?' dot_selected':''}"></span>` // use template literals 
  ).join(""); // join the spans together 

const dots = document.querySelectorAll('.dot');

banner.addEventListener("click", function(e) {
  const tgt = e.target;
  if (tgt.matches('.dot')) {
    dots.forEach((dot,i) => {
      if (tgt === dot) currentSlide = i;
      dot.classList.toggle('dot_selected', tgt === dot);
    });  
  } else {
    const dir = tgt.matches('.arrow_right') ? 1 : -1;
    currentSlide += dir;
    if (currentSlide < 0) currentSlide = 0;
    else if (currentSlide >= numberOfSlide) currentSlide = numberOfSlide - 1;
    dots[currentSlide].click()
  }
  showSlide();
});
span::after {
  content: "*"
}

.dot_selected {
  color: red;
}
<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>

相关问题