This article mainly introduces in detail the simple and practical carousel production method in jquery. It has certain reference value. Interested friends can refer to it. I hope it can help everyone.
WEB development is often used in a situation where the items in a container are played and displayed in a loop, and there are corresponding navigation bar prompts, because this can be used in many places, and the functions are very similar. So write a playback function like this and share it. It should be noted that this requires the support of jQuery. You can search and download it online. The following is summarized as follows. Look at the code directly.
1. Save the following as An independent file 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); }); }); } };
2. How to use:
1. Introduce the jQery file and this itemPlayerApp.js file into the web page you need to use, for example:
2. Create an HTML file in the following format
<p id="containerID"> <p 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> </p> <p 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> </p> </p>
Note: Because it is as simple as possible, it is necessary to create appropriately formatted HTML. The main requirements are as follows. Pay attention to the color part,
//A, the connection A element in id = containerIDNavCon and id = containerIDItemCon The number should be equal;
//B, The ID composition of the navigation container should be the main container ID plus NavCon, as above containerIDNavCon;
//C, The ID composition of each A element in the navigation container is, itemNav Add an increasing number sequence starting from 0, as shown in the green part above;
//D, display the ID of each A element in the item container, which is composed of item plus an increasing number sequence starting with 0, as shown above The purple part;
3. In the loading event onload in the WEB page, initialize and enable the carousel function, for example:
window.onload=function(){
itemPlayerApp.init(' containerID',3000,300,200);
itemPlayerApp.active();
}
3. The following example
If all files are placed in a folder,
<!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> <p id="playerBox" class="columnLeft01 box02"> <p 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> </p> <p 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> </p> </p> <p> </p> <p>项目循环轮播功能</p> <script language="javascript" type="text/javascript"> window.onload=function(){ itemPlayerApp.init('playerBox',3000,300,200); itemPlayerApp.appActive(); } </script> </body> </html>
Tip: Please download the jQuery.js file online.
Just use it directly when needed.
Related recommendations:
An example tutorial of using H5 to implement a carousel (touch screen version)
Detailed introduction How to use H5 to implement a touch-screen carousel
Teach you step by step how to implement a carousel using jQyery
The above is the detailed content of jQuery simple and practical carousel implementation method. For more information, please follow other related articles on the PHP Chinese website!