0
0

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でスライドショーで画像をクリックすることでリンクに飛ばすやつを作ります。

前回↓↓

スライドショー

前回のコード

//スライドショー        
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);

リンクに飛ばす

 document.getElementById("top-img").addEventListener("click", function() {

    if (num === -1 || num === 0){
        window.open("リンク1");
    }
    if (num === 1){
        window.open("リンク2");
    }
    if (num === 2){
        window.open("リンク3");
    }
});
0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?