jquery.asyncslider.js 11.4 KB
/**
 * AsyncSlider
 * JQuery plugin
 *
 * Created by: Arlind Nushi
 * Author email: arlindd@gmail.com
 *
 * Last Update: August 16, 2011
 */


(function(a){function P(a,b){switch(a){case"off":u=false;if(typeof w!="undefined"){window.clearInterval(w);w=null}break;case"on":P("update_interval",v);break;case"update_interval":u=true;v=b;if(typeof w!="undefined"){window.clearInterval(w);w=null}w=setInterval(function(){if(u){K()}},v);break;case"play":u=true;if(typeof w=="undefined"){P("on")}break;case"pause":u=false;break}}function O(a,b){switch(a.toLowerCase()){case"direction":if(b=="vertical"){k="vertical"}else{k="horizontal"}break;case"callback":if(typeof b=="function"){e=b}break;case"minTime":i=parseInt(b,10);break;case"maxTime":j=parseInt(b,10);break;case"easing":m="";n="";l=b;break;case"easingin":m=b;break;case"easingout":n=b;break;case"keyboardnavigate":o=b?true:false;break;case"random":t=b?true:false;break;case"autoswitch":if(typeof b=="number"&&b>0){P("update_interval",b)}else if(typeof b=="string"&&(b.toLowerCase()=="play"||b.toLowerCase()=="on"||b.toLowerCase()=="off"||b.toLowerCase()=="pause")){P(b.toLowerCase())}else if(typeof b=="string"&&b.toLowerCase()=="pause"){P("pause")}break}}function N(a,b){return Math.floor(Math.random()*(b-a+1)+a)}function M(c,h){var o=J(h);var p=J(c);if(!o||!p||c==h){return false}if(D){return false}D=true;var r=parseInt((j+i)/2,10);j=i>j?i:j;i=i>j?j:i;if(p.background){if(!o.background){f.css({background:p.background}).hide().fadeTo(r,1)}else{if(d==0){f.css({background:o.background});g.hide().css({background:p.background}).stop().fadeTo(r,1)}else{f.css({background:p.background}).show();g.stop().fadeTo(r,0,function(){f.hide();g.css({background:p.background}).fadeTo(0,1)})}}}else{g.stop().fadeTo(r,0);f.stop().fadeTo(r,0)}if(q&&E&&F){E.stop().animate({top:parseInt(p.outerHeight()/2-E.outerHeight()/2,10)},r);F.stop().animate({top:parseInt(p.outerHeight()/2-F.outerHeight()/2,10)},r)}f.stop().animate({height:p.height},r);g.height(p.height);if(G){G.find("li").removeClass("active").filter(":nth-child("+(p.index+1)+")").addClass("active")}var s=1,v,x,y,H,I,K,L,M,O,Q,R,S,T;if(k=="horizontal"){var U=a(window);var V=U.width()-p.width;b.css({overflow:"hidden"}).stop().animate({height:p.height},r);var W=parseInt(o.width/2+V/2,10);if(c>h){S=o.height;if(S<p.height){S=p.height-(p.height-o.height)}o.css({position:"relative",zIndex:C+3});p.css({position:"relative",zIndex:C+4,top:-S}).show();s=1}else{S=o.height;if(S<p.height){S=p.height}else{S-=o.height-p.height}o.css({position:"relative",zIndex:C+3,top:-S});p.css({position:"relative",zIndex:C+4}).show();s=-1}if(A){if(A=="right"){s=-1}else{s=1}}if(!m){m=l}if(!n){n=l}y=i;H=parseInt((j-i)/o.elements.length,10);T=o.width+V;for(v=0;v<o.elements.length;v++){x=o.elements[v];I=x.def_left;K=x.def_right;L=x.def_top;M=x.def_bottom;O=x.def_css_horizontal_direction;Q=x.def_css_vertical_direction;R=N(i,j);W=T;if(!t){y+=H;R=y}if(O==="right"){if(Q==="bottom"){x.stop().animate({right:-s*W+K,bottom:M+0},R,m)}else{x.stop().animate({right:-s*W+K,top:L+0},R,m)}}else{if(Q==="bottom"){x.stop().animate({left:-s*W+I,bottom:M+0},R,m)}else{x.stop().animate({left:-s*W+I,top:L+0},R,m)}}}y=i;H=parseInt((j-i)/p.elements.length,10);T=p.width+V;var X=p.elements.length;for(v=0;v<p.elements.length;v++){x=p.elements[v];I=x.def_left;K=x.def_right;L=x.def_top;M=x.def_bottom;O=x.def_css_horizontal_direction;Q=x.def_css_vertical_direction;R=N(i,j);W=T+N(0,x.outerWidth());if(!t){y+=H;R=y}var Y=parseInt(v,10);if(O==="right"){if(Q==="bottom"){x.css({right:s*W+K,bottom:M+0}).stop().animate({right:0+K},R,n)}else{x.css({right:s*W+K,top:L+0}).stop().animate({right:0+K},R,n)}}else{if(Q==="bottom"){x.css({left:s*W+I,bottom:M+0}).stop().animate({left:0+I},R,n)}else{x.css({left:s*W+I,top:L+0}).stop().animate({left:0+I},R,n)}}}}else{b.css({overflow:"hidden"}).stop().animate({height:p.height},r);var Z=b.height();if(c>h){o.css({position:"relative",zIndex:C+3});p.css({position:"relative",zIndex:C+4,top:-o.height}).show();s=1}else{o.css({position:"relative",zIndex:C+3,top:-o.height});p.css({position:"relative",zIndex:C+4}).show();s=-1}if(B){if(B=="top"){s=-1}else{s=1}}if(!m){m=l}if(!n){n=l}o.animate({top:-s*Z},r);y=i;H=parseInt((j-i)/o.elements.length,10);for(v=0;v<o.elements.length;v++){x=o.elements[v];I=x.def_left;K=x.def_right;L=x.def_top;M=x.def_bottom;O=x.def_css_horizontal_direction;Q=x.def_css_vertical_direction;R=N(i,j);S=Z;if(!t){y+=H;R=y}if(Q==="bottom"){x.stop().animate({bottom:-s*S+M},R,m)}else{x.stop().animate({top:-s*S+L},R,m)}}y=i;H=parseInt((j-i)/p.elements.length,10);for(v=0;v<p.elements.length;v++){x=p.elements[v];I=x.def_left;K=x.def_right;L=x.def_top;M=x.def_bottom;O=x.def_css_horizontal_direction;Q=x.def_css_vertical_direction;R=N(i,j);S=Z;if(!t){y+=H;R=y}if(O==="right"){if(Q==="bottom"){x.css({bottom:s*S,right:0+K}).stop().animate({bottom:0+M},R,n)}else{x.css({top:s*S,right:0+K}).stop().animate({top:0+L},R,n)}}else{if(Q==="bottom"){x.css({bottom:s*S,left:0+I}).stop().animate({bottom:0+M},R,n)}else{x.css({top:s*S,left:0+I}).stop().animate({top:0+L},R,n)}}}}z=window.setTimeout(function(){o.css({position:"relative",top:0,left:0}).hide();p.css({position:"relative",top:0,left:0});for(v=0;v<o.elements.length;v++){x=o.elements[v];I=x.def_left;K=x.def_right;L=x.def_top;M=x.def_bottom;O=x.def_css_horizontal_direction;Q=x.def_css_vertical_direction;if(O==="right"){x.css({right:K})}else{x.css({left:I})}if(Q==="bottom"){x.css({bottom:M})}else{x.css({top:L})}}for(v=0;v<p.elements.length;v++){x=p.elements[v];I=x.def_left;K=x.def_right;L=x.def_top;M=x.def_bottom;O=x.def_css_horizontal_direction;Q=x.def_css_vertical_direction;if(O==="right"){x.css({right:K})}else{x.css({left:I})}if(Q==="bottom"){x.css({bottom:M})}else{x.css({top:L})}}A="";e(p.index+1);window.clearTimeout(z);z=null;D=false;if(typeof w!="undefined"){if(u){window.clearTimeout(w);w=null;P("on")}}d++},j);return true}function L(a){if(D){return false}var b=x;x--;x=x<0?y-1:x;A="right";B="top";if(a){A=a=="left"?"left":"right"}if(a){B=a=="top"?"bottom":"top"}M(x,b)}function K(a){if(D){return false}var b=x;x++;x=x%y;A="left";B="bottom";if(a){A=a=="right"?"right":"left"}if(a){B=a=="bottom"?"top":"bottom"}M(x,b)}function J(a){if(a>=0&&a<y){return h[a]}return null}function I(a){if(a>=0&&a<y){x=a}}function H(c,d){if(typeof b=="undefined"){b=a(c);b.css({position:"relative"});if(typeof d=="object"){if(typeof d.direction=="string"){if(d.direction.toLowerCase()=="vertical"){k="vertical"}else{k="horizontal"}}if(typeof d.random!="undefined"){t=d.random}if(typeof d.minTime!="undefined"){i=d.minTime}if(typeof d.maxTime!="undefined"){j=d.maxTime}if(typeof d.easing=="string"){l=d.easing}if(typeof d.easingIn=="string"){m=d.easingIn}if(typeof d.easingOut=="string"){n=d.easingOut}if(typeof d.keyboardNavigate!="undefined"){o=d.keyboardNavigate?true:false}if(typeof d.prevNextNav!="undefined"){p=d.prevNextNav}if(typeof d.centerPrevNextNav!="undefined"){q=true;r=parseInt(d.centerPrevNextNav,10)}if(typeof d.slidesNav!="undefined"){s=d.slidesNav}if(typeof d.autoswitch=="number"&&d.autoswitch>0){u=true;v=d.autoswitch;P("on")}}else{d={}}b.children().each(function(c){var d=a(this);var e=d.find("*");d.hide().css({zIndex:C+10,position:"relative"});d.height=d.outerHeight()>d.height()?d.outerHeight():d.height();d.width=d.outerWidth();d.background=false;var i="";var j=d.css("background-color");var k=d.css("background-image");var l=d.css("background-repeat");var m=d.css("background-position");var n=j+" "+(k=="none"?"":k)+" "+m+" "+l;if(k!="none"||j){i=n;d.css("background","none")}if(i||d.attr("data-background")){d.background=i;if(d.attr("data-background")){d.background=d.attr("data-background")}var o=[];o=d.background.match(/url\((.*?)\)/);if(o&&o.length>0){var p=new Image;p.src=o[1]}}d.elements=[];e.each(function(){var b=a(this);var c=parseInt(b.css("left"),10);var e=parseInt(b.css("right"),10);var f=parseInt(b.css("top"),10);var g=parseInt(b.css("bottom"),10);b.def_left=0;b.def_right=0;b.def_top=0;b.def_bottom=0;var h,i;if(!isNaN(c)){b.def_left=c;b.def_css_horizontal_direction="left"}else if(!isNaN(e)){b.def_right=e;b.def_css_horizontal_direction="right"}if(!isNaN(f)){b.def_top=f;b.def_css_vertical_direction="top"}else if(!isNaN(g)){b.def_bottom=g;b.def_css_vertical_direction="bottom"}if(b.css("position")!="absolute"){b.css({position:"relative"})}var j=parseInt(b.css("zIndex"));if(isNaN(j)){b.css({zIndex:C+10})}else{b.css({zIndex:C+10+j})}if(b.get(0).tagName!="BR"){d.elements.push(b)}});d.index=parseInt(c,10);h.push(d);if(c===0){d.show();b.height(d.height).css({overflow:"hidden"});f=a('<div id="asyncslider_background_mask"></div>');g=a('<div id="asyncslider_background_mask_2"></div>');d.before(f);d.before(g);f.css({position:"absolute",top:0,left:0,zIndex:C+1,padding:0,margin:0,width:b.outerWidth(),height:b.outerHeight()});g.css({position:"absolute",top:0,left:0,zIndex:C+2,padding:0,margin:0,width:b.outerWidth(),height:b.outerHeight()});a(window).resize(function(){f.width(b.outerWidth());g.width(b.outerWidth())});if(d.background){f.css("background",d.background)}}});y=h.length;if(typeof d!="undefined"&&typeof d.firstSlide!="undefined"){var w=d.firstSlide-1;if(w>=0&&w<y){var z=i,A=j,B=e;i=j=0;e=function(){};x=w;M(x,0);i=z;j=A;e=B}}if(b.children().length<2){return}a(window).keydown(function(a){if(o){switch(a.keyCode){case 37:L();break;case 39:K();break}}});if(p&&y>1){var H=a('<ul id="asyncslider_next_prev_nav" />');H.css({zIndex:C+100});var I=a('<li class="prev" />');var N=a('<li class="next" />');var O=a('<a href="#">Previous</a>');var Q=a('<a href="#">Next</a>');H.append(I);H.append(N);I.append(O);N.append(Q);I.click(function(a){a.preventDefault();L()});N.click(function(a){a.preventDefault();K()});if(typeof p=="object"){p.append(H)}else{b.append(H);if(q){var R=r;var S=J(x);H.css({position:"absolute",left:"50%",margin:0,padding:0,listStyle:"none",marginLeft:-parseInt(S.outerWidth()/2,10),top:0});H.find("*").css({margin:0,padding:0});var T=H.find(".next");var U=H.find(".prev");E=T.children();F=U.children().css({left:-O.width()-R});U.add(T).css({position:"relative"});U.width(F.width());T.width(E.width());E.add(F).css({position:"absolute"});E.css({left:S.outerWidth()+R,top:parseInt(S.outerHeight()/2-F.outerHeight()/2,10)});F.css({top:parseInt(S.outerHeight()/2-F.outerHeight()/2,10)})}}}if(s){G=a('<ul id="asyncslider_slides_nav" />');G.css({zIndex:C+100});for(var V=0;V<h.length;V++){var W=h[V];var X=a('<li id="asyncslider_slide_'+(parseInt(V,10)+1)+'" />');var Y=a('<a href="#" rel="'+V+'">'+(parseInt(V,10)+1)+"</a>");if(V===0&&typeof d.firstSlide=="undefined"){X.addClass("active")}else if(typeof d.firstSlide!="undefined"&&V==parseInt(d.firstSlide,10)-1){X.addClass("active")}G.append(X);X.append(Y);index=parseInt(V,10);var Z=Y.click(function(b){b.preventDefault();if(D){return false}G.find("li").removeClass("active");a(this).parent().addClass("active");var c=parseInt(a(this).attr("rel"),10);var d=x;x=c;M(x,d)})}if(typeof s=="object"){s.append(G)}else{b.append(G)}}}}var b;var c=this;var d=0;var e=function(){};var f;var g;var h=[];var i=500;var j=1e3;var k="horizontal";var l="easeInOutSine";var m="";var n="";var o=false;var p=true;var q=true;var r=5;var s=true;var t=true;var u=false;var v=5e3;var w=null;var x=0;var y=0;var z;var A="";var B="";var C=50;var D=false;var E,F;var G;a.fn.extend({asyncSlider:function(a,b,c){if(typeof a=="string"){switch(a.toLowerCase()){case"movetoslide":var d=parseInt(b,10);if(d>0&&d<=y){var f=x;x=d-1;if(c){A=c=="right"?"right":"left"}if(c){B=c=="bottom"?"top":"bottom"}M(x,f)}break;case"set":O(b,c);break;case"next":K(b);break;case"prev":L(b);break}}H(this,a);if(typeof b=="function"){e=b}}})})(jQuery)