/* * TouchSlider * @author qiqiboy * @github https://github.com/qiqiboy/touchslider */ ; (function(ROOT, struct, undefined){ "use strict"; var VERSION='2.0.1'; var lastTime=0, nextFrame=ROOT.requestAnimationFrame || ROOT.webkitRequestAnimationFrame || ROOT.mozRequestAnimationFrame || ROOT.msRequestAnimationFrame || function(callback){ var currTime=+new Date, delay=Math.max(1000/60,1000/60-(currTime-lastTime)); lastTime=currTime+delay; return setTimeout(callback,delay); }, cancelFrame=ROOT.cancelAnimationFrame || ROOT.webkitCancelAnimationFrame || ROOT.webkitCancelRequestAnimationFrame || ROOT.mozCancelRequestAnimationFrame || ROOT.msCancelRequestAnimationFrame || clearTimeout, DOC=ROOT.document, divstyle=DOC.createElement('div').style, cssVendor=function(){ var tests="-webkit- -moz- -o- -ms-".split(" "), prop; while(prop=tests.shift()){ if(camelCase(prop+'transform') in divstyle){ return prop; } } return ''; }(), transition=cssTest('transition'), toString=Object.prototype.toString, slice=[].slice, class2type={}, event2type={}, event2code={ click:4, mousewheel:5, dommousescroll:5, keydown:6, resize:7 }, POINTERTYPES={ 2:'touch', 3:'pen', 4:'mouse', pen:'pen' }, STARTEVENT=[], MOVEEVENT=[], EVENT=function(){ var ret={}, states={ start:1, down:1, move:2, end:3, up:3, cancel:3 }; each("mouse touch pointer MSPointer-".split(" "),function(prefix){ var _prefix=/pointer/i.test(prefix)?'pointer':prefix; ret[_prefix]=ret[_prefix]||{}; POINTERTYPES[_prefix]=_prefix; each(states,function(endfix,code){ var ev=camelCase(prefix+endfix); ret[_prefix][ev]=code; event2type[ev.toLowerCase()]=_prefix; event2code[ev.toLowerCase()]=code; if(code==1){ STARTEVENT.push(ev); }else{ MOVEEVENT.push(ev); } }); }); each("otransitionend oTransitionEnd webkitTransitionEnd mozTransitionEnd MSTransitionEnd transitionend".split(" "),function(ev){ STARTEVENT.push(ev); event2code[ev.toLowerCase()]=8; }); return ret; }(), POINTERS={ touch:{}, pointer:{}, mouse:{} }; each("Boolean Number String Function Array Date RegExp Object Error".split(" "),function(name){ class2type["[object "+name+"]"]=name.toLowerCase(); }); function type(obj){ if(obj==null){ return obj+""; } return typeof obj=='object'||typeof obj=='function' ? class2type[toString.call(obj)]||"object" : typeof obj; } function isArrayLike(elem){ var tp=type(elem); return !!elem && tp!='function' && tp!='string' && (elem.length===0 || elem.length && (elem.nodeType==1 || (elem.length-1) in elem)); } function camelCase(str){ return (str+'').replace(/^-ms-/, 'ms-').replace(/-([a-z]|[0-9])/ig, function(all, letter){ return (letter+'').toUpperCase(); }); } function cssTest(name){ var prop=camelCase(name), _prop=camelCase(cssVendor+prop); return (prop in divstyle) && prop || (_prop in divstyle) && _prop || ''; } function isFunction(func){ return type(func)=='function'; } function pointerLength(obj){ var len=0,key; if(type(obj.length)=='number'){ len=obj.length; }else if('keys' in Object){ len=Object.keys(obj).length; }else{ for(key in obj){ if(obj.hasOwnProperty(key)){ len++; } } } return len; } function pointerItem(obj,n){ return 'item' in obj?obj.item(n):function(){ var i=0,key; for(key in this){ if(i++==n){ return this[key]; } } }.call(obj,n); } function each(arr, iterate){ if(isArrayLike(arr)){ if(type(arr.forEach)=='function'){ return arr.forEach(iterate); } var i=0,len=arr.length,item; for(;i=Math.abs(rect[1-dir]-_rect[1-dir]); this.drag && this.fire('dragStart',ev); } if(this.drag){ if(!this.pages[index+(offset>0?-1:1)]){ offset/=Math.abs(offset)/this.size+2; } this.container.style[dir?'top':'left']=this.startPos+offset+'px'; this.fire('dragMove',ev); this._offset=offset; ev.preventDefault(); } } break; case 1: case 3: if(canDrag){ var self=this, index=this.current, type=this.direction?'top':'left', isDrag,offset,tm,nn,sub,curPos,tarPos,myWidth; if(ev.length&&(ev.eventCode==1||this.drag)){ nn=ev.target.nodeName.toLowerCase(); clearTimeout(this.eventTimer); if(!this.pointerType){ this.pointerType=ev.eventType; } this.startPos=parseFloat(getStyle(this.container,type))||0; if(transition){ this.container.style[transition]='none'; }else if(this.timer){ cancelFrame(this.timer); delete this.timer; } this.rect=[ev.clientX,ev.clientY]; this.time=+new Date; this.container.style[type]=this.startPos+'px'; if(ev.eventType!='touch' && (nn=='a' || nn=='img')){ ev.preventDefault(); } }else if(tm=this.time){ offset=this._offset||0; isDrag=this.drag; curPos=this.startPos+offset; tarPos=this.getPos(index); each("rect drag time startPos _offset".split(" "),function(prop){ delete self[prop]; }); if(isDrag){ sub=offset>0?1:-1; while(sub*(curPos-tarPos)>this.getOuterSize(this.pages[index])/2&&this.pages[index-sub]){ tarPos=this.getPos(index-=sub); } if(Math.abs(offset)>20&&+new Date-tm<500){ index-=sub; } this.fire('dragEnd',ev); ev.preventDefault(); } if(curPos!=tarPos){ this.slide(index); } this.eventTimer=setTimeout(function(){ delete self.pointerType; },30); } } break; case 4: if(this.timer){ ev.preventDefault(); } break; case 5: ev.preventDefault(); if(this.isStatic() && +new Date-this.latestTime>Math.max(1000-this.duration,0)){ var wd=ev.wheelDelta||-ev.detail; Math.abs(wd)>=3 && this[wd>0?'prev':'next'](); } break; case 6: var nn=ev.target.nodeName.toLowerCase(); if(this.isStatic() && nn!='input' && nn!='textarea' && nn!='select'){ switch(ev.keyCode||ev.which){ case 33: case 37: case 38: this.prev(); break; case 32: case 34: case 39: case 40: this.next(); break; case 35: this.slide(this.length-1); break; case 36: this.slide(0); break; } } break; case 7: this.resize(); break; case 8: if(oldEvent.propertyName==(this.direction?'top':'left')){ this.container.style[transition]='none'; this.end(); } break; } }, getSum:function(from,to){ var sum=0; while(from