/* * touchslider v1.0.5 * by qiqiboy, http://www.qiqiboy.com, http://weibo.com/qiqiboy, 2012/04/11 */ (function(window, undefined){ var adsupportstouches = ("createtouch" in document) || ('ontouchstart' in window) || 0, doc=document.documentelement || document.getelementsbytagname('html')[0], adsupportstransition = ("webkittransition" in doc.style) || ("mstransition" in doc.style) || ("moztransition" in doc.style) || ("otransition" in doc.style) || ("transition" in doc.style) || 0, adstartevent = adsupportstouches ? "touchstart" : "mousedown", admoveevent = adsupportstouches ? "touchmove" : "mousemove", adendevent = adsupportstouches ? "touchend" : "mouseup", touchslider=function(opt){ this.opt=this.parse_args(opt); this.container=this.$(this.opt.id); try{ if(this.container.nodename.tolowercase()=='ul'){ this.element=this.container; this.container=this.element.parentnode; }else{ this.element=this.container.getelementsbytagname('ul')[0]; } if(typeof this.element==='undefined')throw new error('can\'t find "ul"'); for(var i=0;i=0&&parsefloat(ft.match(/opacity=([^)]*)/i)[1])/100+''||'1'; }, set : function(el,va){ el.style.filter='alpha(opacity='+va*100+')'; el.style.zoom=1; } } ; break; default:var arr=property.split('-'); for(var i = 1; i < arr.length; i++) arr[i] = arr[i].substring(0,1).touppercase() + arr[i].substring(1); property = arr.join(''); return property; break; } }, getstyle=function(el,property){ property=stylefilter(property); var value = el.style[property]; if (!value) { var style = document.defaultview && document.defaultview.getcomputedstyle && getcomputedstyle(el, null) || el.currentstyle || el.style; if(typeof property=='string'){ value=style[property]; }else value=property.get(el,style); } return value == 'auto' ? '' : value; }, setstyle=function(el,css){ var attr; for(var key in css){ attr=stylefilter(key); if(typeof attr=='string'){ el.style[attr]=css[key]; }else{ attr.set(el,css[key]); } } } return function(el,css){ return typeof css=='string'?getstyle(el,css):setstyle(el,css); } })(), //格式化参数 parse_args: function(r){ var _default={}, tostring=object.prototype.tostring; if(r && tostring.call(r)=='[object object]') for(var key in this._default){ _default[key]=typeof r[key]==='undefined' ? this._default[key] : tostring.call(this._default[key])=='[object number]' ? parseint(parsefloat(r[key])*100)/100 : r[key]; } else _default=this._default; return _default; }, //绑定事件 addlistener: function(e, n, o, u){ if(e.addeventlistener){ e.addeventlistener(n, o, u); return true; } else if(e.attachevent){ e.attachevent('on' + n, o); return true; } return false; }, //获取鼠标坐标 getmousepoint:function(ev) { var x = y = 0, doc = document.documentelement, body = document.body; if(!ev) ev=window.event; if (window.pageyoffset) { x = window.pagexoffset; y = window.pageyoffset; }else{ x = (doc && doc.scrollleft || body && body.scrollleft || 0) - (doc && doc.clientleft || body && body.clientleft || 0); y = (doc && doc.scrolltop || body && body.scrolltop || 0) - (doc && doc.clienttop || body && body.clienttop || 0); } if(adsupportstouches && ev.touches.length){ var evt = ev.touches[0]; x += evt.clientx; y += evt.clienty; }else{ x += ev.clientx; y += ev.clienty; } return {'x' : x, 'y' : y}; }, //修正函数作用环境 bind:function(func, obj){ return function(){ return func.apply(obj, arguments); } }, preventdefault:function(e){ if(window.event)window.event.returnvalue=false; else e.preventdefault(); }, //初始化 setup: function(){ this.status=0;//状态码,0表示停止状态,1表示运行状态,2表示暂停状态,-1表示出错 this.slides=this.opt.classname?this.$e(this.opt.classname,'li',this.element):this.element.getelementsbytagname('li'); this.length=this.slides.length; this.opt.timeout=math.max(this.opt.timeout,this.opt.speed); this.touching=!!adsupportstouches; this.css3transition=!!adsupportstransition; this.index=this.opt.auto<0 || this.opt.auto>=this.length ? 0:this.opt.auto; if(this.length<2)return;//小于2不需要滚动 switch(this.opt.direction){ case 'up': this.direction='up'; this.vertical=true; break; case 'down': this.direction='down'; this.vertical=true; break; case 'right': this.direction='right'; this.vertical=false; break; default:this.direction='left'; this.vertical=false; break; } this.resize(); this.begin(); this.addlistener(this.element,adstartevent,this.bind(this._start,this),false); this.addlistener(document,admoveevent,this.bind(this._move,this),false); this.addlistener(document,adendevent,this.bind(this._end,this),false); this.addlistener(this.element,'webkittransitionend',this.bind(this._transitionend,this),false); this.addlistener(this.element,'mstransitionend',this.bind(this._transitionend,this),false); this.addlistener(this.element,'otransitionend',this.bind(this._transitionend,this),false); this.addlistener(this.element,'transitionend',this.bind(this._transitionend,this),false); this.addlistener(window,'resize',this.bind(function(){ cleartimeout(this.resizetimer); this.resizetimer=settimeout(this.bind(this.resize,this),100); },this),false); this.addlistener(this.element,'mousewheel',this.bind(this.mousescroll,this),false); this.addlistener(this.element,'dommousescroll',this.bind(this.mousescroll,this),false); }, resize:function(){ var css; this.css(this.container,{'overflow':'hidden','visibility':'hidden','liststyle':'none','position':'relative'}); this.width=this.container.clientwidth-parseint(this.css(this.container,'padding-left'))-parseint(this.css(this.container,'padding-right')); this.height=this.container.clientheight-parseint(this.css(this.container,'padding-top'))-parseint(this.css(this.container,'padding-bottom')); css={'position':'relative','webkittransitionduration':'0ms','moztransitionduration':'0ms','mstransitionduration':'0ms','otransitionduration':'0ms','transitionduration':'0ms'} if(this.vertical){ css['height']=this.height*this.length+'px'; css['top']=-this.height*this.index+'px'; this.css(this.container,{'height':this.height+'px'}); }else{ css['width']=this.width*this.length+'px'; css['left']=-this.width*this.index+'px'; } this.css(this.element,css); for(var i=0;i=this.length?0:index; speed=typeof speed == 'undefined' ? this.opt.speed : parseint(speed); var el=this.element, timer=null, style=el.style, _this=this, t=0, //动画开始时间 b=parseint(style[direction]) || 0, //初始量 c=-index*this[size]-b, //变化量 d=math.abs(c)this.index?this.length+this.index-offset:this.index-offset; this.slide(index); }, next:function(offset){ if(typeof offset == 'undefined') offset=1; this.slide((this.index+offset)%this.length); }, _start:function(e){ if(!this.touching)this.preventdefault(e); this.element.onclick=null this.startpos=this.getmousepoint(e); var style=this.element.style; style.webkittransitionduration = style.moztransitionduration = style.mstransitionduration = style.otransitionduration = style.transitionduration = '0ms'; this.scrolling=1;//滚动屏幕 this.starttime=new date(); }, _move:function(e){ if(!this.scrolling || e.touches && e.touches.length>1 || e.scale && e.scale !== 1) return; var direction=this.vertical?'top':'left', size=this.vertical?'height':'width', xy=this.vertical?'y':'x', yx=this.vertical?'x':'y'; this.endpos=this.getmousepoint(e); var offx=this.endpos[xy]-this.startpos[xy]; if(this.scrolling===2 || math.abs(offx)>=math.abs(this.endpos[yx]-this.startpos[yx])){ this.preventdefault(e); this.pause(); //暂停幻灯 offx=offx/((!this.index&&offx>0 || this.index==this.length-1&&offx<0) ? (math.abs(offx)/this[size]+1) : 1); this.element.style[direction]=-this.index*this[size]+offx+'px'; if(offx!=0)this.scrolling=2;//标记拖动(有效触摸)2 }else this.scrolling=0;//设置为摒弃标记0 }, _end:function(e){ if(typeof this.scrolling != 'undefined'){ try{ var xy=this.vertical?'y':'x', size=this.vertical?'height':'width', offx=this.endpos[xy]-this.startpos[xy]; if(this.scrolling===2)this.element.onclick=new function('return false;'); }catch(err){ offx=0; } if((new date()-this.starttime<250 && math.abs(offx)>this[size]*0.1 || math.abs(offx)>this[size]/2) && ((offx<0 && this.index+10 && this.index>0))){ offx>0?this.prev():this.next(); }else{ this.slide(this.index); } delete this.scrolling;//删掉标记 delete this.startpos; delete this.endpos; delete this.starttime; if(this.opt.auto>=0)this.begin(); } }, mousescroll:function(e){ if(this.opt.mousewheel){ this.preventdefault(e); e=e||window.event; var wheeldelta=e.wheeldelta || e.detail && e.detail*-1 || 0, flag=wheeldelta/math.abs(wheeldelta);//这里flag指鼠标滚轮的方向,1表示向上,-1向下 wheeldelta>0?this.next():this.prev(); } }, _transitionend:function(e){ this.opt.after.call(this, this.index, this.slides[this.index]); } } window.touchslider=touchslider; })(window, undefined);