/
meny.min.js
40 lines (40 loc) · 5.71 KB
/
meny.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*!
* meny 0.9
* http://lab.hakim.se/meny
* MIT licensed
*
* Created by Hakim El Hattab, @hakimel, http://hakim.se
*/
var Meny={create:function(a){return(function(){if(!a||!a.menuElement||!a.contentsElement){throw"You need to specify which menu and contents elements to use.";
return false;}if(a.menuElement.parentNode!==a.contentsElement.parentNode){throw"The menu and contents elements must have the same parent.";return false;
}var A=["Webkit","Moz","O","ms"],G="top",h="right",w="bottom",q="left";var E="WebkitPerspective" in document.body.style||"MozPerspective" in document.body.style||"msPerspective" in document.body.style||"OPerspective" in document.body.style||"perspective" in document.body.style;
var C={width:300,height:300,position:q,activateThreshold:40};var c={menuElement:a.menuElement,contentsElement:a.contentsElement,wrapper:a.menuElement.parentNode,cover:null};
var z=c.wrapper.offsetLeft,y=c.wrapper.offsetTop,s=null,p=null,I=false,m=false;var x,i,F,O,v;k(C,a);if(E){g();e();u();B();M();N();}else{c.menuElement.style.display="block";
console.log("Your browser doesn't support 3D transforms, fallback coming soon.");return false;}function g(){F="";O="";switch(C.position){case G:x="50% 0";
i="rotateX( 30deg ) translateY( -100% ) translateY( 8px )";v="translateY( "+C.height+"px ) rotateX( -15deg )";break;case h:x="100% 50%";i="rotateY( 30deg ) translateX( 100% ) translateX( -8px )";
v="translateX( -"+C.width+"px ) rotateY( -15deg )";break;case w:x="50% 100%";i="rotateX( -30deg ) translateY( 100% ) translateY( -8px )";v="translateY( -"+C.height+"px ) rotateX( 15deg )";
break;default:x="0 50%";i="rotateY( -30deg ) translateX( -100% ) translateX( 8px )";v="translateX( "+C.width+"px ) rotateY( 15deg )";break;}}function e(){t(c.wrapper,"meny-"+C.position);
c.wrapper.style[D("perspective")]="800px";c.wrapper.style[D("perspectiveOrigin")]=x;}function u(){c.cover=document.createElement("div");c.cover.style.display="block";
c.cover.style.position="absolute";c.cover.style.width="100%";c.cover.style.height="100%";c.cover.style.left=0;c.cover.style.top=0;c.cover.style.visibility="hidden";
c.cover.style.zIndex=1000;c.cover.style.background="rgba( 0, 0, 0, 0.4 )";c.cover.style.opacity=0;c.cover.style[D("transition")]="all .5s ease";c.contentsElement.appendChild(c.cover);
}function B(){switch(C.position){case G:c.menuElement.style.width="100%";c.menuElement.style.height=C.height+"px";break;case h:c.menuElement.style.right="0";
c.menuElement.style.width=C.width+"px";c.menuElement.style.height="100%";break;case w:c.menuElement.style.bottom="0";c.menuElement.style.width="100%";c.menuElement.style.height=C.height+"px";
break;case q:c.menuElement.style.width=C.width+"px";c.menuElement.style.height="100%";break;}c.menuElement.style.display="block";c.menuElement.style.position="fixed";
c.menuElement.style.zIndex=1;c.menuElement.style[D("transform")]=i;c.menuElement.style[D("transformOrigin")]=x;c.menuElement.style[D("transition")]="all .5s ease";
}function M(){c.contentsElement.style[D("transform")]=O;c.contentsElement.style[D("transformOrigin")]=x;c.contentsElement.style[D("transition")]="all .5s ease";
}function N(){document.addEventListener("mousedown",J,false);document.addEventListener("mouseup",Q,false);document.addEventListener("mousemove",b,false);
document.addEventListener("touchstart",K,false);document.addEventListener("touchend",H,false);}function l(){if(!I){I=true;t(c.wrapper,"meny-active");c.cover.style.height=c.contentsElement.scrollHeight+"px";
c.cover.style.visibility="visible";c.cover.style.opacity=1;c.contentsElement.style[D("transform")]=v;c.menuElement.style[D("transform")]=F;}}function d(){if(I){I=false;
n(c.wrapper,"meny-active");c.cover.style.visibility="hidden";c.cover.style.opacity=0;c.contentsElement.style[D("transform")]=O;c.menuElement.style[D("transform")]=i;
}}function k(S,R){for(var T in R){S[T]=R[T];}}function D(V,U){var S=V.slice(0,1).toUpperCase()+V.slice(1);for(var T=0,R=A.length;T<R;T++){var W=A[T];if(typeof(U||document.body).style[W+S]!=="undefined"){return W+S;
}}return V;}function t(S,R){S.className=S.className.replace(/\s+$/gi,"")+" "+R;}function n(S,R){S.className=S.className.replace(R,"");}function J(R){m=true;
}function b(U){if(!m){var R=U.clientX-z,V=U.clientY-y;switch(C.position){case G:if(V>C.height){d();}else{if(V<C.activateThreshold){l();}}break;case h:var S=c.wrapper.offsetWidth;
if(R<S-C.width){d();}else{if(R>S-C.activateThreshold){l();}}break;case w:var T=c.wrapper.offsetHeight;if(V<T-C.height){d();}else{if(V>T-C.activateThreshold){l();
}}break;case q:if(R>C.width){d();}else{if(R<C.activateThreshold){l();}}break;}}}function Q(R){m=false;}function K(R){s=R.touches[0].clientX-z;touchStartY=R.touches[0].clientY-y;
p=null;touchMoveY=null;document.addEventListener("touchmove",L,false);}function L(S){p=S.touches[0].clientX-z;touchMoveY=S.touches[0].clientY-y;var R=null;
if(p<s-C.activateThreshold){R=P;}else{if(p>s+C.activateThreshold){R=o;}else{if(touchMoveY<touchStartY-C.activateThreshold){R=r;}}}if(touchMoveY>touchStartY+C.activateThreshold){R=f;
}if(R&&R()){S.preventDefault();}}function H(R){document.removeEventListener("touchmove",L,false);if(p===null&&touchMoveY===null){j();}}function j(){}function o(){if(C.position===h&&I){d();
return true;}else{if(C.position===q&&!I){l();return true;}}}function P(){if(C.position===h&&!I){l();return true;}else{if(C.position===q&&I){d();return true;
}}}function f(){if(C.position===w&&I){d();return true;}else{if(C.position===G&&!I){l();return true;}}}function r(){if(C.position===w&&!I){l();return true;
}else{if(C.position===G&&I){d();return true;}}}return{activate:l,deactivate:d,isActive:function(){return I;}};})();},getQuery:function(){var a={};location.search.replace(/[A-Z0-9]+?=([\w|:|\/\.]*)/gi,function(b){a[b.split("=").shift()]=b.split("=").pop();
});return a;}};