/*! * EasyBanner * * Date: 2016-12-5 * Author: Leohowl */ (function( window ){ // var EasyBanner = window.EasyBanner; function EasyBanner(opts){ /** * * @param {viewWidth} 娴忚鍣ㄦ湁鏁堝搴 */ this.viewWidth = $(window).width(); /** * * @param {viewHeight} 娴忚鍣ㄦ湁鏁堥珮搴 */ this.viewHeight = $(window).height(); /** * * @param {wrap} 鐖剁骇瀹瑰櫒 */ this.wrap = $(opts.wrap) || null; /** * * @param {img} 瀛愬厓绱 * 瀛愬厓绱犲皢浠庣埗绾у鍣ㄤ腑鏌ユ壘锛屼笉蹇呴噸澶嶇埗绾у鍣ㄧ殑閫夋嫨鍣 * */ this.img = this.wrap.find(opts.img) || null; /** * @param {imgNum} 瀛愬厓绱犵殑鍥剧墖 * 鑷姩鐢熸垚锛屾棤闇€鎵嬪姩娣诲姞 */ this.imgNum = this.img.length; /** * * @param {speed} 瀛愬厓绱犲垏鎹㈤€熷害 */ this.speed = opts.speed || 1000; /** * * @param {interval} 瀛愬厓绱犲垏鎹㈤棿闅 */ this.interval = (opts.interval > this.speed + 500) ? opts.interval : 2000; /** * * @param {mode} 鍒囨崲妯″紡 * 鍖呮嫭"auto","click","passive" * auto妯″紡涓嬪皢鑷姩鍒囨崲 * click妯″紡涓嬶紝闇€瑕侀€氳繃鐐瑰嚮鈥滅劍鐐光€濇垨鈥滀笂涓€寮狅紝涓嬩竴寮犫€濇寜閽垏鎹 * passive妯″紡涓嬶紝鍙兘琚叾浠栧疄渚嬫帶鍒 */ this.mode = opts.mode || 'auto'; /** * * @param {control} 鎺у埗妯″紡 * 瀹炰緥寮€鍚椤规椂锛屽皢琚綔涓烘帶鍒舵満浣跨敤锛屾帶鍒跺叾浠杕ode涓簆assive鐨勫疄渚 */ this.control = opts.control || null; /** * * @param {method} 鍒囨崲椋庢牸 * fade涓烘笎鍏ユ笎鍑 * slide涓烘粦鍔 */ this.method = opts.method || 'fade'; /** * * @param {direction} 鍒囨崲鏂瑰悜 * 姝ら」涓簃ethod涓衡€渟lide鈥濈殑杩涗竴姝ラ厤缃紝鐢ㄤ簬璁剧疆婊戝姩鏂瑰悜 */ this.direction = opts.direction || 'right'; /** * * @param {displacement} slide鍒囨崲鐨勭浉瀵逛綅绉伙紝鍙栧€艰寖鍥翠负[0,1] */ this.displacement = opts.displacement || 0; /** * * @param {preImg} 涓婁竴涓樉绀虹殑鍏冪礌鐨刬ndex */ this.preImg = 0; /** * * @param {curImg} 褰撳墠鏄剧ず鐨勫厓绱犵殑index */ this.curImg = 0; /** * * @param {targetIndex} 鐩爣鏄剧ず鐨勫厓绱犵殑index */ this.targetIndex = 0; /** * * @param {actionAble} 鍒囨崲淇濇姢 */ this.actionAble = true; /** * * @param {wrapWidth} 鐖剁骇瀹瑰櫒瀹藉害 */ this.wrapWidth = this.wrap.width(); /** * * @param {wrapHeight} 鐖剁骇瀹瑰櫒楂樺害 */ this.wrapHeight = this.wrap.height(); /** * * @param {easing} 缂撳姩鏁堟灉 */ this.easing = opts.easing || ''; /** * * @param {initLeft} 瀛愬厓绱犲垵濮嬪寲浣嶇疆淇℃伅 */ this.initLeft = 0; /** * * @param {initLeft} 瀛愬厓绱犲垵濮嬪寲浣嶇疆淇℃伅 */ this.initTop = 0; /** * * @param {timer_autoClick} 鑷姩杞挱瀹氭椂鍣 */ this.timer_autoClick = null; /** * * @param {beforeActionCallback} 瀛愬厓绱犲垏鎹㈠墠鍥炶皟鍑芥暟 */ this.beforeActionCallback = opts.beforeActionCallback || function(){}; /** * * @param {afterActionCallback} 瀛愬厓绱犲垏鎹㈠悗鍥炶皟鍑芥暟 */ this.afterActionCallback = opts.afterActionCallback || function(){}; /** * * @param {delayBefore} 瀛愬厓绱犲垏鎹㈠墠鍥炶皟鍑芥暟鐨勬墽琛屽欢鏃 */ this.delayBefore = opts.delayBefore || 0; /** * * @param {delayAfter} 瀛愬厓绱犲垏鎹㈠悗鍥炶皟鍑芥暟鐨勬墽琛屽欢鏃 */ this.delayAfter = opts.delayAfter || 0; /** * * @param {leftArrow} 鈥濅笂涓€寮犫€滃垏鎹㈡寜閽 */ this.leftArrow = opts.leftArrow || false; /** * * @param {rightArrow} 鈥濅笅涓€寮犫€滃垏鎹㈡寜閽 */ this.rightArrow = opts.rightArrow || false; /** * * @param {arrow} 鍒囨崲鎸夐挳璁剧疆瀹屽叏楠岃瘉 */ this.arrow = !!(opts.leftArrow && opts.rightArrow); /** * * @param {btn} 鍒囨崲鐒︾偣 */ this.btn = opts.btn || false; // this.smallImg = opts.smallImg || false; // this.timeLine = opts.timeLine || false; //鏉傞」 // this.waterMark = opts.waterMark || false; this.init();//initialize } EasyBanner.prototype = { constructor : EasyBanner, init : function(){ //show Author if(this.waterMark){ // this.showDesign(); } //normal init this.wrap.css({ position: 'relative', overflow: 'hidden' }); this.img.css({//default init position : 'absolute', left: 0, top: 0 }); this.img.removeClass('EasyBannerActive'); this.img.eq(0).addClass('EasyBannerActive'); //绉诲姩绔椂闂村垵濮嬪寲 //special init switch(this.method){ case "fade": this.img.hide(); this.img.eq(0).show(); break; case "slide": //also execute with $(window).resize this.slidePositionInit(); break; default: break; } //bind var eb = this; $(window).resize(function(event) { eb.wrapWidth = eb.wrap.width(); eb.wrapHeight = eb.wrap.height(); //slide position init eb.slidePositionInit(); }); //鎵╁睍鍒濆鍖 this.pluginInit(); if(this.mode === 'auto'){ //褰撳墠涓鸿嚜鍔紱杞挱妯″紡 this.timer_autoClick = setTimeout(function(){eb.next();},this.interval); } }, slidePositionInit : function(){ //set origin position if(this.method !== 'slide'){ return false; } switch(this.direction){ case "right": this.initLeft = this.wrapWidth; this.initTop = 0; break; case "left": this.initLeft = -this.wrapWidth; this.initTop = 0; break; case "top": this.initLeft = 0; this.initTop = -this.wrapHeight; break; case "bottom": this.initLeft = 0; this.initTop = this.wrapHeight; break; } var eb = this; this.img.each(function(index, el) { $(this).css({ left: eb.initLeft+'px', top: eb.initTop+'px', 'z-index' : 101 }); }); //鍒濇浣嶇疆鍒濆鍖栨鏌 var activeIndex = 0; this.img.each(function(index, el) { if($(this).hasClass('EasyBannerActive')){ activeIndex = index; return null; } }); // console.log(activeIndex); this.img.eq(activeIndex).css({ 'z-index': 100, left: 0, top: 0 }); }, pluginInit : function(){ /** * 鐒︾偣鍒濆鍖栵紝鍦ㄨ鍔ㄦā寮忎笅锛屼笉浼氬垵濮嬪寲缈婚〉锛屼笉浼氫负鎸夐挳缁戝畾鐐瑰嚮浜嬩欢 * @type {EasyBanner} */ var eb = this; if(this.btn){ $(this.btn).removeClass('EasyBannerBtnActive'); $(this.btn).eq(0).addClass('EasyBannerBtnActive'); //濡傛灉涓嶆槸琚姩妯″紡鍒欎负鎸夐挳缁戝畾鐐瑰嚮浜嬩欢 if(this.mode !== "passive"){ $(this.btn).on('click',function(){ var index = $(this).index(); eb.jump(index,'click'); }); } } //缈婚〉鎸夐挳鍒濆鍖 if(this.arrow && this.mode !== 'passive'){ // $(this.leftArrow).on('click',function(){ //缈婚〉锛屼笂涓€寮 eb.prev(); }); $(this.rightArrow).on('click',function(){ //缈婚〉锛屼笅涓€寮 eb.next(); }) } }, action : function(targetIndex){ var currentIndex = this.curImg; //璁剧疆banner鐨刟ctive this.img.removeClass('EasyBannerActive'); this.img.eq(targetIndex).addClass('EasyBannerActive'); switch (this.method){ case "fade": fade(this); break; case "slide": slide(this); break; } if(this.mode === 'auto'){ //濡傛灉褰撳墠涓鸿嚜鍔ㄦā寮忥紝鍒欒嚜鍔ㄥ垏鎹 var eb = this; this.timer_autoClick = setTimeout(function(){eb.next();},this.interval); } var i = 1; function fade(eb){ eb.img.eq(eb.curImg).fadeOut(eb.speed); eb.img.eq(targetIndex).fadeIn(eb.speed,function(){ //鑷畾涔夊洖璋冨嚱鏁 eb.callback(eb.afterActionCallback,eb.delayAfter); //璋冪敤鎻掍欢 var pluginOpts = { type : 'after', targetIndex : targetIndex }; eb.plugin(pluginOpts); //璁剧疆璇锋眰鏍囧織浣嶅厑璁 eb.actionAble = true; //鍔ㄧ敾瀹屾垚锛岃缃浘鐗囨寚閽 eb.preImg = eb.curImg; eb.curImg = targetIndex; }); } function slide(eb){ eb.img.eq(eb.curImg).css('z-index', '100'); eb.img.eq(eb.curImg).animate({ left: -eb.initLeft*eb.displacement, top: -eb.initTop*eb.displacement },eb.speed,eb.easing,function(){ //閫€鍦哄浘鐗囨牱寮 $(this).css({ left: eb.initLeft, top: eb.initTop, 'z-index':101 }); //鑷畾涔夊洖璋冨嚱鏁 eb.callback(eb.afterActionCallback,eb.delayAfter); //璋冪敤鎻掍欢 var pluginOpts = { type : 'after', targetIndex : targetIndex }; eb.plugin(pluginOpts); //璁剧疆璇锋眰鏍囧織浣嶅厑璁 eb.actionAble = true; //鍔ㄧ敾瀹屾垚锛岃缃浘鐗囨寚閽 eb.preImg = eb.curImg; eb.curImg = targetIndex; }); eb.img.eq(targetIndex).animate({ left: 0, top: 0}, eb.speed, eb.easing); } }, changeAction : function(opts){ // var eb = this; //鍝嶅簲璇锋眰 if(!this.actionAble){ return; } //鍙傛暟澶勭悊 var targetIndex = opts.targetIndex; //鑻ョ洰鏍囧浘鐗囦负褰撳墠鍥剧墖锛屽垯蹇界暐 if(this.curImg === targetIndex){ return; } //鏆撮湶鍒囨崲鐨勭洰鏍囧浘鐗囷紝鏂逛究澶栭儴寮曠敤 this.targetIndex = targetIndex; //璁剧疆璇锋眰鏍囧織浣嶄负涓嶅厑璁 this.actionAble = false; //娓呴櫎寮€濮嬭繘琛岃疆鎾悗浜х敓瀹氭椂鍣 if(this.timer_autoClick){ clearTimeout(this.timer_autoClick); } //濡傛灉寮€鍚帶鍒舵ā寮忥紝杩欏彲浠ユ帶鍒跺叾浠栬疆鎾 if(this.control){ var i; for(i = 0; i < this.control.length; i++){ var run = function(i){ var obj = eb.control[i].object; var delay = eb.control[i].delay; var t_mode = setTimeout(function(){obj.jump(targetIndex,'control');},delay);//control 琛ㄧず鏄敱鎺у埗鏈烘帶鍒 }; run(i); } } //鎵ц鍓嶆墽琛屽洖璋冨嚱鏁 this.callback(this.beforeActionCallback, this.delayBefore); //璋冪敤鎻掍欢 var pluginOpts = { type : 'before', targetIndex : targetIndex }; this.plugin(pluginOpts); //閫夋嫨鍔ㄤ綔鏂规硶 switch (this.method){ case 'fade': this.action(targetIndex); break; case 'slide': this.action(targetIndex); } }, /** * 涓嬩竴寮狅紝浣垮綋鍓嶇殑curImg澧炲姞1锛岀劧鍚庤皟鐢ㄥ浘鐗囧垏鎹 */ next : function(){ //琚姩妯″紡涓紝浜︿笉鍙€氳繃璋冪敤鏂规硶缈婚〉 if(this.mode !== 'passive'){ this.changeAction({ targetIndex : (this.curImg + 1) % this.imgNum }); } }, /** * 涓婁竴寮狅紝浣垮綋鍓嶇殑curImg鍑忓皯1锛岀劧鍚庤皟鐢ㄥ浘鐗囧垏鎹 */ prev : function(){ if(this.mode !== 'passive'){ this.changeAction({ targetIndex : (this.curImg - 1 + this.imgNum) % this.imgNum }); } }, /** * * @param index 鍒囨崲鐨勭洰鏍囧浘鐗 * @param type 褰撳墠鎺у埗妯″紡 */ jump : function(index,type){ //type鐨勭被鍨嬶細click鏉ヨ嚜榧犳爣鐐瑰嚮锛宑ontrol鏉ヨ嚜鎺у埗鏈恒€ if(type === 'control' && this.mode !== 'passive'){ //琛ㄧず褰撳墠涓烘帶鍒舵満鎿嶄綔锛岃嫢娌℃湁鍚敤琚姩妯″紡鍒欎笉鎵ц锛屾帶鍒舵満璇搷浣滈槻姝 return; } this.changeAction({ targetIndex : index }); }, plugin : function(opts){ if(opts.type === 'before'){ if(this.btn){ //btn璁剧疆 $(this.btn).removeClass('EasyBannerBtnActive'); $(this.btn).eq(opts.targetIndex).addClass('EasyBannerBtnActive'); } } if(opts.type === 'after'){ } }, callback : function(c,delay){ var delay = delay || 0; if( typeof c === 'function'){ var t_callback = setTimeout(function () { c(); },delay); // console.log("callback"); } } }; window.EB = EasyBanner; window.EasyBanner = window.EB; })(window);