JS realizes the carousel effect of merry go round pictures

  • 2021-07-12 04:47:56
  • OfStack

In this paper, we share the specific code of js picture carousel for your reference. The specific contents are as follows

Main html code:


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <link rel="stylesheet" type="text/css" href="css/css.css">
  <script type="text/javascript" src="js/animate.js"></script>
</head>
<body>
  <div class="w-wrap" id="js_wrap">
    <div class="wrap-slide" id="wrap_slide">
      <ul>
        <li><a href="#"><img src="images/slidepic1.jpg" alt=""></a></li>
        <li><a href="#"><img src="images/slidepic2.jpg" alt=""></a></li>
        <li><a href="#"><img src="images/slidepic3.jpg" alt=""></a></li>
        <li><a href="#"><img src="images/slidepic4.jpg" alt=""></a></li>
        <li><a href="#"><img src="images/slidepic5.jpg" alt=""></a></li>
        <!-- <li><a href="#"><img src="images/slidepic6.jpg" alt=""></a></li> -->
      </ul>
      <div class="wrap-slide-arrow" id="wrap_slide_arrow">
        <a href="javascript:;" class="prev"></a>
        <a href="javascript:;" class="next"></a>
      </div>
    </div>
  </div>
  <script>
    function $(id){ return document.getElementById(id);}
    var js_wrap = $("js_wrap"); 
    var wrap_slide = $("wrap_slide"); 
    var wrap_slide_arrow = $("wrap_slide_arrow");
    var lis = wrap_slide.children[0].children;


    var json = [
      {
        //1
        width: 400,
        top: 20,
        left: 50,
        opacity: 20,
        z: 2
      },
      {
        //2
        width: 600,
        top: 70,
        left: 0,
        opacity: 80,
        z: 3
      },
      {
        //3
        width: 800,
        top: 100,
        left: 200,
        opacity: 100,
        z: 4
      },
      {
        //4
        width: 600,
        top: 70,
        left: 600,
        opacity: 80,
        z: 3
      },
      {
        //5
        width: 400,
        top: 20,
        left: 750,
        opacity: 20,
        z: 2
      }/*,
      {
        //6
        width: 300,
        top: 10,
        left: 400,
        opacity: 10,
        z: 1
      }*/
    ]
    change(); // Will each picture according to json Spreading layers 
    var jieliu = true;
    // Two button click events 
    var as = wrap_slide_arrow.children;
    for(var k in as){
      as[k].onclick = function(){
        if(this.className == "prev"){
          /*alert(" Left button ");*/
          // All pictures rotate counterclockwise, which is equivalent to the picture not moving. json The first part of 1 Delete and insert to the end 1 A 
          if(jieliu == true){
            change(false);
            jieliu = false;
          }

        }else if(this.className == "next"){
          /*alert(" Right button ");*/
          // All the pictures rotate clockwise, which is equivalent to the pictures not moving. json The last of 1 Delete and insert the 1 A 
          if(jieliu == true){
            change(true);
            jieliu = false;
          }
        }
      }
    }
    function change(flag){
      if(flag){
        // All the pictures rotate clockwise, which is equivalent to the pictures not moving. json The last of 1 Delete and insert the 1 A       
        json.unshift(json.pop());
      }else{
        // All pictures rotate counterclockwise, which is equivalent to the picture not moving. json The first part of 1 Delete and insert to the end 1 A 
        json.push(json.shift());
      }
      for(var k in json){
        animate(lis[k],{
          width: json[k].width,
          top: json[k].top,
          left: json[k].left,
          opacity: json[k].opacity,
          zIndex: json[k].z
        },function(){ jieliu = true;}); // When the animation is finished, the callback function is executed, and the throttle is set to true
      }
    }
    var timer = null;
    timer = setInterval(autoPlay,2000);
    function autoPlay(){
      if(jieliu == true){
        change(true);
        jieliu = false;
      }
    }

    js_wrap.onmouseover = function(){
      clearInterval(timer);
      animate(wrap_slide_arrow,{opacity:100});
    }
    js_wrap.onmouseout = function(){
      clearInterval(timer);
      timer = setInterval(autoPlay,2000);
      animate(wrap_slide_arrow,{opacity:0});
    }

    /*js_wrap.onmouseover = function(){
      animate(wrap_slide_arrow,{opacity:100});
    }
    js_wrap.onmouseout = function(){
      animate(wrap_slide_arrow,{opacity:0});
    }*/
  </script>
</body>
</html> 

Main css code:


/* Initialization  reset*/
blockquote,body,button,dd,dl,dt,fieldset,form,h1,h2,h3,h4,h5,h6,hr,input,legend,li,ol,p,pre,td,textarea,th,ul{margin:0;padding:0}
body,button,input,select,textarea{font:12px/1.5 "Microsoft YaHei", " Microsoft Yahei ", SimSun, " Song Style ", sans-serif;color: #666;}
ol,ul{list-style:none}
a{text-decoration:none}
fieldset,img{border:0;vertical-align:top;}
a,input,button,select,textarea{outline:none;}
a,button{cursor:pointer;}

.w-wrap{
  width: 1200px;
  margin: 100px auto;
}
.wrap-slide{
  position: relative;
}
.wrap-slide li{
  position: absolute;
  left: 200px;
  top: 0;
}
.wrap-slide li img{
  width: 100%;
}
.wrap-slide-arrow{
  opacity: 0;
  position: relative;
}
.prev,.next{
  width: 76px;
  height: 112px;
  position: absolute;
  top: 50%;
  margin-top: -56px;
  background: url(../images/prev.png) no-repeat;
}
.next{
  right: 0;
  background: url(../images/next.png) no-repeat;
}

Related articles: