snap.min.js 10 KB

1
  1. !function(){var n={},t={},e={},r={};n=function(n){function t(){}function e(n,e){var r;return Object.create?r=Object.create(n):(t.prototype=n,r=new t),r.constructor=e,r}function r(n,t){if(n){if(n.nodeType)return[n];var t=t&&t.nodeType?t:document;return n&&"string"==typeof n?t.querySelectorAll(n):void 0}}for(var o=/\\?\{([^{}]+)\}/g,i="",s=/^[\s\xa0]+|[\s\xa0]+$/g,a=String.prototype.trim,l=a?function(n){return null==n?i:a.call(n)}:function(n){return null==n?i:(n+"").replace(s,i)},u=0,c=function(n){var t=n.offsetTop;return null!=n.offsetParent&&(t+=c(n.offsetParent)),t},p=function(n){var t=n.offsetLeft;return null!=n.offsetParent&&(t+=p(n.offsetParent)),t},f=({isObject:function(n){return n===Object(n)},isArray:Array.isArray||function(n){return"[object Array]"==toString.call(n)},isEmpty:function(n){if(null==n)return!0;if(this.isArray(n)||this.isString(n))return 0===n.length;for(var t in n)if(this.has(n,t))return!1;return!0},mix:function(n,t,e){for(var r in t)n[r]=t[r];return n},extend:function(n,t,r,o){if(!t||!n)return n;var i,s=t.prototype;return i=e(s,n),n.prototype=this.mix(i,n.prototype),n.superclass=e(s,t),r&&this.mix(i,r),o&&this.mix(n,o),n},startsWith:function(n,t){return 0===n.lastIndexOf(t,0)},endsWith:function(n,t){var e=n.length-t.length;return e>=0&&n.indexOf(t,e)===e},trim:l,substitute:function(n,t,e){return"string"==typeof n&&t?n.replace(e||o,function(n,e){return"\\"===n.charAt(0)?n.slice(1):void 0===t[e]?i:t[e]}):n},vendor:function(){for(var n,t=document.createElement("div").style,e=["t","webkitT","MozT","msT","OT"],r=0,o=e.length;r<o;r++)if(n=e[r]+"ransform",n in t)return e[r].substr(0,e[r].length-1);return!1}(),prefixStyle:function(n){return this.vendor!==!1&&(""===this.vendor?n:this.vendor+n.charAt(0).toUpperCase()+n.substr(1))},hasClass:function(n,t){return n&&n.className&&t&&n.className.indexOf(t)!=-1},addClass:function(n,t){n&&t&&!this.hasClass(n,t)&&(n.className+=" "+t)},removeClass:function(n,t){n&&n.className&&t&&(n.className=n.className.replace(t,""))},remove:function(n){n&&n.parentNode&&n.parentNode.removeChild(n)},getOffsetTop:c,getOffsetLeft:p,findParentEl:function(n,t,e){var r=null,o=null,i=/^#/.test(t)?"id":/^\./.test(t)?"class":"tag",s=t.replace(/\.|#/g,"");if(e&&"string"==typeof e&&(e=document.querySelector(e)),e=e||document.body,n&&t){if("class"==i&&n.className&&n.className.match(s))return n;if("id"==i&&n.id&&l(n.id)==s)return n;if("tag"==i&&n.tagName.toLowerCase()==s)return n;for(;!r&&o!=e&&(o=n.parentNode);){if("class"==i&&o.className&&o.className.match(s)||"id"==i&&o.id&&l(o.id)==s||"tag"==i&&o.tagName&&o.tagName.toLowerCase()==s)return r=o;n=o}return null}},guid:function(n){var t=++u+"";return n?n+t:t},isAndroid:function(){return/Android /.test(window.navigator.appVersion)},isBadAndroid:function(){return/Android /.test(window.navigator.appVersion)&&!/Chrome\/\d/.test(window.navigator.appVersion)},px2Num:function(n){return Number(n.replace(/px/,""))},getNodes:r,getNode:function(n,t){var e=r(n,t);return e&&e[0]},stringifyStyle:function(n){var t="";for(var e in n)t+=[e,":",n[e],";"].join("");return t}}),d=["Arguments","Function","String","Number","Date","RegExp"],h=0;h<d.length;h++)f["is"+d[h]]=function(n){return toString.call(n)=="[object "+d[h]+"]"};return"object"==typeof module&&module.exports?n=f:f}(n),t=function(t){var e=n,r=function(n){var t,e=!1;return function(){return e?t:(e=!0,t=n.apply(this,arguments),n=null,t)}},o={on:function(n,t,e){if(!s(this,"on",n,[t,e])||!t)return this;this._events||(this._events={});var r=this._events[n]||(this._events[n]=[]);return r.push({callback:t,context:e,ctx:e||this}),this},once:function(n,t,e){if(!s(this,"once",n,[t,e])||!t)return this;var o=this,i=r(function(){o.off(n,i),t.apply(this,arguments)});return i._callback=t,this.on(n,i,e)},off:function(n,t,e){if(!this._events||!s(this,"off",n,[t,e]))return this;if(!n&&!t&&!e)return this._events=void 0,this;for(var r=n?[n]:Object.keys(this._events),o=0,i=r.length;o<i;o++){n=r[o];var a=this._events[n];if(a)if(t||e){for(var l=[],u=0,c=a.length;u<c;u++){var p=a[u];(t&&t!==p.callback&&t!==p.callback._callback||e&&e!==p.context)&&l.push(p)}l.length?this._events[n]=l:delete this._events[n]}else delete this._events[n]}return this},trigger:function(n){if(!this._events)return this;var t=Array.prototype.slice.call(arguments,1);if(!s(this,"trigger",n,t))return this;var e=this._events[n],r=this._events.all;return e&&a(e,t),r&&a(r,arguments),this},listenTo:function(n,t,r){var o=this._listeningTo||(this._listeningTo={}),i=n._listenId||(n._listenId=e.guid("l"));return o[i]=n,r||"object"!=typeof t||(r=this),n.on(t,r,this),this},listenToOnce:function(n,t,e){if("object"==typeof t){for(var o in t)this.listenToOnce(n,o,t[o]);return this}var i=r(function(){this.stopListening(n,t,i),e.apply(this,arguments)});return i._callback=e,this.listenTo(n,t,i)},stopListening:function(n,t,r){var o=this._listeningTo;if(!o)return this;var i=!t&&!r;r||"object"!=typeof t||(r=this),n&&((o={})[n._listenId]=n);for(var s in o)n=o[s],n.off(t,r,this),(i||e.isEmpty(n._events))&&delete this._listeningTo[s];return this}},i=/\s+/,s=function(n,t,e,r){if(!e)return!0;if("object"==typeof e){for(var o in e)n[t].apply(n,[o,e[o]].concat(r));return!1}if(i.test(e)){for(var s=e.split(i),a=0,l=s.length;a<l;a++)n[t].apply(n,[s[a]].concat(r));return!1}return!0},a=function(n,t){var e,r=-1,o=n.length,i=t[0],s=t[1],a=t[2];switch(t.length){case 0:for(;++r<o;)(e=n[r]).callback.call(e.ctx);return;case 1:for(;++r<o;)(e=n[r]).callback.call(e.ctx,i);return;case 2:for(;++r<o;)(e=n[r]).callback.call(e.ctx,i,s);return;case 3:for(;++r<o;)(e=n[r]).callback.call(e.ctx,i,s,a);return;default:for(;++r<o;)(e=n[r]).callback.apply(e.ctx,t);return}};return o.bind=o.on,o.unbind=o.off,"object"==typeof module&&module.exports?t=o:o}(t),e=function(e){var r=n,o=t,i=function(){};return r.mix(i.prototype,o),r.mix(i.prototype,{plug:function(n){var t=this;if(n&&n.pluginId){t.__plugins||(t.__plugins=[]);var e=t.getPlugin(n.pluginId);return e&&t.unplug(n.pluginId),n.pluginInitializer(t),t.__plugins.push(n),t}},unplug:function(n){var t=this;if(n&&t.__plugins){var e="string"==typeof n?t.getPlugin(n):n;e.pluginDestructor(t);for(var r=0,o=t.__plugins.length;r<o;r++)if(t.__plugins[r]==e)return t.__plugins.splice(r,1)}},getPlugin:function(n){var t=this,e=[];if(t.__plugins){for(var r=0,o=t.__plugins.length;r<o;r++)t.__plugins[r]&&t.__plugins[r].pluginId==n&&e.push(t.__plugins[r]);return e.length>1?e:e[0]||null}}}),"object"==typeof module&&module.exports?e=i:i}(e),r=function(t){var r=n,o=e,i=function(n){i.superclass.constructor.call(this,n),this.userConfig=r.mix({snapColIndex:0,snapRowIndex:0,snapDuration:500,snapEasing:"ease",snapOffsetLeft:0,snapOffsetTop:0,autoStep:!1},n)};if(r.extend(i,o,{pluginId:"snap",pluginInitializer:function(n){var t=this;t.xscroll=n.render(),t.snapColIndex=t.userConfig.snapColIndex,t.snapRowIndex=t.userConfig.snapRowIndex,t.render()},pluginDestructor:function(){var n=this,t=n.xscroll;t.on("panend",t._onpanend,t),t.off("panend",n._snapAnimate,n)},snapTo:function(n,t,e,r,o){return this.snapToCol(n,e,r,o),this.snapToRow(t,e,r,o),this},snapToCol:function(n,t,e,r){var o=this,i=o.xscroll,s=o.userConfig,t=t||s.snapDuration,e=e||s.snapEasing,a=s.snapWidth,l=s.snapColsNum,u=s.snapOffsetLeft;n=n>=l?l-1:n<0?0:n,o.prevColIndex=o.snapColIndex,o.snapColIndex=n;var c=o.snapColIndex*a+u;return c>i.containerWidth-i.boundry.width&&(c=i.containerWidth-i.boundry.width),i.scrollLeft(c,t,e,r),o},_colChange:function(n){var t=this;return t.prevColIndex!=t.snapColIndex&&t.trigger("colchange",r.mix(n,{type:"colchange",curColIndex:t.snapColIndex,prevColIndex:t.prevColIndex})),t},snapToRow:function(n,t,e,r){var o=this,i=o.xscroll,s=o.userConfig,t=t||s.snapDuration,e=e||s.snapEasing,a=s.snapHeight,l=s.snapRowsNum,u=s.snapOffsetTop;n=n>=l?l-1:n<0?0:n,o.prevRowIndex=o.snapRowIndex,o.snapRowIndex=n;var c=o.snapRowIndex*a+u;return c>i.containerHeight-i.boundry.height&&(c=i.containerHeight-i.boundry.height),o.xscroll.scrollTop(c,t,e,r),o},_rowChange:function(n){var t=this;return t.prevRowIndex!=t.snapRowIndex&&t.trigger("rowchange",r.mix(n,{type:"rowchange",curRowIndex:t.snapRowIndex,prevRowIndex:t.prevRowIndex})),t},_snapAnimate:function(n){var t=this,e=t.userConfig,r=e.snapWidth,o=e.snapHeight;t.xscroll.__topstart=null,t.xscroll.__leftstart=null;var i=n.direction;if(Math.abs(n.velocity)<=.2){var s=t.xscroll.getScrollLeft(),a=t.xscroll.getScrollTop(),l=Math.round(s/r),u=Math.round(a/o);t.snapTo(l,u)}else if(e.autoStep){var c=t.xscroll.computeScroll("x",n.velocityX),p=t.xscroll.computeScroll("y",n.velocityY),l=c&&c.pos?Math.round(c.pos/r):t.snapColIndex,u=p&&p.pos?Math.round(p.pos/o):t.snapRowIndex,f=Math.ceil(c&&c.duration,p&&p.duration);c&&"inside"==c.status?t.snapToCol(l,f,c&&c.easing,function(){t.xscroll.boundryCheckX()}):c&&t.xscroll.scrollLeft(c.pos,c.duration,c.easing,function(){t.xscroll.boundryCheckX(),t.prevColIndex=t.snapColIndex,t.snapColIndex=Math.round(Math.abs(t.xscroll.getScrollLeft())/r)}),p&&"inside"==p.status?t.snapToRow(u,f,p&&p.easing,function(){t.xscroll.boundryCheckY()}):p&&t.xscroll.scrollTop(p.pos,p.duration,p.easing,function(){t.xscroll.boundryCheckY(),t.prevRowIndex=t.snapRowIndex,t.snapRowIndex=Math.round(Math.abs(t.xscroll.getScrollTop())/o)})}else 2==i?t.snapColIndex++:4==i?t.snapColIndex--:void 0,8==i?t.snapRowIndex++:16==i?t.snapRowIndex--:void 0,t.snapTo(t.snapColIndex,t.snapRowIndex)},render:function(){var n=this,t=n.xscroll;return n.userConfig.snapWidth=n.userConfig.snapWidth||t.width||100,n.userConfig.snapHeight=n.userConfig.snapHeight||t.height||100,n.userConfig.snapColsNum=n.userConfig.snapColsNum||Math.max(Math.round(t.containerWidth/t.width),1),n.userConfig.snapRowsNum=n.userConfig.snapRowsNum||Math.max(Math.round(t.containerHeight/t.height),1),t.off("panend",t._onpanend),t.on("panend",n._snapAnimate,n),n._bindEvt(),n},_bindEvt:function(){var n=this,t=n.xscroll;n._isEvtBinded||(n._isEvtBinded=!0,t.on("scrollend",function(e){"y"!=e.zoomType||t.isBoundryOutTop()||t.isBoundryOutBottom()||n._rowChange(e)}),t.on("scrollend",function(e){"x"!=e.zoomType||t.isBoundryOutLeft()||t.isBoundryOutRight()||n._colChange(e)}))}}),"object"==typeof module&&module.exports)t=i;else if(window.XScroll&&window.XScroll.Plugins)return XScroll.Plugins.Snap=i;return t}(r)}();