js achieve image carousel effect

The main use of the setInterval () method:

  <img id='img'  src='img/轮播01.png'>

 

  <script>

    let img = document.querySlector('img');

    // initialize the timer

    let timer = null;

    let n = 0;

    timer = setInterval(()=>{

      n++

      // If n is greater than the rotation cycle to the number of pictures, then re-start from the first picture carousel

      if (n> the number of image rotation) {

        n = 1;

      }else{

        img.src = "img/轮播0" + n + ".png";

    },1000)

    

Guess you like

Origin www.cnblogs.com/blogXie/p/11834140.html