锘?** * 娆㈣繋浣跨敤 SliderPlay 骞荤伅鐗囥€庣劍鐐瑰浘銆忔彃浠 * * jQuery SliderPlay plugin **/ ;(function(window, $) { $.fn.sliderPlay = function(options) { options = $.extend({ speed: 700, //鍔ㄧ敾鏁堟灉鎸佺画鏃堕棿 timeout: 3000, //骞荤伅闂撮殧鏃堕棿 moveType: 'opacity', // randomMove: 闅忔満杩愬姩锛宮oveTo: 骞荤伅鍒囨崲杩愬姩 锛宱pacity: 娣″叆娣″嚭 direction: 'left', //left , top 鍦╩oveType 涓簃oveTo鐨勬椂鍊?姝ゅ弬鏁扮敓鏁 mouseEvent: 'click', //浜嬩欢绫诲瀷锛岄粯璁ゆ槸 click ,mouseover isShowTitle: true, //鏄惁寮€濮嬫爣棰?寮€鍚垯閲囩敤img鏍囩alt鐨勬枃瀛 isShowBtn: true //鏄惁鏄剧ず鎸夐挳 }, options); var oSliderPlayBox = this; var aSliderPlayImg = oSliderPlayBox.find('img'); var aSliderPlayItem = oSliderPlayBox.children(); var size = aSliderPlayImg.length; var iSpeed = options.speed; var iTimeout = options.timeout; var sMoveType = options.moveType; var sDirection = options.direction; var sMouseEvent = options.mouseEvent; var bIsShowTitle = options.isShowTitle; var bIsShowBtn = options.isShowBtn; var iImgWidth = aSliderPlayImg.width(); var iImgHeight = aSliderPlayImg.height(); var iCurIndex = 0; var iPrevIndex = 0; var iBtnIndex = 0; var oAutoTimer = null; var oTimer = null; var flag = false; var autoPlay = function() {}; var move = function() {}; //妫€娴嬩紶鍏ョ殑鍙傛暟鏄惁鏈夎. if(sMouseEvent != 'mouseover' && sMouseEvent != 'click') { sMouseEvent = 'click'; } if(sDirection != 'left' && sDirection != 'top') { sMouseEvent = 'left'; } if(sMoveType != 'randomMove' && sMoveType != 'moveTo' && sMoveType != 'opacity') { sMoveType = 'moveTo'; } //璁剧疆榛樿鏍峰紡 !function setDefaultStyle() { oSliderPlayBox.addClass('sliderPlay'); oSliderPlayBox.css({width: iImgWidth, height: iImgHeight , visibility: 'visible'}); }(); //濡傛灉鏄剧ず鎸夐挳灏辩敓鎴愭寜閽 if(bIsShowBtn == true) { var sBtnHtml = '
1'; for(var i = 2; i <= size; i++) { sBtnHtml += ''+i+''; }; sBtnHtml += '
'; this.append(sBtnHtml); var aSliderPlayBtn = this.find('.sliderPlay-btn').find('span'); } //濡傛灉鏄剧ずtitle 灏辩敓鎴恡itle,榛樿鏄剧ず鐨則itle鏂囧瓧涓虹涓€寮犲浘鐗囩殑alt; if(bIsShowTitle) { this.append('
'+aSliderPlayImg.attr('alt')+'
'); var oSliderPlayTitle = this.find('.sliderPlay-title'); } //鎵ц杩愬姩鍑芥暟 switch(sMoveType) { case 'moveTo' : setMoveTo(); break; case 'opacity' : setOptity(); break; case 'randomMove' : setRandomMove(); break; } //娣诲姞浜嬩欢 function addEvent(move, autoPlay) { if(bIsShowBtn) { if(sMouseEvent == 'click') { aSliderPlayBtn.on('click', function(e) { e.preventDefault(); oAutoTimer && clearInterval(oAutoTimer); iBtnIndex = iCurIndex = $(this).index(); move() }) } else { aSliderPlayBtn.mouseenter(function() { oAutoTimer && clearInterval(oAutoTimer); iBtnIndex = iCurIndex = $(this).index(); oTimer = setTimeout(move, 200) }).mouseleave(function(){ clearTimeout(oTimer) }) } } oSliderPlayBox.hover(function() { oAutoTimer && clearInterval(oAutoTimer) }, function() { oAutoTimer= setInterval(autoPlay, iTimeout) }) }; //璁剧疆鎸夐挳鐨刢lass鍜屾爣棰樻枃瀛 function setAttr(index) { oSliderPlayTitle &&銆€oSliderPlayTitle.html( aSliderPlayImg.eq(index).attr('alt') ); aSliderPlayBtn && aSliderPlayBtn.removeClass('current').eq(index).addClass('current'); }; //璁剧疆骞荤伅鍒囨崲杩愬姩 function setMoveTo() { var style = ''; if(sDirection == 'left') { style = 'width:' + (size+1) * iImgWidth +'px'; } else { style = 'height:' + (size+1) * iImgHeight + 'px'; } oSliderPlayBox.css('overflow', 'hidden'); aSliderPlayItem.wrapAll('
'); aSliderPlayImg.css('float', 'left'); var oSliderPlayMain = oSliderPlayBox.find('.sliderPlayMain'); var data = {}; move = function () { if(sDirection == 'left') { data['left'] = -iImgWidth * iCurIndex } else { data['top'] = -iImgHeight * iCurIndex } oSliderPlayMain.animate(data, iSpeed, function() { if(flag) { if(sDirection == 'left') { oSliderPlayMain.css('left', 0); } else { oSliderPlayMain.css('top', 0); } iCurIndex = 0; oSliderPlayMain.children().last().remove(); flag = false; } }); setAttr(iBtnIndex) }; autoPlay = function () { iBtnIndex = ++iCurIndex; if(iCurIndex == size) { aSliderPlayItem.eq(0).clone().appendTo(oSliderPlayMain); flag = true; iBtnIndex = 0; } move() }; addEvent(move, autoPlay); oAutoTimer = setInterval(autoPlay, iTimeout) }; //璁剧疆娣″叆娣″嚭杩愬姩 function setOptity() { aSliderPlayImg.css({position: 'absolute', left: 0, top: 0, display: 'none'}).first().show(); move = function () { setAttr(iCurIndex); aSliderPlayImg.stop().fadeOut(iSpeed).eq(iCurIndex).fadeIn(iSpeed); iPrevIndex = iCurIndex }; autoPlay = function (){ iCurIndex++; if(iCurIndex == size) { iCurIndex = 0; } move() }; addEvent(move, autoPlay); oAutoTimer = setInterval(autoPlay, iTimeout) }; //璁剧疆骞荤伅鍒囨崲杩愬姩 function setRandomMove() { oSliderPlayBox.css('overflow', 'hidden'); aSliderPlayImg.css({position: 'absolute', left: 0, top: 0, zIndex: 0}).first().css({zIndex: 1}); var dir = [0, 1, 2, 3]; move = function () { var oPrev = aSliderPlayImg.eq(iPrevIndex); var o = aSliderPlayImg.eq(iCurIndex); var data = {opacity: 0}; if(dir.length == 0) { dir = [0, 1, 2, 3] } if(dir.length == 4) { dir.sort(function() { return Math.random() > 0.5 ? 1 : -1 }) } switch(dir.shift()) { case 0: data['top'] = -iImgHeight;break; case 1: data['left'] = iImgWidth;break; case 2: data['top'] = iImgHeight;break; default : data['left'] = -iImgWidth } setAttr(iCurIndex); oPrev.css('zIndex', 2).animate(data, iSpeed, function(){ oPrev.css({zIndex: 0, opacity: 1, top: 0, left: 0}) }); o.css('zIndex', 1); iPrevIndex = iCurIndex; }; autoPlay = function () { iCurIndex++; if(iCurIndex == size) { iCurIndex = 0 } move() }; addEvent(move, autoPlay); oAutoTimer = setInterval(autoPlay, iTimeout) }; return oSliderPlayBox; } })(window, jQuery);