SoFunction
Updated on 2025-04-10

JavaScript method to implement image carousel

This article describes the method of implementing image carousel in JavaScript. Share it for your reference. The details are as follows:

JQUERY is not used here, there is no transition effect, and the picture can be replaced by itself.

page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http:///TR/xhtml1/DTD/">
<html xmlns="http:///1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="/js/jquery/1.9.1/jquery-1.9."></script>
<style>
.img-div img{display:none;}
</style>
</head>
<body>
<div class="slide" >
<div class="img-div">
 <img src="" />
 <img src="" />
 <img src="" />
 <img src="" />
 <img src="" />
</div>
<div class="slide-btn">
<a href="#" class="hover">1</a>
<a href="#" class="hover">2</a>
<a href="#" class="hover">3</a>
<a href="#" class="hover">4</a>
<a href="#" class="hover">5</a>
</div>
</div>
<script type="text/javascript">
var zBase=
{
 $id:function(id){return (id);},
 $tagName:function(tagName,obj){return ((obj?obj:document).getElementsByTagName(tagName));},
 $c:function(clsN,obj)
 {
 var tag=this.$tagName('*'),reg=new RegExp('(^|\\s)'+clsN+'(\\s|$)'),arr=[];
 for(var i=0;i<;i++)
 {
  if((tag[i].className))
  {
  (tag[i]); 
  } 
 }
 return arr;
 },
 $add:function(obj,clsN)
 {
 var reg=new RegExp('(^|\\s)'+clsN+'(\\s|$)');
 if(!())
 {
  +=' '+clsN; 
 } 
 },
 $remove:function(obj,clsN)
 {
 var cla=;
 var reg='/|\\s*'+clsN+'\\b/g';
 =cla?(eval(reg),''):''; 
 },
 css:function(obj,attr,value)
 {
 if(value)
 {
  [attr]=value; 
 } 
 else
 {
  return typeof !="undefined"?(obj,null)[attr]:[attr]; 
 }
 },
 easing:
 {
  liner:function(t,b,c,d){return c*t/d+b},
  easeOut:function(t,b,c,d){return -c*((t=t/d-1)*t*t*t-1)+b}
 },
 config:
 {
  index:0,
  auto:true,
  direct:'left' 
 },
 init:function()
 {
  =this.$id('slide');
  this.img_div=this.$c('img-div')[0];
  this.slide_btn=this.$tagName('a',this.$c('slide-btn')[0]);
  this.img_arr=this.$tagName('img',this.img_div);
  if(){();}
  ();
 },
 animate:function(obj,attr,val)
 {
  var d=1000;
  if(obj[attr+"timer"]){clearInterval(obj[attr+"timer"])};
  var start= parseInt((obj,attr));
  var space=val-start,st=(new Date().getTime()),m=space>0?'cell':'floor';
  obj[attr+'timer']=setInterval(function(){
  var t=(new Date().getTime()-st);
  if(t<d)
  {
   (obj,attr,Math[m](["easeOut"](t,start,space,d))+"px");
  }
  else
  {
   clearInterval(obj[attr+'timer']);
   (obj,attr,top+space+"px");
  }  
  },20);
 },
 hover:function()
  {
  for(var i=0;i<this.slide_btn.length;i++)
  {
   this.slide_btn[i].index=i;
   this.slide_btn[i].onmouseover=function()
   {
   if()
   {
    clearInterval(); 
   } 
   =;
   //(this.slide_btn);
   for(var j=0;j<zBase.slide_btn.length;j++)
   {
    zBase.$remove(zBase.slide_btn[j],'hover');
   }
   zBase.$add(zBase.slide_btn[],'hover');
   (zBase.img_div,,-*1000);
   }
   this.slide_btn[i].onmouseout=function()
   {
   (); 
   }
  }
  },
  play:function(){
   = setInterval(function(){
   var tags=zBase.$tagName('img',this.img_div);
   //zBase.$tagName('img',this.img_div)[].="block";
   for(var i =0;i<;i++)
   {
   if(==i)
   {
   zBase.$tagName('img',this.img_div)[].="block";
   } 
   else
   {
   zBase.$tagName('img',this.img_div)[i].="none"; 
   }
   }
   ++;
   if(>=zBase.img_arr.length) =0;
   (zBase.img_div,,-*500);
   for(var j=0;j<zBase.slide_btn.length;j++){
   zBase.$remove(zBase.slide_btn[j],'hover') ;
   }
   zBase.$add(zBase.slide_btn[],'hover');
  },3000)
  }
}
 ();
</script>
 </body>
</html>

I hope this article will be helpful to everyone's JavaScript programming.