js realizes fade in and fade out carousel switching function

  • 2021-07-10 18:52:04
  • OfStack

Without saying much, please look at the code:


<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Document</title>
 <style>
   *{
    margin:0;
    padding:0;
   }
   /* Minimum width   So that the picture can be adaptively centered */
   .warp{
    min-width:900px;
    width:100%;
    height: 600px;
    margin:0 auto;
   }
   #banner{
    position: relative;
   }
   ul{
    position: relative;
    width:100%;
    height:600px;
    overflow: hidden;
   }
   ul li{
    display: none;
    position: absolute;
    top:0;
    left: 0;
    width: 100%;
    height: 600px;
    text-align: center;
   }
   .cur{
    position:absolute;
    bottom:20px;
    text-align: center;
    width: 100%;
    height: 40px;
    line-height: 40px;
   }
   .cur span{
    display: inline-block;
    width: 20px;
    height: 20px;
    background:#000;
   }
   .cur span.active{
    background:blue;
   }
   .btn{
    position: absolute;
    top:50%;
    width: 50px;
    height: 100px;
    line-height: 100px;
    text-align: center;
    margin-top:-50px;
    color:#fff;
    font-size:18px;
    background: rgba(0,0,0,0.9)
   }
   .btnLeft{
    left:0;
   }
   .btnRight{
    right:0;
   }
   img{
    width: 900px;
    height: 600px;
   }
 </style>
</head>
<body>
<div class="warp">
 <div class="lunbo" id="banner">
  <ul>
   <li style="background:red;display: block"> Full screen gradient   Image adaptive centering </li>
   <li style="background:green"> Full screen gradient   Image adaptive centering </li>
   <li style="background:yellow"> Full screen gradient   Image adaptive centering </li>
  </ul>
   <!--  If two buttons are outside the picture   Just move the button outside and put it in js Add button to move to clear timer event  -->
  <div class="btn btnLeft"><</div>
  <div class="btn btnRight">></div>
 </div>
</div>
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
<script>
  var num=0;
  var btnLeft=$(".btnLeft");
  var btnRight=$(".btnRight");
  var Id=$("#banner");
  var oUl=Id.find("ul");
  var oLi=oUl.find("li");
  var oLiLen=oLi.length;
  var curHtml="<div class='cur'></div>"; 
  Id.append(curHtml);
  var oCur=$(".cur");
  //  Dynamic addition of dots 
  for(var i=0;i<oLiLen;i++){
    var curA="<span></span>"
    oCur.append(curA);
  }
  var oCurSpan=oCur.find("span");
  var oCurS=oCur.find("span:first");
  oCurS.addClass('active')
  //  Automatic carousel 
  var t=setInterval(function(){
    num++;
    lunbo();
  },3000);
  //  Move to Carousel Clear Timer 
  Id.hover(function(){
    clearInterval(t)
  },function(){
    t=setInterval(function(){
      num++;
      lunbo();
    },3000);
  });
  //  Left arrow button 
  btnLeft.on("click",function(){
    num--;
    lunbo();
  })  
   // Right arrow button  
  btnRight.on("click",function(){
    num++;
    lunbo();
  })
  function lunbo(){
    if(num==oLiLen){
      num=0;
    }
    oLi.eq(num).fadeIn().siblings().fadeOut();
    oCurSpan.eq(num).addClass('active').siblings().removeClass('active');
  }
  lunbo();
</script>
</body>
</html>

Related articles: