/** * created by wenyu on 15/4/16. */ (function($){ function reslider(){ this.defaults = { speed:1000,//设置轮播的高度 delay:5000,//设置轮播的延迟时间 imgcount:4,//设置轮播的图片数 dots:true,//设置轮播的序号点 autoplay:true//设置轮播是否自动播放 } this.count = 0;//轮播计数器 this.timer = null;//轮播计时器 } var _repro = reslider.prototype; _repro.init = function(){ this.doevent(); this.setbackground(); settimeout(this.autoplay(),this.defaults.delay); if(this.defaults.dots){ this.dots() } this.seturl(this.count); } _repro.setbackground = function(){ $('.slider-block').eq(0).css({ 'z-index':'98', 'opacity':'1' }) $('.slider-direction').css({ 'z-index':parseint($('.slider-block').css('z-index'))+1 }) } _repro.doevent = function(){ var _self = this; $('.slider-direction-next').on('click',function(){ _self.next() }); $('.slider-direction-prev').on('click',function(){ _self.prev() }) } _repro.nextnormal = function(position){ var _self = this; _self.seturl(position); $('.slider-block').eq(position).animate({ 'opacity':'1' },_self.defaults.speed) $('.slider-block').eq(position).siblings('.slider-block').animate({ 'opacity':'0' },_self.defaults.speed) } _repro.next = function(){ var _self = this; _self.nextnormal(_self.count+1) _self.dotsactive($('.slider-dots ul li').eq(_self.count+1)); if(_self.count>=(_self.defaults.imgcount-1)) { _self.count = 0; _self.dotsactive($('.slider-dots ul li').eq(_self.count)); $('.slider-block').eq(0).animate({ 'opacity':'1' },1000) } else if(_self.count>=0 && (_self.defaults.imgcount-1)){ _self.count++; } } _repro.prev = function(){ var _self = this; $('.slider-block').eq(_self.count).prev().animate({ 'opacity':'1' },_self.defaults.speed) _self.dotsactive($('.slider-dots ul li').eq(_self.count-1)); if(_self.count>0){ $('.slider-block').eq(_self.count).animate({ 'opacity':'0' },_self.defaults.speed) _self.count--; }else{ _self.count = 0; } console.log(_self.count) } _repro.autoplay = function(){ var _self = this; if(_self.defaults.autoplay){ _self.timer=setinterval(function(){ _self.next() },_self.defaults.delay) } else{ clearinterval(_self.timer); } } _repro.dots = function(){ var _self = this; for(var i = 0 ;i') } $('.slider-dots').css({ 'z-index':parseint($('.slider-block').css('z-index'))+1 }) $('.slider-dots ul li').eq(0).addclass('active'); $('.slider-dots ul li').on('click',function(){ _self.dotsactive($(this)); _self.nextnormal(($(this).index())) _self.count = $(this).index() }) } _repro.dotsactive = function(elements){ elements.addclass('active').siblings().removeclass('active') } _repro.seturl = function(index){ var $url = $('.slider-block').eq(index).attr('data-url'); $('.slider-block').eq(index).css({ 'background-image':'url('+$url+')' }) } //扩展jquery.prototype $.fn.reslider = function(options){ var reslider = new reslider(); $.extend(reslider.defaults,options) reslider.init() } })(jquery)