// javascript document /* $("").mchange({ auto : 1, //是否自动播放,1为自动,0为手动 arrow : 1, //是否反转箭头,1为点击左侧向右滚动,0则相反 speed : 500, //效果速度,大于0.3 time : 5000, //周期时间,大于1 nav : null, //导航 nave : "click", //导航事件 navs : 0, //导航滚动 prev : null, //上一个 next : null, //下一个 cycle : 1, //是否循环 mousestop : 0, //是否鼠标划入停止 mousewheel : 0, //是否开启滚轮事件 animates : function(idx){} //自定义函数 }); */ (function($) { $.fn.mchange = function(options) { var w1,np,ns,v; var opts = $.extend({},$.fn.mchange.defaults,options); //初始化 var c=$(this); var f=c.parent(); var n=$(opts.nav); var i=0; var sw=1; c.hide(); if(opts.navs){ w1 = n.outerwidth()+(parseint(n.css("margin-left"))?parseint(n.css("margin-left")):0)+(parseint(n.css("margin-right"))?parseint(n.css("margin-right")):0); np= $(opts.nav).parent(); n.wrapall("
"); ns=np.find(".scroll"); ns.width(n.size()*w1); np.stop().animate({scrollleft:0},100); } function run(){ if(sw){ i++; i=change(i); } } function change(idx){ if(c.size()>1 || idx=="begin"){ sw=0; if(idx=="begin"){ idx=0; speed=0; } else{ speed=opts.speed; c.stop().fadeout(speed); } n.removeclass("now"); if(idx==c.size()){idx=0;} if(idx<0){idx=c.size()-1;} c.eq(idx).stop().fadein(speed,function(){ if(typeof(opts.animates)=="function"){ opts.animates(idx);//执行自定义动画 } sw=1; }); n.eq(idx).addclass("now"); if(opts.navs){autonav(idx);} return idx; } } change("begin"); if(opts.auto==1){ v=setinterval(run,opts.time); } if(opts.mousestop==1){ c.hover( function(){sw=0;}, function(){sw=1;} ); } //导航溢出部分滑动显示 function autonav(idx){ np.animate({scrollleft:idx*w1}); } n.each(function(index, element) { $(this).bind(opts.nave,function(){ if(sw==1 && c.eq(index).size()>0){ change(index); i=index; if(opts.auto){ clearinterval(v); v=setinterval(run,opts.time); } } }); }); $(opts.prev).click(function(){ if(sw==1){ if(opts.cycle==1 || (opts.cycle==0 && i>0)){ i--; i=change(i); if(opts.auto==1){ clearinterval(v); v=setinterval(run,opts.time); } } } }); $(opts.next).click(function(){ if(sw==1){ if(opts.cycle==1 || (opts.cycle==0 && i0)){ i--; i=change(i); if(opts.auto){ clearinterval(v); v=setinterval(run,opts.time); } } } } event.stoppropagation(); event.preventdefault(); }); } }; $.fn.mchange.defaults = { auto : 1, arrow : 1, speed : 800, time : 5000, nav : null, nave : "click", navs : 0, prev : null, next : null, cycle : 1, mousestop : 0, mousewheel : 0, animates : null } })(jquery);