1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

どうもこんにちはAtsu1209です。
今回はJavaScriptでスライドショーをつくる方法を書きます。

JS

//スライドショー        
const pics_src = ["img/one.png", "img/two.png", "img/three.png"];
let num = -1;
const indicators = [
  document.getElementById("spanone"),
  document.getElementById("spantwo"),
  document.getElementById("spanthree"),
];

function updateIndicators() {
  indicators.forEach((indicator, index) => {
    if (index === num) {
      indicator.style.color = "black";
    } else {
      indicator.style.color = "#808080";
    }
  });
}

function slideshow_timer() {
  if (num === 2) {
    num = 0;
  } else {
    num++;
  }
  document.getElementById("top-img").src = pics_src[num];
  updateIndicators();
}

indicators.forEach((indicator, index) => {
  indicator.addEventListener("click", function () {
    num = index;
    document.getElementById("top-img").src = pics_src[num];
    updateIndicators();
  });
});

setInterval(slideshow_timer, 6000);

spanone,spantwo,spanthreeは、スライドショーの下の丸いやつたち(インジケーター)
setIntervalで、表示秒数指定
ifで、numの最大値を指定

実装例

StedTechOrganizationのHPで実装しています。

1
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?