网站首页 > 基础教程 正文
jQuery封装构造函数版轮播图
function Tab(){
this.$box=$('#box');
this.$innerBox=this.$box.find('.boxInner');
this.$aDiv=null;//jQuery中没有DOM映射
this.$aImg=null;
this.$ul=this.$box.find('ul');
this.$aLi=null;
this.$left=this.$box.find('.left');
this.$right=this.$box.find('.right');
this.data=null;
this.timer=null;
this.n=0;
this.init();}
Tab.prototype={
constructor:Tab,
init:function(){
var _this=this;
//1.获取数据
this.getData();
//2.绑定数据
this.bind();
//3.延迟加载
this.lazyImg();
//4.图片渐隐渐现
clearInterval(this.timer);
this.timer=setInterval(function(){
_this.autoMove();
},2000)
//5.焦点自动轮播
//6.移入停止,移出继续
this.overout();
//7.点击焦点手动切换
this.handleChange();
//8.点击左右按钮进行切换
this.leftRight();
},
getData:function(){
var _this=this;
$.ajax({
type:'get',
url:'json/data.txt',
async:false,
dataType:'json',
success:function(val){
_this.data=val;
}
})
},
bind:function(){
var strDiv='';
var strLi='';
$.each(this.data,function(index,item){
strDiv+='<div><img src="" realImg="'+item.imgSrc+'" alt=""></div>';
strLi+=index==0?'<li class="on"></li>':'<li></li>';
})
this.$innerBox.html(strDiv);
this.$ul.html(strLi);
},
lazyImg:function(){
var _this=this;
this.$aDiv=this.$innerBox.children('div');
this.$aImg=this.$innerBox.find('img');
this.$aLi=this.$ul.find('li');
$.each(this.$aImg,function(index,item){
var tmpImg=new Image;
tmpImg.src=item.getAttribute('realImg');
tmpImg.onload=function(){
item.src=this.src;
tmpImg=null;
var $div1=_this.$aDiv.eq(0);
$div1.css('zIndex',1).fadeIn();
}})},
autoMove:function(){
if(this.n>=this.$aDiv.length-1){
this.n=-1;}
this.n++;
this.setBanner();},
setBanner:function(){
var _this=this;
$.each(this.$aDiv,function(index,item){
if(index==_this.n){
// $(item).css('zIndex',1).fadeIn().siblings().fadeOut(300);
$(item).css('zIndex',1).siblings().css('zIndex',0);
$(item).fadeIn(500,function(){
$(this).siblings().fadeOut()});}});
this.bannerTip();},
bannerTip:function(){
var _this=this;
$.each(this.$aLi,function(index,item){
item.className=index==_this.n?'on':null;})},
overout:function(){
var _this=this;
this.$box.mouseover(function(){
clearInterval(_this.timer);
_this.$left.show();
_this.$right.show();})
this.$box.mouseout(function(){
_this.timer=setInterval(function(){
_this.autoMove();
},2000)
_this.$left.hide();
_this.$right.hide();
})},
handleChange:function(){
var _this=this;
this.$aLi.click(function(){
_this.n=$(this).index();
_this.setBanner();
}) },
leftRight:function(){
var _this=this;
this.$right.click(function(){
_this.autoMove();
});
this.$left.click(function(){
if(_this.n<=0){
_this.n=_this.$aLi.length;}
_this.n--;
_this.setBanner();
})}}
猜你喜欢
- 2024-11-06 河北最新公布!这1070辆车请注意,你们被盯上了!
- 2024-11-06 炉石通灵学园十职业最新卡组大全,快收藏
- 2024-11-06 带你五分钟了解jsoup教程 jsoup使用教程
- 2024-11-06 多行文本控件(富文本框)怎样自动填写?
- 2024-11-06 每一个人都要做出自己的选择,选择不同终点同样不同
- 2024-11-06 web开发之-jquery的基本使用(2) jquery使用教程
- 2024-11-06 99版冠号印制顺序表 99版各面值冠号
- 2024-11-06 魔兽世界怀旧服:mc与bwl高手进阶输出手法与职业心得理解
- 2024-11-06 卓越管理者必备的3个能力:洞察力,共鸣力和人格力
- 2024-11-06 问题:Jquery的eq()方法返回被选元素中带有指定索引号的元素
- 最近发表
- 标签列表
-
- jsp (69)
- pythonlist (60)
- gitpush (78)
- gitreset (66)
- python字典 (67)
- dockercp (63)
- gitclone命令 (63)
- dockersave (62)
- linux命令大全 (65)
- mysql教程 (60)
- pythonif (68)
- pythonifelse (59)
- deletesql (62)
- c++模板 (62)
- linuxgzip (68)
- 字符串连接 (73)
- nginx配置文件详解 (61)
- html标签 (69)
- c++初始化列表 (64)
- mysqlinnodbmyisam区别 (63)
- arraylistadd (66)
- console.table (62)
- mysqldatesub函数 (63)
- window10java环境变量设置 (66)
- c++虚函数和纯虚函数的区别 (66)