Native js and css realize picture carousel effect

  • 2021-07-18 06:29:01
  • OfStack

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


<!DOCTYPE HTML> 
<html lang="zh-CN"> 
 
<head> 
  <meta charset="utf-8"> 
  <title> Picture carousel </title> 
   
  <style> 
    #box { 
      width:506px; 
      height:306px; 
      margin: 20px auto; 
      border:3px solid black; 
      position:relative; 
      background-color:orange; 
      overflow: hidden; 
      /*overflow: hidden;*/ 
    } 
    .pic { 
      position: absolute; 
      width:500px; 
      height:300px; 
      line-height: 300px; 
      text-align: center; 
      font-size: 100px; 
      color:white; 
      bottom:0; 
    } 
    .red { 
      background-color:red; 
    } 
    .green { 
      background-color:green; 
    } 
    .blue { 
      background-color:blue; 
    } 
    .orange{ 
      background-color: orange; 
    } 
    .move { 
      bottom:300px; 
      transition:bottom 3s; /*  Set the time spent moving pictures */ 
    } 
  </style> 
</head> 
 
<body> 
  <div id="box"> 
    <div id="pic1" class="pic red">1</div> 
    <div id="pic2" class="pic green">2</div> 
    <div id="pic3" class="pic blue">3</div> 
    <div id="pic3" class="pic orange">4</div> 
  </div> 
   
   
  <script> 
    window.addEventListener('load',function(){ 
      var pics = document.getElementsByClassName('pic'); 
       
      // For each pic Element settings z-index Value of  
      for(let i=0;i<pics.length;i++){ 
        pics[i].style.zIndex = pics.length-i; 
      } 
       
      // Function to loop back pictures  
      var loopPics = (function(){ 
        var index=0; 
        return function(pics,delay){ 
          var recall = function(pic){ 
            // Add to the picture move Class, calling the css Adj. transition Property to play mobile animation  
            pic.className += ' move'; 
            setTimeout(function(){ 
              // Cancel the picture's move Class, the picture returns to its original position  
              pic.className=pic.className.replace(' move',''); 
              // Change the stacking order of picture groups. The outermost picture is placed at the bottom, and the other pictures are moved outward in turn  
              for(let i=0;i<pics.length;i++){ 
                if(pics[i].style.zIndex==pics.length){ 
                  pics[i].style.zIndex=1; 
                } else { 
                  pics[i].style.zIndex=pics[i].style.zIndex*1+1; 
                } 
              } 
              index++; 
              if(index==pics.length) index=0; 
              recall(pics[index]); 
            },delay); 
          }; 
          recall(pics[index]); 
        }; 
      })(); 
      // Call the function and play it in a loop. delay Time needs to be greater than or equal to css Picture movement time set in animation  
      loopPics(pics,4000); 
    }); 
 
  </script> 
</body> 
 
</html>


Related articles: