可用于圖片或者容器,使用與常規(guī)jQuery插件調(diào)用方式無異。實(shí)現(xiàn)原理也不難理解,都在代碼注釋中。想研究的可以看下面的代碼,或者樣例演示。
;(function($){
/*
* 基于jQuery的簡(jiǎn)易手風(fēng)琴切換插件
*/
$.fn.iAccordion=function(iSet){
var self=this;
iSet=$.extend({Type:'mouseover',Select:'img',Cur:0,InitInterval:100,Interval:500,Easing:''},iSet||{});
/*
* Type: 鼠標(biāo)事件類型,mouseover,click,mouseleave等
* Select: 選擇器,用以獲取需要切換的元素集合
* Cur: 默認(rèn)展開元素的索引
* InitInterval: 初始化手風(fēng)琴效果動(dòng)畫間隔時(shí)間
* Interval: 鼠標(biāo)事件動(dòng)畫間隔時(shí)間
* Easing: 動(dòng)畫效果,需要jQuery.easing支持,參數(shù)可參考jQuery.easing@ http://gsgd.co.uk/sandbox/jquery/easing/
*/
var item,boxW,selectW,animateW,sIndex,animateL;
$(self).each(function(){
//初始化容器樣式
$(this).css({'position':'relative','overflow':'hidden'});
item=$(this).find(iSet.Select);
//初始化切換元素樣式
item.css({'position':'absolute','left':0,'top':0});
boxW=$(this).outerWidth();
selectW=item.outerWidth();
animateW=(boxW-selectW)/(item.size()-1);
//初始化元素排列并為元素data一個(gè)索引值
item.each(function(i){
$(this).animate({'left':animateW*i+'px'},iSet.InitInterval,iSet.Easing);
$(this).data('index',i);
}).on(iSet.Type,function(e){//綁定鼠標(biāo)事件
//獲取當(dāng)前元素索引值
sIndex=$(this).data('index');
//鼠標(biāo)事件動(dòng)畫,通過判斷元素索引值與當(dāng)前元素索引值的大小關(guān)系動(dòng)畫顯示當(dāng)前元素并動(dòng)畫排列
item.each(function(n){
n > sIndex ? animateL=selectW+animateW*(n-1) : animateL=animateW*n;
$(this).stop().animate({'left':animateL+'px'},iSet.Interval,iSet.Easing);
});
}).eq(iSet.Cur).trigger(iSet.Type);
});
}
})(jQuery);
如何調(diào)用?
1、在頁(yè)面中引入上面的插件代碼;
2、$(selectmain).iAccordion({…});
3、相關(guān)參數(shù)及功能,請(qǐng)參考插件中的注釋說明。
小小的提示,若需要定義Easing,需要導(dǎo)入jQuery.easing插件 ,Easing的參數(shù)即jQuery.easing的方法名稱,如easeOutBounce、easeOutQuint等。