jquery如何实现轮播器
这篇文章将为大家详细讲解有关jquery如何实现轮播器,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
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" class="lazy" data-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 class="lazy" data-src="img/pic0.jpg" width="300" height="200"></a>
<a id="item1" href="#"><img class="lazy" data-src="img/pic1.jpg" width="300" height="200"></a>
<a id="item2" href="#"><img class="lazy" data-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" class="lazy" data-src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script language="javascript" type="text/javascript" class="lazy" data-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 class="lazy" data-src="/file/upload/202210/19/enu3uulnefp.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 class="lazy" data-src="/file/upload/202210/19/i0h0jdtogih.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 class="lazy" data-src="/file/upload/202210/19/y0e1vgzwdcv.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 的文件请网上自己下载。
关于“jquery如何实现轮播器”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341