Realization of left right sliding carousel map by jquery

  • 2021-07-26 06:09:16
  • OfStack

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


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
<title> Picture carousel jq( Switch left and right )</title>
<style>
 *{margin: 0;padding:0; }
 ul{list-style: none;}
 .banner{width: 600px;height: 300px;border: 2px solid #ccc;margin: 100px auto;position: relative;overflow: hidden;z-index: 1;}
 .img{position: absolute;top: 0;left: 0;}
 .des{position: absolute;bottom: 0;left: 0;z-index: -2;background: #ccc}
 .des li{float: left;width: 600px;}
 .img li{float: left;}
 .num{position: absolute;bottom: 10px;width: 100%;text-align: center;font-size: 0;}
 .num li{width: 10px;height: 10px;background:rgba(0,0,0,0.5);display: block;border-radius: 100%;display: inline-block;margin: 0 5px;cursor: pointer;}
 .btn{display: none;}
 .btn span{display: block;width: 50px;height: 100px;background: rgba(0,0,0,0.6);color: #fff;font-size: 40px;line-height: 100px;text-align: center;cursor:pointer;}
 .btn .prev{position: absolute;left: 0;top: 50%;margin-top: -50px;}
 .btn .next{position: absolute;right: 0;top: 50%;margin-top: -50px;}
 .num .active{background-color: #fff;}
 .hide{display: none;}
 </style>
</head>
<body>
 <div class="banner">
 <ul class="img">
 <li><a href="#"><img width="600" height="300" src="img/1.jpg" alt=" No. 1 1 A picture "></a></li>
 <li><a href="#"><img width="600" height="300" src="img/1.jpg" alt=" No. 1 2 A picture "></a></li>
 <li><a href="#"><img width="600" height="300" src="img/1.jpg" alt=" No. 1 3 A picture "></a></li>
 <li><a href="#"><img width="600" height="300" src="img/1.jpg" alt=" No. 1 4 A picture "></a></li>
 <li><a href="#"><img width="600" height="300" src="img/1.jpg" alt=" No. 1 5 A picture "></a></li>
 </ul>
 <ul class="num"></ul>
 <ul class="des">
 <li> No. 1 1 A </li>
 <li> No. 1 2 A </li>
 <li> No. 1 3 A </li>
 <li> No. 1 4 A </li>
 <li> No. 1 5 A </li>
 <li> No. 1 1 A </li>
 </ul>
 <div class="btn">
 <span class="prev"><</span>
 <span class="next">></span>
 </div>
 
</div>
 
<script>
 
 $(function(){
 
 var i=0;
 var timer=null;
 for (var j = 0; j < $('.img li').length; j++) { // Create a dot 
 $('.num').append('<li></li>')
 }
 $('.num li').first().addClass('active'); // Give the first 1 Dot Add Style 
 
 var firstimg=$('.img li').first().clone(); // Duplicate number 1 A picture 
 $('.img').append(firstimg).width($('.img li').length*($('.img img').width())); 
 // No. 1 1 Put the picture at the end 1 Picture, set the ul The width of is the number of pictures * Picture width 
 $('.des').width($('.img li').length*($('.img img').width()));
 
 
 //  Under 1 Buttons 
 $('.next').click(function(){
 i++;
 if (i==$('.img li').length) {
 i=1; // This is not i=0
 $('.img').css({left:0}); // Ensure seamless carousel and set left Value 
 };
 
 $('.img').stop().animate({left:-i*600},300);
 if (i==$('.img li').length-1) { // Set dot indication 
 $('.num li').eq(0).addClass('active').siblings().removeClass('active');
 $('.des li').eq(0).removeClass('hide').siblings().addClass('hide');
 }else{
 $('.num li').eq(i).addClass('active').siblings().removeClass('active');
 $('.des li').eq(i).removeClass('hide').siblings().addClass('hide');
 }
 
 })
 
 //  Upper 1 Buttons 
 $('.prev').click(function(){
 i--;
 if (i==-1) {
 i=$('.img li').length-2;
 $('.img').css({left:-($('.img li').length-1)*600});
 }
 $('.img').stop().animate({left:-i*600},300);
 $('.num li').eq(i).addClass('active').siblings().removeClass('active');
 $('.des li').eq(i).removeClass('hide').siblings().addClass('hide');
 })
 
 // Set the display and hiding of buttons 
 $('.banner').hover(function(){
 $('.btn').show();
 },function(){
 $('.btn').hide();
 })
 
 // Drop the mouse into the dot 
 $('.num li').mouseover(function(){
 var _index=$(this).index();
 $('.img').stop().animate({left:-_index*600},150);
 $('.num li').eq(_index).addClass('active').siblings().removeClass('active');
 $('.des li').eq(_index).removeClass('hide').siblings().addClass('hide');
 })
 
 // Automatic timer playback 
 timer=setInterval(function(){
 i++;
 if (i==$('.img li').length) {
 i=1;
 $('.img').css({left:0});
 };
 
 $('.img').stop().animate({left:-i*600},300);
 if (i==$('.img li').length-1) { 
 $('.num li').eq(0).addClass('active').siblings().removeClass('active');
 $('.des li').eq(0).removeClass('hide').siblings().addClass('hide');
 }else{
 $('.num li').eq(i).addClass('active').siblings().removeClass('active');
 $('.des li').eq(i).removeClass('hide').siblings().addClass('hide');
 }
 },1000)
 
 // Move the mouse in, pause auto-play, move out, start auto-play 
 $('.banner').hover(function(){ 
 clearInterval(timer);
 },function(){
 timer=setInterval(function(){
 i++;
 if (i==$('.img li').length) {
 i=1;
 $('.img').css({left:0});
 };
 
 $('.img').stop().animate({left:-i*600},300);
 if (i==$('.img li').length-1) { 
 $('.num li').eq(0).addClass('active').siblings().removeClass('active');
 $('.des li').eq(0).removeClass('hide').siblings().addClass('hide');
 }else{
 $('.num li').eq(i).addClass('active').siblings().removeClass('active');
 $('.des li').eq(i).removeClass('hide').siblings().addClass('hide');
 }
 },1000)
 })
 
 })
</script>

</body>
</html>

For more topics on sliding effects, please click the link below to view:

Summary of javascript sliding operations

jquery Slide Operation Summary


Related articles: