jquery實(shí)現(xiàn)簡(jiǎn)單實(shí)用的輪播器
WEB開發(fā)經(jīng)常實(shí)用到一種情況,即某個(gè)容器內(nèi)的各項(xiàng)輪流循環(huán)播放顯示,同時(shí)有相應(yīng)的導(dǎo)航條提示,因?yàn)檫@個(gè)在很多地方可以使用,而且功能很相似的,所以寫一個(gè)這樣的播放功能,共享一下,需要注意的是這個(gè)需要jQuery的支持, 這個(gè)自己網(wǎng)上搜索下載即可,下面總結(jié)出來(lái)如下,直接看代碼,
一、把如下保存為一個(gè)獨(dú)立的文件 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頁(yè)面中引入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>
注意:因?yàn)楸M量簡(jiǎn)單,所以需要建立適當(dāng)格式的HTML,主要要求如下,注意顏色部分,
//A, id = containerIDNavCon和 id = containerIDItemCon 中的連接 A 元素的數(shù)量應(yīng)該相等;
//B, 導(dǎo)航容器的 ID 構(gòu)成應(yīng)為主容器 ID 加上 NavCon,如上 containerIDNavCon;
//C, 導(dǎo)航容器中的每個(gè) A 元素的 ID 構(gòu)成為,itemNav 加上以0開始的遞增數(shù)字序列,如上面的綠色部分;
//D, 顯示項(xiàng)目容器內(nèi)的每個(gè) A 元素的 ID 構(gòu)成為,item 加上以0開始的遞增數(shù)字序列,如上面的紫色部分;
3.在WEB頁(yè)面中的加載事件onload,初始化和啟用該輪播功能,例如:
window.onload=function(){
itemPlayerApp.init('containerID',3000,300,200);
itemPlayerApp.active();
}
三、如下一個(gè)例子
假如所有文件都放在一個(gè)文件夾里,
<!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>項(xiàng)目循環(huán)輪播功能</p> <script language="javascript" type="text/javascript"> window.onload=function(){ itemPlayerApp.init('playerBox',3000,300,200); itemPlayerApp.appActive(); } </script> </body> </html>
提示: jQuery.js 的文件請(qǐng)網(wǎng)上自己下載。
在使用到的時(shí)候直接使用即可。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Easyui使用Dialog行內(nèi)按鈕布局的實(shí)例
下面小編就為大家?guī)?lái)一篇Easyui使用Dialog行內(nèi)按鈕布局的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-07-07jquery實(shí)現(xiàn)倒計(jì)時(shí)小應(yīng)用
這篇文章主要為大家詳細(xì)介紹了jquery實(shí)現(xiàn)倒計(jì)時(shí)小應(yīng)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09jquery下拉select控件操作方法分享(jquery操作select)
這篇文章主要介紹了jquery下拉select控件操作方法分享(jquery操作select),需要的朋友可以參考下2014-03-03jquery刪除table當(dāng)前行的實(shí)例代碼
下面小編就為大家?guī)?lái)一篇jquery刪除table當(dāng)前行的實(shí)例代碼。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2016-10-10jQuery仿Flash上下翻動(dòng)的中英文導(dǎo)航菜單實(shí)例
這篇文章主要介紹了jQuery仿Flash上下翻動(dòng)的中英文導(dǎo)航菜單,實(shí)例分析了jQuery實(shí)現(xiàn)Flash反動(dòng)特效的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03jQuery+css last-child實(shí)現(xiàn)選擇最后一個(gè)子元素操作示例
這篇文章主要介紹了jQuery+css last-child實(shí)現(xiàn)選擇最后一個(gè)子元素操作,結(jié)合實(shí)例形式分析了jQuery結(jié)合css進(jìn)行頁(yè)面元素選擇與樣式修改相關(guān)操作技巧,需要的朋友可以參考下2018-12-12