搜索
首页web前端js教程jQuery简单实用的轮播器实现方法

jQuery简单实用的轮播器实现方法

Jan 10, 2018 pm 03:03 PM
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){      
   $(&#39;#itemNav&#39;+j).css({&#39;background-color&#39;:&#39;#333333&#39;,&#39;color&#39;:&#39;#FFFFFF&#39;});     
   $(&#39;#item&#39;+j).show(&#39;fast&#39;);    
   }else{   
   $(&#39;#itemNav&#39;+j).css({&#39;background-color&#39;:&#39;#CCCCCC&#39;,&#39;color&#39;:&#39;#000000&#39;}); 
   $(&#39;#item&#39;+j).hide(&#39;fast&#39;);  
   } 
  }   
 }, 
 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 == &#39;undefined&#39; || refContainerId == null || refContainerId == &#39;&#39;){ 
   return; 
  }else{ 
   cid = $.trim(refContainerId); 
  }  
  if(refWidth == &#39;undefined&#39; || refWidth == null || refWidth == &#39;&#39;){ 
   w = 300; 
  }else{ 
   w = parseInt(refWidth); 
  }  
  if(refHeight == &#39;undefined&#39; || refHeight == null || refHeight == &#39;&#39;){ 
   h = 200; 
  }else{ 
   h = parseInt(refHeight); 
  }  
   
  $(&#39;#&#39; + cid).css({"position":"relative",&#39;width&#39;:w,&#39;height&#39;:h,&#39;overflow&#39;:&#39;hidden&#39;}); 
  $(&#39;#&#39; + cid + "NavCon").css({&#39;color&#39;:&#39;#333333&#39;,&#39;height&#39;:&#39;26px&#39;,&#39;position&#39;:&#39;absolute&#39;,&#39;width&#39;:&#39;95%&#39;,&#39;left&#39;:&#39;0&#39;,&#39;bottom&#39;:&#39;3px&#39;,&#39;text-align&#39;:&#39;right&#39;,&#39;display&#39;:&#39;block&#39;}); 
  var t = 0; 
  if(intervalTime == &#39;undefined&#39; || 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({&#39;padding&#39;:&#39;2px 5px&#39;,&#39;margin-right&#39;:&#39;2px&#39;,&#39;background-color&#39;:&#39;#CCCCCC&#39;}); 
   if(i == 0){ 
    $(this).css({&#39;background-color&#39;:&#39;#333333&#39;,&#39;color&#39;:&#39;#FFFFFF&#39;}); 
   }     
   $(this).mouseover(function(){ 
   _this.playData=i+&#39;xstop&#39;; 
   _this.appTab(i);  
   }); 
   $(this).mouseout(function(){ 
   _this.playData=i+&#39;xplay&#39;; 
   _this.appTab(i); 
   }); 
  }); 
 } 
};

二、如何使用:

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

 

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

<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>

注意:因为尽量简单,所以需要建立适当格式的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> 
<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>

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

相关推荐:

利用H5实现一个轮播器(触屏版)的实例教程

具体介绍如何用H5实现触屏版的轮播器

教你一步步用jQyery实现轮播器

以上是jQuery简单实用的轮播器实现方法的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
JavaScript数据类型:浏览器和nodejs之间是否有区别?JavaScript数据类型:浏览器和nodejs之间是否有区别?May 14, 2025 am 12:15 AM

JavaScript核心数据类型在浏览器和Node.js中一致,但处理方式和额外类型有所不同。1)全局对象在浏览器中为window,在Node.js中为global。2)Node.js独有Buffer对象,用于处理二进制数据。3)性能和时间处理在两者间也有差异,需根据环境调整代码。

JavaScript评论:使用//和 / * * / * / * /JavaScript评论:使用//和 / * * / * / * /May 13, 2025 pm 03:49 PM

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python vs. JavaScript:开发人员的比较分析Python vs. JavaScript:开发人员的比较分析May 09, 2025 am 12:22 AM

Python和JavaScript的主要区别在于类型系统和应用场景。1.Python使用动态类型,适合科学计算和数据分析。2.JavaScript采用弱类型,广泛用于前端和全栈开发。两者在异步编程和性能优化上各有优势,选择时应根据项目需求决定。

Python vs. JavaScript:选择合适的工具Python vs. JavaScript:选择合适的工具May 08, 2025 am 12:10 AM

选择Python还是JavaScript取决于项目类型:1)数据科学和自动化任务选择Python;2)前端和全栈开发选择JavaScript。Python因其在数据处理和自动化方面的强大库而备受青睐,而JavaScript则因其在网页交互和全栈开发中的优势而不可或缺。

Python和JavaScript:了解每个的优势Python和JavaScript:了解每个的优势May 06, 2025 am 12:15 AM

Python和JavaScript各有优势,选择取决于项目需求和个人偏好。1.Python易学,语法简洁,适用于数据科学和后端开发,但执行速度较慢。2.JavaScript在前端开发中无处不在,异步编程能力强,Node.js使其适用于全栈开发,但语法可能复杂且易出错。

JavaScript的核心:它是在C还是C上构建的?JavaScript的核心:它是在C还是C上构建的?May 05, 2025 am 12:07 AM

javascriptisnotbuiltoncorc; saninterpretedlanguagethatrunsonenginesoftenwritteninc.1)javascriptwasdesignedAsalightweight,解释edganguageforwebbrowsers.2)Enginesevolvedfromsimpleterterterpretpreterterterpretertestojitcompilerers,典型地提示。

JavaScript应用程序:从前端到后端JavaScript应用程序:从前端到后端May 04, 2025 am 12:12 AM

JavaScript可用于前端和后端开发。前端通过DOM操作增强用户体验,后端通过Node.js处理服务器任务。1.前端示例:改变网页文本内容。2.后端示例:创建Node.js服务器。

Python vs. JavaScript:您应该学到哪种语言?Python vs. JavaScript:您应该学到哪种语言?May 03, 2025 am 12:10 AM

选择Python还是JavaScript应基于职业发展、学习曲线和生态系统:1)职业发展:Python适合数据科学和后端开发,JavaScript适合前端和全栈开发。2)学习曲线:Python语法简洁,适合初学者;JavaScript语法灵活。3)生态系统:Python有丰富的科学计算库,JavaScript有强大的前端框架。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境