欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jquery实现简单实用的轮播器

程序员文章站 2022-07-17 17:50:11
web开发经常实用到一种情况,即某个容器内的各项轮流循环播放显示,同时有相应的导航条提示,因为这个在很多地方可以使用,而且功能很相似的,所以写一个这样的播放功能,共享一下,...

web开发经常实用到一种情况,即某个容器内的各项轮流循环播放显示,同时有相应的导航条提示,因为这个在很多地方可以使用,而且功能很相似的,所以写一个这样的播放功能,共享一下,需要注意的是这个需要jquery的支持, 这个自己网上搜索下载即可,下面总结出来如下,直接看代码,

一、把如下保存为一个独立的文件 itemplayerapp.js

//attend: this need jquery.js support 
var itemplayerapp={ 
 author:'shenzhennba', 
 version:'v1.0', 
 appmaxnum:0, 
 playdata:'1xplay', 
 playerid:"", 
 speed:3000, 
 appplay:function(){  
  var f=this.playdata.tolowercase().split('x'); 
  if(f[1]=='play'){ 
   var i; 
   try{i=parseint(f[0]);}catch(e){i=0;} 
   if(i>=this.appmaxnum){i=0;}    
   this.apptab(i);   
   this.playdata=(++i)+"xplay"; 
  }   
 }, 
 apptab:function(tabindex){ 
  var k,j; 
  try{k=parseint(tabindex);}catch(e){k=0;}   
  for(j=0;j<this.appmaxnum;j++){    
   if(k==j){      
   $('#itemnav'+j).css({'background-color':'#333333','color':'#ffffff'});     
   $('#item'+j).show('fast');    
   }else{   
   $('#itemnav'+j).css({'background-color':'#cccccc','color':'#000000'}); 
   $('#item'+j).hide('fast');  
   } 
  }   
 }, 
 appactive:function(){ 
  var _this = this; 
  this.playerid = setinterval(function(){ _this.appplay(); },this.speed); 
 }, 
 init:function(refcontainerid,intervaltime,refwidth,refheight){  
  var cid = "";  
  var w = 300; 
  var h = 200; 
  if(refcontainerid == 'undefined' || refcontainerid == null || refcontainerid == ''){ 
   return; 
  }else{ 
   cid = $.trim(refcontainerid); 
  }  
  if(refwidth == 'undefined' || refwidth == null || refwidth == ''){ 
   w = 300; 
  }else{ 
   w = parseint(refwidth); 
  }  
  if(refheight == 'undefined' || refheight == null || refheight == ''){ 
   h = 200; 
  }else{ 
   h = parseint(refheight); 
  }  
   
  $('#' + cid).css({"position":"relative",'width':w,'height':h,'overflow':'hidden'}); 
  $('#' + cid + "navcon").css({'color':'#333333','height':'26px','position':'absolute','width':'95%','left':'0','bottom':'3px','text-align':'right','display':'block'}); 
  var t = 0; 
  if(intervaltime == 'undefined' || intervaltime == null){ 
   t = 3000; 
  }else{ 
   try{ t = parseint(intervaltime);}catch(e){ t = 3000;} 
  } 
  this.speed = t; 
  var navlist = "#" + cid + "navcon a"; 
  this.appmaxnum = $(navlist).size(); 
  if(0 == this.appmaxnum){ return; } 
  var _this = this; 
  $(navlist).each(function(i){ 
   $(this).css({'padding':'2px 5px','margin-right':'2px','background-color':'#cccccc'}); 
   if(i == 0){ 
    $(this).css({'background-color':'#333333','color':'#ffffff'}); 
   }     
   $(this).mouseover(function(){ 
   _this.playdata=i+'xstop'; 
   _this.apptab(i);  
   }); 
   $(this).mouseout(function(){ 
   _this.playdata=i+'xplay'; 
   _this.apptab(i); 
   }); 
  }); 
 } 
}; 

二、如何使用:

1.需要使用的web页面中引入jqery文件和本 itemplayerapp.js 文件,例如:

<script language="javascript" src="xpath/itemplayer.js"></script> 

2.建立如下格式的html文件

<div id="containerid">

<div id="containeridnavcon">
<a id="itemnav0" class="item1" href="#">1</a>
<a id="itemnav1" class="item1" href="#">2</a>
<a id="itemnav2" class="item1" href="#">3</a>
</div>
<div id="containeriditemcon">
<a id="item0" href="#"><img src="img/pic0.jpg" width="300" height="200"></a>
<a id="item1" href="#"><img src="img/pic1.jpg" width="300" height="200"></a>
<a id="item2" href="#"><img src="img/pic2.jpg" width="300" height="200"></a>
</div>
</div>

注意:因为尽量简单,所以需要建立适当格式的html,主要要求如下,注意颜色部分,

//a, id = containeridnavcon和 id = containeriditemcon 中的连接 a 元素的数量应该相等;
//b, 导航容器的 id 构成应为主容器 id 加上 navcon,如上 containeridnavcon;
//c, 导航容器中的每个 a 元素的 id 构成为,itemnav 加上以0开始的递增数字序列,如上面的绿色部分;
//d, 显示项目容器内的每个 a 元素的 id 构成为,item 加上以0开始的递增数字序列,如上面的紫色部分;

3.在web页面中的加载事件onload,初始化和启用该轮播功能,例如:
window.onload=function(){
itemplayerapp.init('containerid',3000,300,200);
itemplayerapp.active();
}

三、如下一个例子

假如所有文件都放在一个文件夹里,

<!doctype html public "-//w3c//dtd html 4.01 transitional//en" 
"http://www.w3.org/tr/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="content-type" content="text/html; charset=utf-8"> 
<title>test</title> 
<script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script> 
<script language="javascript" type="text/javascript" src="itemplayerapp.js"></script> 
<style type="text/css"> 
*{font-family:"宋体",verdana,arial; font-size:12px;color:#000000;} 
#playerbox{font-family:"宋体",verdana,arial; font-size:12px;color:#000000;} 
</style> 
</head> 
<body> 
<div id="playerbox" class="columnleft01 box02"> 
<div id="playerboxnavcon"> 
<a id="itemnav0" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >1</a> 
<a id="itemnav1" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >2</a> 
<a id="itemnav2" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >3</a> 
</div> 
<div id="playerboxitemcon"> 
<a id="item0" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="http://www.baidu.com/img/shouye_b5486898c692066bd2cbaeda86d74448.gif" width="100%" height="200" border="0"></a> 
<a id="item1" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="http://csdnimg.cn/www/images/csdnindex_logo.gif" width="100%" height="200" border="0"></a> 
<a id="item2" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="http://avatar.csdn.net/5/1/9/1_shenzhennba.jpg" width="100%" height="200" border="0"></a> 
</div> 
</div> 
<p> </p> 
<p>项目循环轮播功能</p> 
<script language="javascript" type="text/javascript"> 
window.onload=function(){ 
itemplayerapp.init('playerbox',3000,300,200); 
itemplayerapp.appactive(); 
} 
</script> 
</body> 
</html> 

提示: jquery.js 的文件请网上自己下载。
在使用到的时候直接使用即可。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。