/* 基于scrollleft和scrolltop 支持自适应 根据minsize计算child尺寸和滚动距离 支持总数,当前数,标题 支持jquery.easing,jquery.mousewheel 支持导航滚动及方向设定 */ /* ======示例=====
/
=============== */ (function($) { $.fn.mscroll = function(options) { var opts = $.extend({},$.fn.mscroll.defaults,options); this.each(function(){ //初始化 var f,s,c,n,np,ns,w,ww,h,hh,w1,h1,size,iv,ti,sw=1,idx=0,cm=0; f = $(this); if(f.children().is("ul") && f.children().size()>1 && !f.children().hasclass("child")){ f.children().addclass("scroll"); } else{ f.children().wrapall("
"); } s = f.find(".scroll"); size=s.children().size(); $(opts.count).text(size); if(opts.cycle==1 && size>opts.minsize){s.append(s.html());} c = s.children(); //初始化导航 n = $(opts.nav); if(opts.scrollnav){ w1 = n.outerwidth()+parseint(n.css("margin-left"))+parseint(n.css("margin-right")); h1 = n.outerheight()+parseint(n.css("margin-top"))+parseint(n.css("margin-bottom")); np= $(opts.nav).parent(); n.wrapall("
"); ns=np.find(".scroll"); if(opts.nav_direction=="h"){ ns.width(n.size()*w1); np.stop().animate({scrollleft:0},100); } if(opts.nav_direction=="v"){ ns.height(n.size()*h1); np.stop().animate({scrolltop:0},100); } } n.eq(0).addclass("now"); //初始化尺寸 if(opts.direction=="h"){ cm=parseint(c.css("margin-left"))+parseint(c.css("margin-right")); if(cm==0){ w = math.floor(f.width()/opts.minsize); c.css({width:w}); f.width(w*opts.minsize); } else{ w=c.width()+cm; } s.css({width:100000}); ww=c.size()*w; f.stop().animate({scrollleft:0},100); } if(opts.direction=="v"){ cm=parseint(c.css("margin-top"))+parseint(c.css("margin-bottom")); if(cm==0){ h = math.floor(f.height()/opts.minsize); c.css({height:h}); f.height(h*opts.minsize); } else{ h=c.height()+cm; } s.height(100000); hh=c.size()*h; f.stop().animate({scrolltop:0},100); } //设置title if(opts.cycle==0){opts.auto=0;} $(opts.title).text(c.eq(0).attr("title")); $(opts.index).text(1); //焦点图自适应 $(window).resize(function(){rs();}); rs(); settimeout(function(){rs();},300); //自适应 function rs(){ sw==0; cleartimeout(ti); if(opts.direction=="h"){ if(cm==0){ w = math.floor(f.width()/opts.minsize); c.css({width:w}); f.width(w*opts.minsize); } else{ w=c.width()+cm; } } if(opts.direction=="v"){ if(cm==0){ h = math.floor(f.height()/opts.minsize); c.css({height:h}); f.height(h*opts.minsize); } else{ h=c.height()+cm; } } ww=c.size()*w; hh=c.size()*h; if(opts.direction=="h"){f.stop().scrollleft(idx*w);} if(opts.direction=="v"){f.stop().scrolltop(idx*h);} ti=settimeout(function(){ sw==1; },500); } if(size>opts.minsize){//判断是否满足数量 //自动开始 if(opts.auto==1){ iv = window.setinterval(function(){move(opts.dtype);},opts.delay); } //-------------start-------------// //滚动函数 function move(act){ if(sw==1){ sw=0; if(opts.direction=="h"){ if(act=="left"){ if(opts.cycle!=1){ if(idx+opts.minsize=ww*0.5){f.scrollleft(0);} f.animate({scrollleft:f.scrollleft()+w},opts.speed,opts.easing,function(){sw=1;}); } } if(act=="right"){ if(opts.cycle!=1){ if(idx>0){ idx--; f.animate({scrollleft:f.scrollleft()-w},opts.speed,opts.easing,function(){sw=1;}); } else{sw=1;} } if(opts.cycle==1){ if(idx==0){idx=size-1;} else{idx--;} if(opts.cycle==1 && f.scrollleft()<=0){f.scrollleft(ww*0.5);} f.animate({scrollleft:f.scrollleft()-w},opts.speed,opts.easing,function(){sw=1;}); } } } if(opts.direction=="v"){ if(act=="left"){ if(opts.cycle!=1){ if(idx+opts.minsize=hh*0.5){f.scrolltop(0);} f.animate({scrolltop:f.scrolltop()+h},opts.speed,opts.easing,function(){sw=1;}); } } if(act=="right"){ if(opts.cycle!=1){ if(idx>0){ idx--; f.animate({scrolltop:f.scrolltop()-h},opts.speed,opts.easing,function(){sw=1;}); } else{sw=1;} } if(opts.cycle==1){ if(idx==0){idx=size-1;} else{idx--;} if(opts.cycle==1 && f.scrolltop()<=0){f.scrolltop(hh*0.5);} f.animate({scrolltop:f.scrolltop()-h},opts.speed,opts.easing,function(){sw=1;}); } } } n.removeclass("now"); n.eq(idx).addclass("now"); $(opts.title).text(c.eq(idx).attr("title")); $(opts.index).text(idx+1); if(opts.scrollnav){autonav();} } } //左右按钮 if(opts.prev){ $(opts.prev).hover(function(){sw=1;}); $(opts.prev).click(function(){ if(opts.arrow==1){move("right");} else{move("left");} if(opts.auto){ clearinterval(iv); iv = window.setinterval(function(){move(opts.dtype);},opts.delay); } }); } if(opts.next){ $(opts.next).hover(function(){sw=1;}); $(opts.next).click(function(){ if(opts.arrow==1){move("left");} else{move("right");} if(opts.auto){ clearinterval(iv); iv = window.setinterval(function(){move(opts.dtype);},opts.delay); } }); } //导航溢出部分滑动显示 function autonav(){ if(opts.nav_direction=="h"){ if(idx*w1==np.scrollleft()){ np.stop().animate({scrollleft:np.scrollleft()-w1},opts.speed); } if((idx+1)*w1==np.scrollleft()+np.width()){ np.stop().animate({scrollleft:np.scrollleft()+w1},opts.speed); } if(idx==0){np.stop().animate({scrollleft:0},opts.speed);} if(idx==size-1){np.stop().animate({scrollleft:size*w1},opts.speed);} } if(opts.nav_direction=="v"){ if(idx*h1==np.scrolltop()){ np.stop().animate({scrolltop:np.scrolltop()-h1},opts.speed); } if((idx+1)*h1==np.scrolltop()+np.height()){ np.stop().animate({scrolltop:np.scrolltop()+h1},opts.speed); } if(idx==0){np.stop().animate({scrolltop:0},opts.speed);} if(idx==size-1){np.stop().animate({scrolltop:size*h1},opts.speed);} } } //导航点击 n.each(function(index, element) { $(this).click(function(){ if(opts.direction=="h"){f.stop().animate({scrollleft:index*w},opts.speed,opts.easing,function(){sw=1;});} if(opts.direction=="v"){f.stop().animate({scrolltop:index*h},opts.speed,opts.easing,function(){sw=1;});} idx=index; n.removeclass("now"); n.eq(idx).addclass("now"); if(opts.scrollnav){autonav();} $(opts.title).text(c.eq(idx).attr("alt")); if(opts.auto){ clearinterval(iv); iv = window.setinterval(function(){move(opts.dtype);},opts.delay); } }); }); //鼠标滚轮 if(opts.mousewheel){ f.mousewheel(function(event,delta,deltax,deltay){ if(delta<0){ if(opts.arrow==1){move("left");} else{move("right");} } else{ if(opts.arrow==1){move("right");} else{move("left");} } event.stoppropagation(); event.preventdefault(); }); } //鼠标划入停止 if(opts.mousestop && !opts.mousewheel){ f.hover(function(){sw=0},function(){sw=1}); } //-------------end-------------// } }) }; $.fn.mscroll.defaults = { auto : 1, //自动开始 direction : "h", //滚动方向,h横向,v纵向 dtype : "left", //正反方向 isfocus : 0, //是否作为焦点图 minsize : 1, //最小数量 speed : 500, //滚动速度 easing : "", //缓冲类型 delay : 5000, //自动滚动间隔 nav : null, //导航按钮 scrollnav : 0, //导航滚动 nav_direction : "h", //导航方向,h横向,v纵向 prev : null, //前一个 next : null, //后一个 index : null, //当前序号 count : null, //总数 title : null, //标题 arrow : 1, //是否反转 cycle : 1, //是否循环 mousestop : 1, //鼠标划入停止 mousewheel : 0 //开启滚轮事件 } })(jquery);