/** * TouchSlider v1.2.4 * By qiqiboy, http://www.qiqiboy.com, http://weibo.com/qiqiboy, 2012/12/05 */ (function(window, undefined){ "use strict"; var hasTouch=("createTouch" in document) || ('ontouchstart' in window), testStyle=document.createElement('div').style, testVendor=(function(){ var cases={ 'OTransform':['-o-','otransitionend'], 'WebkitTransform':['-webkit-','webkitTransitionEnd'], 'MozTransform':['-moz-','transitionend'], 'msTransform':['-ms-','MSTransitionEnd'], 'transform':['','transitionend'] },prop; for(prop in cases){ if(prop in testStyle)return cases[prop]; } return false; })(), sg=[['width','left','right'],['height','top','bottom']], cssVendor=testVendor&&testVendor[0], toCase=function(str){ return (str+'').replace(/^-ms-/, 'ms-').replace(/-([a-z]|[0-9])/ig, function(all, letter){ return (letter+'').toUpperCase(); }); }, testCSS=function(prop){ var _prop=toCase(cssVendor+prop); return (prop in testStyle)&& prop || (_prop in testStyle)&& _prop; }, parseArgs=function(arg,dft){ for(var key in dft){ if(typeof arg[key]=='undefined'){ arg[key]=dft[key]; } } return arg; }, children=function(elem){ var children=elem.children||elem.childNodes, _ret=[],i=0; for(;i=this.length ? 0 : this.cfg.begin; if(this.length<1)return false; switch(this.cfg.direction){ case 'up': case 'down':this.direction=this.cfg.direction; this.vertical=1; break; case 'right':this.direction='right'; default:this.direction=this.direction||'left'; this.vertical=0; break; } this.addListener(this.element,startEvent,this.bind(this._start,this),false); this.addListener(document,moveEvent,this.bind(this._move,this),false); this.addListener(document,endEvent,this.bind(this._end,this),false); this.addListener(document,'touchcancel',this.bind(this._end,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); if(this.cfg.mouseWheel){ this.addListener(this.element,'mousewheel',this.bind(this.mouseScroll,this),false); this.addListener(this.element,'DOMMouseScroll',this.bind(this.mouseScroll,this),false); } this.playing=this.cfg.auto; this.resize(); }, getSum:function(type,start,end){ var sum=0,i=start, _type=toCase('-'+type); for(;i=Math.abs(this.stopPos[1-this.vertical]-this.startPos[1-this.vertical])){ evt.preventDefault(); offset=offset/((!this.index&&offset>0 || this.index==this.length-1&&offset<0) ? (Math.abs(offset)/this[type]+1) : 1); this.element.style[direction]=this._pos+offset+'px'; if(offset&&typeof this.scrolling=='undefined'){ this.scrolling=true;//标记拖动(有效触摸) clearTimeout(this.timer);//暂停幻灯 clearTimeout(this.aniTimer);//暂停动画 } }else this.scrolling=false; }, _end:function(evt){ if(this.startPos){ if(this.scrolling){ var type=sg[this.vertical][0], direction=sg[this.vertical][1], offset=this.stopPos[this.vertical]-this.startPos[this.vertical], absOff=Math.abs(offset), sub=absOff/offset, myWidth,curPos,tarPos, next=this.index,off=0; this.addListener(this.element,'click',returnFalse); if(absOff>20){//有效移动距离 curPos=parseFloat(this.css(this.element,sg[this.vertical][1])); do{ if(next>=0 && nextmyWidth/2 && (next-=sub)); off=Math.abs(next-this.index); if(!off && +new Date-this.startTime<250){ off=1; } } offset>0?this.prev(off,false):this.next(off,false); this.playing && this.play(); } delete this._pos; delete this.stopPos; delete this.startPos; delete this.scrolling; delete this.startTime; } }, mouseScroll:function(evt){ if(this.cfg.mouseWheel){ evt=this.eventHook(evt); evt.preventDefault(); var _e=evt.origEvent; var wheelDelta=_e.wheelDelta || _e.detail && _e.detail*-1 || 0, flag=wheelDelta/Math.abs(wheelDelta); wheelDelta>0?this.prev(1,false):this.next(1,false); } }, transitionend:function(evt){ if(evt.propertyName==sg[this.vertical][1]){ this.cfg.after.call(this, this.index, this.slides[this.index]); this.playing && this.play(); } } } each(['Width','Height'],function(i,type){ var _type=type.toLowerCase(); each(['margin','padding','border'],function(j,name){ TouchSlider.fn[name+type]=function(elem){ return parseFloat(this.css(elem,name+'-'+sg[i][1]+(name=='border'?'-width':'')))+parseFloat(this.css(elem,name+'-'+sg[i][2]+(name=='border'?'-width':''))); } }); TouchSlider.fn['get'+type]=function(elem){ return elem['offset'+type]-this['padding'+type](elem)-this['border'+type](elem); } TouchSlider.fn['getOuter'+type]=function(elem){ return elem['offset'+type]+this['margin'+type](elem); } }); window.TouchSlider=TouchSlider; })(window);