SoFunction
Updated on 2025-03-01

jquery realizes left and right sliding carousel diagram

This article shares the specific code of the jquery left and right sliding carousel for your reference. The specific content is as follows

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="js/jquery-1.10."></script>
<title>Picture carouseljq(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">
 &lt;li&gt;&lt;a href="#"><img width="600" height="300" src="img/" alt="Picture 1"></a></li> &lt;li&gt;&lt;a href="#"><img width="600" height="300" src="img/" alt="Picture 2"></a></li> &lt;li&gt;&lt;a href="#"><img width="600" height="300" src="img/" alt="3rd picture"></a></li> &lt;li&gt;&lt;a href="#"><img width="600" height="300" src="img/" alt="4th picture"></a></li> &lt;li&gt;&lt;a href="#"><img width="600" height="300" src="img/" alt="Picture 5"></a></li> &lt;/ul&gt;
 &lt;ul class="num"&gt;&lt;/ul&gt;
 &lt;ul class="des"&gt;
 &lt;li&gt;The first&lt;/li&gt;
 &lt;li&gt;The second one&lt;/li&gt;
 &lt;li&gt;Third&lt;/li&gt;
 &lt;li&gt;The fourth&lt;/li&gt;
 &lt;li&gt;The fifth&lt;/li&gt;
 &lt;li&gt;The first&lt;/li&gt;
 &lt;/ul&gt;
 &lt;div class="btn"&gt;
 &lt;span class="prev"&gt;&lt;&lt;/span&gt;
 &lt;span class="next"&gt;&gt;&lt;/span&gt;
 &lt;/div&gt;
 
&lt;/div&gt;
 
&lt;script&gt;
 
 $(function(){
 
 var i=0;
 var timer=null;
 for (var j = 0; j &lt; $('.img li').length; j++) { //Create dots $('.num').append('&lt;li&gt;&lt;/li&gt;')
 }
 $('.num li').first().addClass('active'); //Add a style to the first dot 
 var firstimg=$('.img li').first().clone(); //Copy the first picture $('.img').append(firstimg).width($('.img li').length*($('.img img').width())); 
 //After the first picture is placed in the last picture, set the width of ul to the number of pictures * picture width $('.des').width($('.img li').length*($('.img img').width()));
 
 
 // Next button $('.next').click(function(){
 i++;
 if (i==$('.img li').length) {
 i=1; //This is not i=0 $('.img').css({left:0}); // Ensure seamless carousel, set left value };
 
 $('.img').stop().animate({left:-i*600},300);
 if (i==$('.img li').length-1) { //Set small dot indicator $('.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');
 }
 
 })
 
 // Previous button $('.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 hide buttons $('.banner').hover(function(){
 $('.btn').show();
 },function(){
 $('.btn').hide();
 })
 
 //The mouse is inserted 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');
 })
 
 //Timer automatically plays 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 automatic playback, move out, and start automatic playback $('.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)
 })
 
 })
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;

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

Summary of javascript sliding operations

jquery sliding operation summary

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.