;(function($){ //创建类 var carousel = function(con){ var self = this; this.con = con; this.setdirect(); this.conitem = con.find("ul.list"); this.prev = con.find(".prev-btn"); this.next = con.find(".next-btn"); this.conitems = con.find("li"); this.conitemfirst = this.conitems.first(); this.conitemlast = this.conitems.last(); this.flag = true; //默认配置参数 this.settings = { width:900, //幻灯片的宽度 height:4110, //幻灯片的高度 postwidth:658, //第一帧的宽度 postheight:411, //第一帧的高度 scale:0.8, speed:500, verticalalign:'center', autoplay:false, delay:1000 } $.extend(this.settings,this.getsetting()); this.setsettingvalue(); this.setpostother(); this.next.on("click",function(){ if(self.flag){ self.flag = false; self.rotate("left"); } }); this.prev.on("click",function(){ if(self.flag){ self.flag = false; self.rotate("right"); } }); if(this.settings.autoplay){ this.autoplay(); this.con.hover(function(){ window.clearinterval(self.timer); },function(){ self.autoplay(); }); } } //原型方法 carousel.prototype = { //设置上一张和下一张大小 setdirect:function(){ var prev = $("
").addclass("content-btn prev-btn").append($("").attr("src","img/left.png").addclass("btn-img")); this.con.prepend(prev); var next = $("
").addclass("content-btn next-btn").append($("").attr("src","img/right.png").addclass("btn-img")); this.con.append(next); }, //自动播放函数 autoplay:function(){ var self = this; this.timer = window.setinterval(function(){ self.next.click(); },this.settings.delay); }, //旋转函数 rotate:function(dir){ var _this = this; var zindex = [] if(dir === "left"){ this.conitems.each(function(){ var prev = $(this).prev().get(0)?$(this).prev():_this.conitemlast; zindex.push(prev.css("zindex")); $(this).animate({ width:prev.width(), height:prev.height(), top:prev.css("top"), left:prev.css("left"), opacity:prev.css("opacity") },_this.settings.speed,function(){ _this.flag = true; }); }); this.conitems.each(function(i){ $(this).css({zindex:zindex[i]}) }); }else if(dir === "right"){ this.conitems.each(function(){ var next = $(this).next().get(0)?$(this).next():_this.conitemfirst; zindex.push(next.css("zindex")); $(this).animate({ width:next.width(), height:next.height(), top:next.css("top"), left:next.css("left"), opacity:next.css("opacity") },_this.settings.speed,function(){ _this.flag = true; }); }); this.conitems.each(function(i){ $(this).css({zindex:zindex[i]}) }); } }, //设置剩余帧位置关系 setpostother:function(){ var self =this, sliceitem = this.conitems.slice(1), slicelength = sliceitem.length, rightitem = sliceitem.slice(0,slicelength/2), leftitem = sliceitem.slice(slicelength/2), level = math.floor(slicelength/2), llevel = level, rw = this.settings.postwidth, rh = this.settings.postheight, gap = (this.settings.width - this.settings.postwidth)/2/level; rightitem.each(function(i){ rw = rw*self.settings.scale; rh = rh*self.settings.scale; var j = i; $(this).css({ zindex:--level, width:rw, height:rh, left:(self.settings.width + self.settings.postwidth)/2 + gap*(++i) - rw, top:self.setverticalalign(rh), opacity:1/(++j) }); }); var lw = rightitem.last().width(), lh = rightitem.last().height(); leftitem.each(function(i){ $(this).css({ zindex:level++, width:lw, height:lh, left:gap*i, top:self.setverticalalign(lh), opacity:1/llevel-- }); lw = lw/self.settings.scale; lh = lh/self.settings.scale; }); }, //设置对齐方式 setverticalalign:function(h){ if(this.settings.verticalalign === "middle"){ return (this.settings.height - h)/2; }else if(this.settings.verticalalign === "top"){ return 0; }else if(this.settings.verticalalign === "bottom"){ return this.settings.height - h; }else { return (this.settings.height - h)/2; } }, //设置配置参数控制幻灯片显示 setsettingvalue:function(){ this.con.css({ width:this.settings.width, height:this.settings.height }); this.conitem.css({ width:this.settings.width, height:this.settings.height }); var w = (this.settings.width - this.settings.postwidth)/2; this.prev.css({ width:w, height:this.settings.height, zindex:math.ceil((this.conitems.length)/2) }); this.prev.find("img").css({ width:w, height:w, }); this.next.css({ width:w, height:this.settings.height, zindex:math.ceil((this.conitems.length)/2) }); this.next.find("img").css({ width:w, height:w }); this.conitemfirst.css({ top:0, left:w, width:this.settings.postwidth, height:this.settings.postheight, zindex:this.conitems.length }); }, //接收配置参数 getsetting:function(){ var con = this.con.attr("data-setting"); if(con && con!==""){ return $.parsejson(con); }else { return ""; } } } //初始化 carousel.init = function(carousel){ var _this = this; carousel.each(function(){ new _this($(this)); }); } window.carousel = carousel; })(jquery)