/* * myfocus javascript library v2.0.1 * open source under the bsd & gpl license * * copyright 2012, koen lee * http://cosmissy.com/ * * date: 2012/05/26 */ (function(){ //dom基础操作函数 var $id=function(id){ return typeof id==='string'?document.getelementbyid(id):id; }, $tag=function(tag,parentnode){ return ($id(parentnode)||document).getelementsbytagname(tag); }, $tag_=function(tag,parentnode){ return $getchild(tag,parentnode,'tag'); }, $class=function(classname,parentnode){ var doms=$tag('*',parentnode),arr=[]; for(var i=0,l=doms.length;i'://children ischild=true; break; case '.'://class var cls=s.slice(1); var fn=ischild?$class_:$class; $(parent).each(function(){ dom=dom.concat(fn(cls,this)); }); ischild=false; break; case '#'://id var id=s.slice(1),e=$id(id); if(e) dom.push($id(id)); ischild=false; break; default://tag(支持'tag.class'寻找,不支持也不建议用'tag#id'寻找,请用'#id') var fn=ischild?$tag_:$tag,sarr=s.split('.'); var tag=sarr[0],cls=sarr[1]; $(parent).each(function(){ var arr=fn(tag,this); for(var i=0,len=arr.length;i+~,(\[\\]+)+|[>+~])(\s*,\s*)?/g; var parts=[],m; while((m = chunker.exec(selector)) !== null ) { parts.push( m[1] );//存储匹配的字符串信息 } return parts; }, wrap:function(html){//每次只wrap一个元素,多个请用each var o=this[0],e=document.createelement('div'); e.innerhtml=html; var wrap=e.firstchild; o.parentnode.replacechild(wrap,o); wrap.appendchild(o); return this; }, addhtml:function(html){ var parent=this[0]; var e=document.createelement('div'); e.innerhtml=html; var dom=e.childnodes[0]; parent.appendchild(dom); return myfocus(dom); }, addlist:function(classname,type){ var li=this.htmlulist,n=this.htmlulistlength; var strarr=['
    ']; for(var i=0;i';break;//b标签主要是为了做透明背景,下同 case 'txt' :html=img?li[i].innerhtml.replace(/\/i,img.alt)+'

    '+img.getattribute("text")+'

    ':'';break; case 'thumb':html=img?'':'';break; default :html=''; } strarr.push('
  • '+html+'
  • '); } strarr.push('
'); return this.addhtml(strarr.join('')); }, addlistnum:function(classname){ return this.addlist(classname||'num','num');//默认class=num }, addlisttxt:function(classname){ return this.addlist(classname||'txt','txt');//默认class=txt }, addlistthumb:function(classname){ return this.addlist(classname||'thumb','thumb');//默认class=thumb }, remove:function(){ var o=this[0]; if(o) o.parentnode.removechild(o); }, repeat:function(n){ var n=n||2,pnode=this[0].parentnode,html=pnode.innerhtml,s=[]; for(var i=0;i({height:'300px'}),可以不带单位px for(var p in css){ if(typeof css[p]==='number'&&!this.cssnumber[p]) css[p]+='px'; arr.push(p.replace(/([a-z])/g,'-$1')+':'+css[p]+';'); if(p==='opacity') arr.push('filter:alpha(opacity='+css[p]*100+')'); } o.style.csstext+=arr.join(''); return this; } }, setopacity:function(value){//仅用于animate要求高效的核心算法中,其它情况可用css()代替 this[0].style.opacity=value,this[0].style.filter='alpha(opacity='+value*100+')'; }, setanimatestyle:function(value,prop,m){//仅用于animate要求高效的核心算法中,其它情况可用css()代替 this[0].style[prop]=math[m](value)+'px'; }, addclass:function(classname){ this[0].classname+=' '+classname; return this; }, removeclass:function(classname){ var o=this[0],cls=classname&&o.classname,reg="/\\s*\\b"+classname+"\\b/g"; o.classname=cls?cls.replace(eval(reg),''):''; return this; }, cssnumber:{fillopacity:true,fontweight:true,lineheight:true,opacity:true,orphans:true,widows:true,zindex:true,zoom:true}//不加px的css,参考jquery }); myfocus.fn.extend({//animate animate:function(attr,value,time,type,funcbefore,funcafter){//value支持相对增值'+=100',相对减值'-=100'形式 var $o=this,o=$o[0],isopacity=attr==='opacity',diffvalue=false; funcbefore&&funcbefore.call(o); if(typeof value==='string'){ if(/^[+-]=\d+/.test(value)) value=value.replace('=',''),diffvalue=true; value=parsefloat(value); } var orival=$o.css(attr),//原始属性值 b=isnan(orival)?0:orival,//开始值,无值时为0 c=diffvalue?value:value-b,//差值 d=time,//总运行时间 e=this.easing[type],//缓动类型 m=c>0?'ceil':'floor',//取最大绝对值 timerid='__myfocustimer__'+attr,//计数器id setproperty=$o[isopacity?'setopacity':'setanimatestyle'],//属性设置方法 origtime=(new date)*1;//原始时间值 o[timerid]&&clearinterval(o[timerid]); o[timerid]=setinterval(function(){ var t=(new date)-origtime;//已运行时间 if(t<=d){ setproperty.call($o,e(t,b,c,d),attr,m); }else{ setproperty.call($o,b+c,attr,m);//设置最终值 clearinterval(o[timerid]),o[timerid]=null; funcafter&&funcafter.call(o); } },13); return this; }, fadein:function(time,type,fn){ if(typeof time!=='number') fn=time,time=400;//默认400毫秒 if(typeof type==='function') fn=type,type=''; this.animate('opacity',1,time,type||'linear',function(){ myfocus(this).css({display:'block',opacity:0}); },fn); return this; }, fadeout:function(time,type,fn){ if(typeof time!=='number') fn=time,time=400;//默认400毫秒 if(typeof type==='function') fn=type,type=''; this.animate('opacity',0,time,type||'linear',null,function(){ this.style.display='none'; fn&&fn.call(this); }); return this; }, slide:function(params,time,type,fn){ if(typeof time!=='number') fn=time,time=800;//默认800毫秒 if(typeof type==='function') fn=type,type=''; for(var p in params) this.animate(p,params[p],time,type||'easeout',null,fn); return this; }, stop:function(){//停止所有运动 var o=this[0]; for(var p in o) if(p.indexof('__myfocustimer__')!==-1) o[p]&&clearinterval(o[p]); return this; }, easing:{ linear:function(t,b,c,d){return c*t/d + b;}, swing:function(t,b,c,d) {return -c/2 * (math.cos(math.pi*t/d) - 1) + b;}, easein:function(t,b,c,d){return c*(t/=d)*t*t*t + b;}, easeout:function(t,b,c,d){return -c*((t=t/d-1)*t*t*t - 1) + b;}, easeinout:function(t,b,c,d){return ((t/=d/2) < 1)?(c/2*t*t*t*t + b):(-c/2*((t-=2)*t*t*t - 2) + b);} } }); myfocus.fn.extend({//method(fn) bind:function(type,fn){ myfocus.addevent(this[0],type,fn); return this; }, play:function(funclastframe,funccurrentframe,seamless){ var this_=this,p=this_.settings,n=this_.htmlulistlength,t=p.time*1000,seamless=seamless||false,//是否无缝 float=myfocus(this_.htmlulist).css('float'),islevel=float==='left',//仅支持'left'方向和'top'方向 direction=islevel?'left':'top',distance=islevel?p.width:p.height,//运动距离 indexlast=0,indexcurrent=p.index;//帧索引值,默认0 this_.find('.loading').remove();//删除loading... this_.run=function(value){//循环运动函数,支持+-=value funclastframe&&funclastframe(indexlast,n);//运行前一帧 indexcurrent=typeof value==='string'?indexlast+parseint(value.replace('=','')):value;//fixed index if(indexcurrent<=-1){//prev run indexcurrent=n-1;//转到最后一帧 if(seamless) this_.htmlulist[0].parentnode.style[direction]=-n*distance+'px';//无缝的ul定位 } if(indexcurrent>=n){//next run if(!seamless) indexcurrent=0;//非无缝时转到第一帧 if(indexcurrent>=2*n){//无缝 this_.htmlulist[0].parentnode.style[direction]=-(n-1)*distance+'px';//无缝的ul定位 indexcurrent=n; } } if(seamless&&indexlast>=n&&indexcurrent=this.showend){//next this.showend=i0?i-1:0; this.showend=this.showstart+this.shownum-1; } css[dir]=-this.showstart*this.distance; $ul.slide(css,time||500,'easeout'); return this; } }); myfocus.extend({//init set:function(p,callback){ var f=this,id=p.id,ostyle=f.initbasecss(id); p.pattern=p.pattern||f.defconfig.pattern; p.__clsname=p.pattern+'_'+id; f.addevent(window,'load',function(){f.onloadwindow=true}); f.loadpattern(p,function(){ p=f.extend({},f.defconfig,f.config[p.pattern],p);//收集完整参数 f.getboxready(p,function(){ var $o=f($id(id)); p.width=p.width||$o.css('width'),p.height=p.height||$o.css('height');//获得box高/宽 f.initcss(p,$o,ostyle);//css f.inithtml($o);//html $o.addclass(p.pattern+' '+p.__clsname);//+classname f.getimgready(p,function(){ f.pattern[p.pattern](p,f);//运行pattern代码 callback&&callback(); }); }); }); }, onloadwindow:false, loadpattern:function(p,callback){ if(this.pattern[p.pattern]){callback();return;}//如果已加载pattern var path=this.getfilepath()+'mf-pattern/'+p.pattern; var js= document.createelement("script"),css=document.createelement("link"),src=path+'.js',href=path+'.css'; js.type = "text/javascript",js.src=src; css.rel = "stylesheet",css.href=href; var head=$tag('head')[0],issuccess=0,timeout=3000;//超时3秒 head.appendchild(css); head.appendchild(js); js.onload=js.onreadystatechange=function(){ if(!js.readystate||js.readystate=="loaded"||js.readystate=="complete") callback(),issuccess=1; }; settimeout(function(){if(!issuccess) alert('failed to load: '+src);},timeout); }, getfilepath:function(){ var path=''; var scripts=$tag("script"); for(var i=0,len=scripts.length;i'); css.push('.'+p.__clsname+' *{margin:0;padding:0;border:0;list-style:none;}.'+p.__clsname+'{position:relative;width:'+w+'px;height:'+h+'px;overflow:hidden;font:12px/1.5 verdana;text-align:left;background:#fff;visibility:visible!important;}.'+p.__clsname+' .loading{position:absolute;z-index:9999;width:100%;height:100%;color:#666;text-align:center;padding-top:'+0.26*h+'px;background:#fff;}.'+p.__clsname+' .pic{position:relative;width:'+w+'px;height:'+h+'px;overflow:hidden;}.'+p.__clsname+' .txt li{width:'+w+'px;height:'+p.txtheight+'px!important;overflow:hidden;}'); //if(p.autozoom) css.push('.'+p.__clsname+' .pic li{text-align:center;width:'+w+'px;height:'+h+'px;}');//缩放图片居中 try{ostyle.stylesheet.csstext=css.join('')}catch(e){ostyle.innerhtml=css.join('')} }, initbasecss:function(id){ var s='#'+id+' *{display:none}',ostyle=document.createelement('style'); ostyle.type='text/css'; try{ostyle.stylesheet.csstext=s}catch(e){ostyle.innerhtml=s} var ohead = $tag('head',document)[0]; ohead.insertbefore(ostyle,ohead.firstchild); return ostyle; }, inithtml:function($o){ var $load=$o.find('.loading'),$img=$load.find('img'),img=$img[0]; if($img.length){ $load.addhtml('

'+img.alt+'

'); if(!img.getattribute('src')) img.style.display='none'; } } }); myfocus.extend({//method(myfocus) isie:!!(document.all&&navigator.useragent.indexof('opera')===-1),//!(+[1,]) bug in ie9+? addevent:function(o,type,fn){ var ie=this.isie,e=ie?'attachevent':'addeventlistener',t=(ie?'on':'')+type; o[e](t,function(e){ var e=e||window.event,flag=fn.call(o,e); if(flag===false){ if(ie) e.cancelbubble=true,e.returnvalue=false; else e.stoppropagation(),e.preventdefault(); } },false); } }); //支持jq if(typeof jquery!=='undefined'){ jquery.fn.extend({ myfocus:function(p,fn){ if(!p) p={}; p.id=this[0].id; if(!p.id) p.id=this[0].id='mf__id__'; myfocus.set(p,fn); } }); } })();