UNPKG

vm-swipe

Version:

jQuery plugin for swipe HTML blocks

1 lines 5.68 kB
!function(t){function e(e){return e||(e={}),e=t.extend({},t.fn.vmSwipe.defaults,e),this.each(function(){var r=t(this),o=r.data(i);o||(o=new n(r,e),r.data(i,o))})}function n(e,n){function c(n){if(!C(t(n.target))){n.preventDefault(),n=n.originalEvent?n.originalEvent:n;var i=n.touches;n=i?i[0]:n,O=0,P=0,q=null,B=null,F={},R=0,U=0,G={},L=null,N=0,e.toggleClass("moving","mousedown"===n.type),f(),m(n),Z.stop(),t(document).on("touchend.vmswipe mouseup.vmswipe",t.proxy(l,e)),t(document).on("touchmove.vmswipe mousemove.vmswipe",t.proxy(h,e))}}function h(t){t=t.originalEvent?t.originalEvent:t;var e=t.touches;t=e?e[0]:t,w(t),X(),U=V=W(),B=H(J.last,J.end),P=E(J.end,J.last),O=E(J.end,J.start),L=Y(),g(Z,B,P,I)}function l(){U=W(),F=T(),G=j(O,F),Q(),e.removeClass("moving"),t(document).off("touchmove.vmswipe mousemove.vmswipe",t.proxy(h,e)),t(document).off("touchend.vmswipe mouseup.vmswipe",t.proxy(l,e))}function d(e,n){var i,r=t(window).width(),o=[].slice.call(arguments,2);t(window).resize(function(){r!==t(window).width()&&(i&&clearTimeout(i),r=t(window).width(),i=setTimeout(function(){n.apply(e,o),clearTimeout(i)},150))})}function f(){D.transform?(I=Z.css("transform").replace(/.*\(|\)| /g,"").split(","),I={x:+I[16===I.length?12:4],y:+I[16===I.length?13:5]}):(I=Z.position(),I={x:+I.left,y:+I.top})}function x(t){t.contents().wrapAll(p())}function p(){var i=t("<div />",{"class":"vm-swipe-inner"});return i.css({width:"horizontal"===n.direction||"both"===n.direction?y(e):"",height:"vertical"===n.direction||"both"===n.direction?v(e):"",transform:"translate3d(0px, 0px, 0px)"}),i}function y(e){var n=0;return e.children().each(function(){var e=t(this);n+=e.outerWidth(!0)}),n}function v(e){var n=0;return e.children().each(function(){var e=t(this);n+=e.outerHeight(!0)}),n}function m(t){var e={start:{x:0,y:0},last:{x:0,y:0},end:{x:0,y:0}};return e.start.x=e.last.x=e.end.x=t.pageX||t.clientX,e.start.y=e.last.y=e.end.y=t.pageY||t.clientY,J=e,e}function w(t){return Object.keys(J).length||m(t),J.last.x=J.end.x,J.last.y=J.end.y,J.end.x=t.pageX||t.clientX,J.end.y=t.pageY||t.clientY,J}function g(t,n,i,r){switch(K){case"horizontal":if("up"===n||"down"===n)return;r.x+=i.x,r.x=b(t.outerWidth(),e.outerWidth(),r.x);break;case"vertical":if("left"===n||"right"===n)return;r.y+=i.y,r.y=b(t.outerHeight(),e.outerHeight(),r.y);break;case"both":r.x+=i.x,r.y+=i.y,r.x=b(t.outerWidth(),e.outerWidth(),r.x),r.y=b(t.outerHeight(),e.outerHeight(),r.y)}t.css({transform:"translate3d("+r.x+"px, "+r.y+"px, 0px)"})}function b(t,e,n){return t<=e?0:(n>0&&(n=0),Math.abs(n)>t-e&&(n=-(t-e)),n)}function M(e){var n=t("<support />").get(0).style;t.each(e,function(t,i){void 0!==n[t]&&(e[t]=!0)})}function W(){return(new Date).getTime()}function H(t,e){if(k(t,e))return u;var n=z(t,e);return n<=45&&n>=0?r:n<=360&&n>=315?r:n>=135&&n<=225?o:n>45&&n<135?s:a}function k(t,e){return t.x===e.x&&t.y===e.y}function z(t,e){var n=t.x-e.x,i=e.y-t.y,r=Math.atan2(i,n),o=Math.round(180*r/Math.PI);return o<0&&(o=360-Math.abs(o)),o}function E(t,e){return{x:t.x-e.x,y:t.y-e.y}}function j(t,e){return{x:t.x/e,y:t.y/e}}function T(){var t;return t=R?U-R:0}function S(){t(document).off(".vmswipe"),e.off(".vmswipe")}function X(){U||(R=W())}function Y(){var t,e,n=W();switch(N||(N=n),K){case"horizontal":t=Math.abs(J.end.x-J.last.x);break;case"vertical":t=Math.abs(J.end.y-J.last.y);break;case"both":t=Math.max(Math.abs(J.end.x-J.last.x),Math.abs(J.end.y-J.last.y))}return e=n-N,t<10&&e>100}function Q(){var e;switch(K){case"horizontal":e=Math.abs(O.x);break;case"vertical":e=Math.abs(O.y);break;case"both":e=Math.max(Math.abs(O.x),Math.abs(O.y))}var i=U-V<50,r=t(window).width()<=n.inertiaMaxWidth,o=e>n.inertiaThreshold;i&&!L&&r&&o&&A()}function A(){Z.css({"text-indent":100}),Z.animate({textIndent:0},{duration:5e3*Math.max(Math.abs(G.x),Math.abs(G.y)),step:function(t){var e=W(),n=e-U;U=e,G.x*=t/100,G.y*=t/100,P.x=G.x*n,P.y=G.y*n,g(Z,B,P,I)}})}function C(t){for(var e=0,i=n.activeElements.length;e<i;e++)if(t.closest(n.activeElements[e],Z).length)return!0;return!1}var n=t.extend({},n),D={transition:null,animation:null,transform:null},I={x:0,y:0},O=0,P=null,q=null,B=null,F={},G={},J={},K=n.direction;M(D);var L,N,R=0,U=0,V=0;x(e);var Z=e.find(".vm-swipe-inner");try{e.on("touchstart.vmswipe mousedown.vmswipe",c),e.on("touchcancel.vmswipe",l)}catch($){t.error("events not supported on jQuery.swipe")}this.destroy=function(){S(),e.data(i,null),Z.children().unwrap()},this.refresh=function(){switch(K){case"horizontal":Z.width(y(Z)),I.x=b(Z.outerWidth(),e.outerWidth(),I.x);break;case"vertical":Z.height(v(Z)),I.y=b(Z.outerHeight(),e.outerHeight(),I.y);break;case"both":Z.width(y(Z)),Z.height(v(Z)),I.x=b(Z.outerWidth(),e.outerWidth(),I.x),I.y=b(Z.outerHeight(),e.outerHeight(),I.y)}Z.css({transform:"translate3d("+I.x+"px, "+I.y+"px, 0px)"})},d(null,function(){switch(K){case"horizontal":I.x=b(Z.outerWidth(),e.outerWidth(),I.x);break;case"vertical":I.y=b(Z.outerHeight(),e.outerHeight(),I.y);break;case"both":I.x=b(Z.outerWidth(),e.outerWidth(),I.x),I.y=b(Z.outerHeight(),e.outerHeight(),I.y)}Z.css({transform:"translate3d("+I.x+"px, "+I.y+"px, 0px)"})})}var i="vmSwipe",r="left",o="right",a="up",s="down",u="none",c={direction:"horizontal",inertiaMaxWidth:1030,inertiaThreshold:25,activeElements:["a"]};t.fn.vmSwipe=function(n){var r=t(this),o=r.data(i);if(o&&"string"==typeof n){if(o[n])return o[n].apply(this,Array.prototype.slice.call(arguments,1));t.error("Метод с именем "+n+" не существует для jQuery.vmSlider")}else if(o&&"object"==typeof n)o.option.apply(o,arguments);else if("object"==typeof n||!n)return e.apply(this,arguments);return r},t.fn.vmSwipe.defaults=c}(jQuery);