Realization of millet carousel map by JS

  • 2021-08-28 07:10:58
  • OfStack

Native JS imitation millet carousel map (the latest version is easy to understand), for your reference, the specific contents are as follows

The main achievable effects of this content:

Automatic carousel You can click on the top one and the bottom one to switch pictures Click on the small dot at the bottom right to switch pictures

Code section:

H5:


<div class="wrap">
    <ul class="list">
      <li class="item active"><img src="img/001.jpg" alt=""></li>
      <li class="item"><img src="img/002.jpg"" alt=""></li>
      <li class="item"><img src="img/001.jpg" alt=""></li>
      <li class="item"><img src="img/002.jpg" alt=""></li>
      <li class="item"><img src="img/001.jpg" alt=""></li>
    </ul>
    <!--  Small dot  -->
    <ul class="pointList">
      <li class="point active" data-index="0"></li>
      <li class="point" data-index="1"></li>
      <li class="point"data-index="2"></li>
      <li class="point"data-index="3"></li>
      <li class="point"data-index="4"></li>
    </ul>
    <button type="button" class="btn" id="goPre"><</button>
    <button type="button" class="btn" id="goNext">></button>
</div>

CSS section:


<style>
    .wrap {
      width: 800px;
      height: 400px;
      position: relative;
    }

    .list {
      width: 800px;
      height: 400px;
      list-style: none;
      position: relative;
      padding-left: 0px;
    }

    .item {
      position: absolute;
      width: 100%;
      height: 100%;
      color: white;
      font-size: 50px;
      opacity: 0.6;
      transform: all .8s;
    }
    .item img{
      width: 800px;
      height: 400px;
    }
    .btn {
      width: 50px;
      height: 100px;
      position: absolute;
      top: 150px;
      z-index: 100;
    }

    #goPre {
      left: 0px;
    }

    #goNext {
      right: 0px;
    }

    .item.active {
      opacity: 1;
      z-index: 10;
    }

    .pointList{
      padding-left: 0;
      list-style: none;
      position: absolute;
      right: 20px;
      bottom: 20px;
      z-index: 1000;
    }
    .point{
      width: 10px;
      height: 10px;
      background-color: rgba(0,0,0,0.4);
      border-radius: 50%;
      float: left;
      margin-right: 16px;
      border-style: solid;
      border-width: 2px;
      border-color: rgba(255,255,255, 0.6);
      cursor: pointer;
    }
    .point.active{
      background-color: rgba(255,255,255,0.4);
    }
</style>

JS Part


<script>
    // Get Node 
    var items = document.getElementsByClassName('item')// Picture 
    var goPreBtn = document.getElementById('goPre');
    var goNextBtn = document.getElementById('goNext');
    // Acquisition point 
    var points=document.getElementsByClassName('point');

   var time=0;// Timer picture jump parameter 

    var index = 0; // Indicates which picture is on display 
    // What point can you show 

    var clearActive=function(){
      for(var i=0;i<items.length;i++){
        items[i].className='item';
        points[i].className='point';
      }
    }

    var goIndex=function(){
      clearActive();
      items[index].className='item active';
      points[index].className='point active';
    }

    var goNext=function(){
      if(index<4){
        index++;
      }else{
        index=0;
      }
      goIndex();
    }
    var goPre=function(){
      if(index==0){
        index=4;
      }else{
        index--;
      }
      goIndex();
    }

    // Click under 1 Zhang Switch Picture 
    goNextBtn.addEventListener('click' ,function(){
      goNext();
      time=0;
    })
  // Click on 1 Zhang carries out switching pictures 
    goPreBtn.addEventListener('click' ,function(){
      goPre();
      time=0;
    })

   for(var i=0;i<points.length;i++){
    points[i].addEventListener('click',function(){
      var pointIndex=this.getAttribute('data-index');
      index=pointIndex;
      goIndex();
      time=0;
    })
   }
   
  // Automatic carousel timer 
  setInterval(function(){
    time++;
    if(time==20){
      goNext();
      time=0;
    } 
  },100)
</script>

Related articles: