;(function($){ //上下切换 var SwitchPlugin = function(options){ var self = this; this.options = { autoSlider:false, //默认不自动切换 direction:'horizontal' //默认水平切换 }; this.flag = false; //记录是否到达最后 //追加替换用户的参数 $.extend(this.options,options); //垂直切换还是水平切换 if(this.options.direction == 'vertical'){ this.options.switch_bottom.on('click',function(){ self.switchAnimate('bottom'); }); this.options.switch_top.on('click',function(){ self.switchAnimate('top'); }); }else if(this.options.direction == 'horizontal'){ this.setSwitchUlWidth(); this.options.switch_prev.on('click',function(){ self.switchAnimate('left'); }); this.options.switch_next.on('click',function(){ self.switchAnimate('right'); }); } //是否是自动切换 if(this.options.autoSlider){ setInterval(function(){ if(self.flag){ self.switchAnimate('top'); }else{ self.switchAnimate('bottom'); } },3000); } } SwitchPlugin.prototype = { //执行切换动画 switchAnimate:function(align){ var top = null; var left = null; if(align == 'bottom'){ top = this.getSwitchUlTop() - this.getSwitchLiHeight(); if(Math.abs(top) > this.getSwitchUlHeight() - this.getSwitchLiHeight()){ this.flag = true; return false; } if(!this.options.switch_ul.is(':animated')){ this.options.switch_ul.animate({ top:top }); } }else if (align == 'top'){ top = this.getSwitchUlTop() + this.getSwitchLiHeight(); if(top > 0){ this.flag = false; return false; } if(!this.options.switch_ul.is(':animated')){ this.options.switch_ul.animate({ top:top }); } }else if(align == 'left'){ left = this.getSwitchUlLeft() + this.getSwitchLiWidth(); if(left > 0){ return false; } if(!this.options.switch_ul.is(':animated')){ this.options.switch_ul.animate({ left:left }); } }else if(align == 'right'){ left = this.getSwitchUlLeft() - this.getSwitchLiWidth(); if(Math.abs(left) > this.getSwitchUlWidth() - (this.getSwitchLiWidth() * 4 )){ return false; } if(!this.options.switch_ul.is(':animated')){ this.options.switch_ul.animate({ left:left }); } } }, //设置总宽度 setSwitchUlWidth:function(){ this.options.switch_ul.width(this.options.switch_ul.find('li').size() * (this.getSwitchLiWidth() + 10)); }, //获取一小块内容的宽度 getSwitchLiWidth:function(){ return parseInt(this.options.switch_ul.find('li').outerWidth(true)); }, //获取一小块内容的高度 getSwitchLiHeight:function(){ return parseInt(this.options.switch_ul.find('li').height()); }, //获取总的宽度 getSwitchUlWidth:function(){ return parseInt(this.options.switch_ul.width()); }, //获取总的高度 getSwitchUlHeight:function(){ return parseInt(this.options.switch_ul.height()); }, //获取内容的top值 getSwitchUlTop:function(){ return parseInt(this.options.switch_ul.css('top')); }, //获取内容的left值 getSwitchUlLeft:function(){ return parseInt(this.options.switch_ul.css('left')); } }; window['SwitchPlugin'] = SwitchPlugin; })(jQuery);