Instantly share code, notes, and snippets.
Created
April 16, 2013 15:43
-
Star
0
(0)
You must be signed in to star a gist -
Fork
1
(1)
You must be signed in to fork a gist
-
-
Save johnny5th/5397018 to your computer and use it in GitHub Desktop.
Click to open nav menu for mobile screens based on drupal. Includes jquery.animate-enhanced and FastClick
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @file | |
| * A JavaScript file for the theme. | |
| * | |
| * In order for this JavaScript to be loaded on pages, see the instructions in | |
| * the README.txt next to this file. | |
| */ | |
| // JavaScript should be made compatible with libraries other than jQuery by | |
| // wrapping it with an "anonymous closure". See: | |
| // - http://drupal.org/node/1446420 | |
| // - http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth | |
| (function ($, Drupal, window, document, undefined) { | |
| $(function(){ | |
| var menu_button = $('#block-block-2') | |
| var main_menu_block = $('#block-menu-block-2'); | |
| clickToToggle(menu_button, main_menu_block, function(is_open){ | |
| if(is_open) { | |
| main_menu_block.css('height',''); | |
| } | |
| }); | |
| $('.menu-block-wrapper > ul > li > a', main_menu_block).each(function(){ | |
| var elem = $(this); | |
| elem.removeAttr('href'); | |
| clickToToggle(elem, elem.parent().find('> ul')); | |
| }); | |
| new FastClick(document.body); | |
| }); | |
| function clickToToggle(click, open, callback) { | |
| var menu_height = 0; | |
| click.click(function(){ | |
| if(open.height() > menu_height) | |
| menu_height = open.height(); | |
| if(open.hasClass('closed') || open.css('display') == "none") { | |
| open.css({display: "block", height: 0}).addClass('open'); | |
| click.addClass('open').removeClass('closed'); | |
| open.animate({ | |
| height: menu_height, | |
| useTranslate3d: true | |
| }, 300, function() { | |
| this.removeClass('closed'); | |
| if(callback) callback(true); | |
| }); | |
| } else { | |
| open.css('height', menu_height).addClass('closed'); | |
| click.addClass('closed').removeClass('open'); | |
| open.animate({ | |
| height: 0, | |
| useTranslate3d: true | |
| }, 300, function() { | |
| console.log('closed'); | |
| this.removeClass('open'); | |
| if(callback) callback(false); | |
| }); | |
| } | |
| }); | |
| } | |
| })(jQuery, Drupal, this, this.document); | |
| /* | |
| jquery.animate-enhanced plugin v0.99.1 | |
| --- | |
| http://github.com/benbarnett/jQuery-Animate-Enhanced | |
| http://benbarnett.net | |
| @benpbarnett | |
| */ | |
| (function(c,J,K){function P(a,b,c,f,m,l,h,q,r){var u=!1;h=!0===h&&!0===q;b=b||{};b.original||(b.original={},u=!0);b.properties=b.properties||{};b.secondary=b.secondary||{};q=b.meta;for(var n=b.original,g=b.properties,Q=b.secondary,C=s.length-1;0<=C;C--){var E=s[C]+"transition-property",F=s[C]+"transition-duration",j=s[C]+"transition-timing-function";c=h?s[C]+"transform":c;u&&(n[E]=a.css(E)||"",n[F]=a.css(F)||"",n[j]=a.css(j)||"");Q[c]=h?(!0===r||!0===G&&!1!==r)&&L?"translate3d("+q.left+"px, "+q.top+ | |
| "px, 0)":"translate("+q.left+"px,"+q.top+"px)":l;g[E]=(g[E]?g[E]+",":"")+c;g[F]=(g[F]?g[F]+",":"")+f+"ms";g[j]=(g[j]?g[j]+",":"")+m}return b}function A(a){for(var c in a)return!1;return!0}function H(a){return parseFloat(a.replace(a.match(/\D+$/),""))}function M(a){var c=!0;a.each(function(a,f){return c=c&&f.ownerDocument});return c}var R="top right bottom left opacity height width".split(" "),I=["top","right","bottom","left"],s=["-webkit-","-moz-","-o-",""],S=["avoidTransforms","useTranslate3d","leaveTransforms"], | |
| T=/^([+-]=)?([\d+-.]+)(.*)$/,U=/([A-Z])/g,V={secondary:{},meta:{top:0,right:0,bottom:0,left:0}},N=null,B=!1,y=(document.body||document.documentElement).style,O=void 0!==y.WebkitTransition||void 0!==y.MozTransition||void 0!==y.OTransition||void 0!==y.transition,L="WebKitCSSMatrix"in window&&"m11"in new WebKitCSSMatrix,G=L;c.expr&&c.expr.filters&&(N=c.expr.filters.animated,c.expr.filters.animated=function(a){return c(a).data("events")&&c(a).data("events")["webkitTransitionEnd oTransitionEnd transitionend"]? | |
| !0:N.call(this,a)});c.extend({toggle3DByDefault:function(){return G=!G},toggleDisabledByDefault:function(){return B=!B},setDisabledByDefault:function(a){return B=a}});c.fn.translation=function(){if(!this[0])return null;var a=window.getComputedStyle(this[0],null),c={x:0,y:0};if(a)for(var p=s.length-1;0<=p;p--){var f=a.getPropertyValue(s[p]+"transform");if(f&&/matrix/i.test(f)){a=f.replace(/^matrix\(/i,"").split(/, |\)$/g);c={x:parseInt(a[4],10),y:parseInt(a[5],10)};break}}return c};c.fn.animate=function(a, | |
| b,p,f){a=a||{};var m=!("undefined"!==typeof a.bottom||"undefined"!==typeof a.right),l=c.speed(b,p,f),h=this,q=0,r=function(){q--;0===q&&"function"===typeof l.complete&&l.complete.apply(h,arguments)},u;if(!(u=!0===("undefined"!==typeof a.avoidCSSTransitions?a.avoidCSSTransitions:B)))if(!(u=!O))if(!(u=A(a))){var n;a:{for(n in a)if(("width"==n||"height"==n)&&("show"==a[n]||"hide"==a[n]||"toggle"==a[n])){n=!0;break a}n=!1}u=n||0>=l.duration||!0===c.fn.animate.defaults.avoidTransforms&&!1!==a.avoidTransforms}return u? | |
| J.apply(this,arguments):this[!0===l.queue?"queue":"each"](function(){var g=c(this),b=c.extend({},l),h=function(b){var f=g.data("jQe")||{original:{}},e={};if(2==b.eventPhase){if(!0!==a.leaveTransforms){for(b=s.length-1;0<=b;b--)e[s[b]+"transform"]="";if(m&&"undefined"!==typeof f.meta){b=0;for(var d;d=I[b];++b)e[d]=f.meta[d+"_o"]+"px",c(this).css(d,e[d])}}g.unbind("webkitTransitionEnd oTransitionEnd transitionend").css(f.original).css(e).data("jQe",null);"hide"===a.opacity&&g.css({display:"none",opacity:""}); | |
| r.call(this)}},f={bounce:"cubic-bezier(0.0, 0.35, .5, 1.3)",linear:"linear",swing:"ease-in-out",easeInQuad:"cubic-bezier(0.550, 0.085, 0.680, 0.530)",easeInCubic:"cubic-bezier(0.550, 0.055, 0.675, 0.190)",easeInQuart:"cubic-bezier(0.895, 0.030, 0.685, 0.220)",easeInQuint:"cubic-bezier(0.755, 0.050, 0.855, 0.060)",easeInSine:"cubic-bezier(0.470, 0.000, 0.745, 0.715)",easeInExpo:"cubic-bezier(0.950, 0.050, 0.795, 0.035)",easeInCirc:"cubic-bezier(0.600, 0.040, 0.980, 0.335)",easeInBack:"cubic-bezier(0.600, -0.280, 0.735, 0.045)", | |
| easeOutQuad:"cubic-bezier(0.250, 0.460, 0.450, 0.940)",easeOutCubic:"cubic-bezier(0.215, 0.610, 0.355, 1.000)",easeOutQuart:"cubic-bezier(0.165, 0.840, 0.440, 1.000)",easeOutQuint:"cubic-bezier(0.230, 1.000, 0.320, 1.000)",easeOutSine:"cubic-bezier(0.390, 0.575, 0.565, 1.000)",easeOutExpo:"cubic-bezier(0.190, 1.000, 0.220, 1.000)",easeOutCirc:"cubic-bezier(0.075, 0.820, 0.165, 1.000)",easeOutBack:"cubic-bezier(0.175, 0.885, 0.320, 1.275)",easeInOutQuad:"cubic-bezier(0.455, 0.030, 0.515, 0.955)",easeInOutCubic:"cubic-bezier(0.645, 0.045, 0.355, 1.000)", | |
| easeInOutQuart:"cubic-bezier(0.770, 0.000, 0.175, 1.000)",easeInOutQuint:"cubic-bezier(0.860, 0.000, 0.070, 1.000)",easeInOutSine:"cubic-bezier(0.445, 0.050, 0.550, 0.950)",easeInOutExpo:"cubic-bezier(1.000, 0.000, 0.000, 1.000)",easeInOutCirc:"cubic-bezier(0.785, 0.135, 0.150, 0.860)",easeInOutBack:"cubic-bezier(0.680, -0.550, 0.265, 1.550)"},n={},f=f[b.easing||"swing"]?f[b.easing||"swing"]:b.easing||"swing",j;for(j in a)if(-1===c.inArray(j,S)){var p=-1<c.inArray(j,I),k;var d=g,x=a[j],v=j,t=p&&!0!== | |
| a.avoidTransforms;if("d"==v)k=void 0;else if(M(d)){var e=T.exec(x);k="auto"===d.css(v)?0:d.css(v);k="string"==typeof k?H(k):k;"string"==typeof x&&H(x);var t=!0===t?0:k,u=d.is(":hidden"),w=d.translation();"left"==v&&(t=parseInt(k,10)+w.x);"right"==v&&(t=parseInt(k,10)+w.x);"top"==v&&(t=parseInt(k,10)+w.y);"bottom"==v&&(t=parseInt(k,10)+w.y);!e&&"show"==x?(t=1,u&&d.css({display:"LI"==d.context.tagName?"list-item":"block",opacity:0})):!e&&"hide"==x&&(t=0);e?(d=parseFloat(e[2]),e[1]&&(d=("-="===e[1]? | |
| -1:1)*d+parseInt(t,10)),k=d):k=t}else k=void 0;if(e=!0!==a.avoidTransforms)if(e=j,d=k,x=g,M(x)){v=-1<c.inArray(e,R);if(("width"==e||"height"==e||"opacity"==e)&&parseFloat(d)===parseFloat(x.css(e)))v=!1;e=v}else e=!1;if(e){e=g;d=j;x=b.duration;v=f;k=p&&!0===a.avoidTransforms?k+"px":k;var p=p&&!0!==a.avoidTransforms,t=m,u=!0===a.useTranslate3d,w=(w=e.data("jQe"))&&!A(w)?w:c.extend(!0,{},V),z=k;if(-1<c.inArray(d,I)){var D=w.meta,B=H(e.css(d))||0,y=d+"_o",z=k-B;D[d]=z;D[y]="auto"==e.css(d)?0+z:B+z||0; | |
| w.meta=D;t&&0===z&&(z=0-D[y],D[d]=z,D[y]=0)}e.data("jQe",P(e,w,d,x,v,z,p,t,u))}else n[j]=a[j]}g.unbind("webkitTransitionEnd oTransitionEnd transitionend");if((j=g.data("jQe"))&&!A(j)&&!A(j.secondary)){q++;g.css(j.properties);var G=j.secondary;setTimeout(function(){g.bind("webkitTransitionEnd oTransitionEnd transitionend",h).css(G)})}else b.queue=!1;A(n)||(q++,J.apply(g,[n,{duration:b.duration,easing:c.easing[b.easing]?b.easing:c.easing.swing?"swing":"linear",complete:r,queue:b.queue}]));return!0})}; | |
| c.fn.animate.defaults={};c.fn.stop=function(a,b,p){if(!O)return K.apply(this,[a,b]);a&&this.queue([]);this.each(function(){var f=c(this),m=f.data("jQe");if(m&&!A(m)){var l,h={};if(b){if(h=m.secondary,!p&&void 0!==typeof m.meta.left_o||void 0!==typeof m.meta.top_o){h.left=void 0!==typeof m.meta.left_o?m.meta.left_o:"auto";h.top=void 0!==typeof m.meta.top_o?m.meta.top_o:"auto";for(l=s.length-1;0<=l;l--)h[s[l]+"transform"]=""}}else if(!A(m.secondary)){var q=window.getComputedStyle(f[0],null);if(q)for(var r in m.secondary)if(m.secondary.hasOwnProperty(r)&& | |
| (r=r.replace(U,"-$1").toLowerCase(),h[r]=q.getPropertyValue(r),!p&&/matrix/i.test(h[r]))){l=h[r].replace(/^matrix\(/i,"").split(/, |\)$/g);h.left=parseFloat(l[4])+parseFloat(f.css("left"))+"px"||"auto";h.top=parseFloat(l[5])+parseFloat(f.css("top"))+"px"||"auto";for(l=s.length-1;0<=l;l--)h[s[l]+"transform"]=""}}f.unbind("webkitTransitionEnd oTransitionEnd transitionend");f.css(m.original).css(h).data("jQe",null)}else K.apply(f,[a,b])});return this}})(jQuery,jQuery.fn.animate,jQuery.fn.stop); | |
| /* | |
| FastClick: polyfill to remove click delays on browsers with touch UIs. | |
| @version 0.6.3 | |
| @codingstandard ftlabs-jsv2 | |
| @copyright The Financial Times Limited [All Rights Reserved] | |
| @license MIT License (see LICENSE.txt) | |
| */ | |
| function FastClick(layer){var oldOnClick,self=this;this.trackingClick=false;this.trackingClickStart=0;this.targetElement=null;this.touchStartX=0;this.touchStartY=0;this.lastTouchIdentifier=0;this.layer=layer;if(!layer||!layer.nodeType)throw new TypeError("Layer must be a document node");this.onClick=function(){return FastClick.prototype.onClick.apply(self,arguments)};this.onMouse=function(){return FastClick.prototype.onMouse.apply(self,arguments)};this.onTouchStart=function(){return FastClick.prototype.onTouchStart.apply(self, | |
| arguments)};this.onTouchEnd=function(){return FastClick.prototype.onTouchEnd.apply(self,arguments)};this.onTouchCancel=function(){return FastClick.prototype.onTouchCancel.apply(self,arguments)};if(FastClick.notNeeded())return;if(this.deviceIsAndroid){layer.addEventListener("mouseover",this.onMouse,true);layer.addEventListener("mousedown",this.onMouse,true);layer.addEventListener("mouseup",this.onMouse,true)}layer.addEventListener("click",this.onClick,true);layer.addEventListener("touchstart",this.onTouchStart, | |
| false);layer.addEventListener("touchend",this.onTouchEnd,false);layer.addEventListener("touchcancel",this.onTouchCancel,false);if(!Event.prototype.stopImmediatePropagation){layer.removeEventListener=function(type,callback,capture){var rmv=Node.prototype.removeEventListener;if(type==="click")rmv.call(layer,type,callback.hijacked||callback,capture);else rmv.call(layer,type,callback,capture)};layer.addEventListener=function(type,callback,capture){var adv=Node.prototype.addEventListener;if(type==="click")adv.call(layer, | |
| type,callback.hijacked||(callback.hijacked=function(event){if(!event.propagationStopped)callback(event)}),capture);else adv.call(layer,type,callback,capture)}}if(typeof layer.onclick==="function"){oldOnClick=layer.onclick;layer.addEventListener("click",function(event){oldOnClick(event)},false);layer.onclick=null}}FastClick.prototype.deviceIsAndroid=navigator.userAgent.indexOf("Android")>0;FastClick.prototype.deviceIsIOS=/iP(ad|hone|od)/.test(navigator.userAgent); | |
| FastClick.prototype.deviceIsIOS4=FastClick.prototype.deviceIsIOS&&/OS 4_\d(_\d)?/.test(navigator.userAgent);FastClick.prototype.deviceIsIOSWithBadTarget=FastClick.prototype.deviceIsIOS&&/OS ([6-9]|\d{2})_\d/.test(navigator.userAgent);FastClick.prototype.needsClick=function(target){switch(target.nodeName.toLowerCase()){case "button":case "input":if(this.deviceIsIOS&&target.type==="file")return true;return target.disabled;case "label":case "video":return true;default:return/\bneedsclick\b/.test(target.className)}}; | |
| FastClick.prototype.needsFocus=function(target){switch(target.nodeName.toLowerCase()){case "textarea":case "select":return true;case "input":switch(target.type){case "button":case "checkbox":case "file":case "image":case "radio":case "submit":return false}return!target.disabled&&!target.readOnly;default:return/\bneedsfocus\b/.test(target.className)}}; | |
| FastClick.prototype.sendClick=function(targetElement,event){var clickEvent,touch;if(document.activeElement&&document.activeElement!==targetElement)document.activeElement.blur();touch=event.changedTouches[0];clickEvent=document.createEvent("MouseEvents");clickEvent.initMouseEvent("click",true,true,window,1,touch.screenX,touch.screenY,touch.clientX,touch.clientY,false,false,false,false,0,null);clickEvent.forwardedTouchEvent=true;targetElement.dispatchEvent(clickEvent)}; | |
| FastClick.prototype.focus=function(targetElement){var length;if(this.deviceIsIOS&&targetElement.setSelectionRange){length=targetElement.value.length;targetElement.setSelectionRange(length,length)}else targetElement.focus()}; | |
| FastClick.prototype.updateScrollParent=function(targetElement){var scrollParent,parentElement;scrollParent=targetElement.fastClickScrollParent;if(!scrollParent||!scrollParent.contains(targetElement)){parentElement=targetElement;do{if(parentElement.scrollHeight>parentElement.offsetHeight){scrollParent=parentElement;targetElement.fastClickScrollParent=parentElement;break}parentElement=parentElement.parentElement}while(parentElement)}if(scrollParent)scrollParent.fastClickLastScrollTop=scrollParent.scrollTop}; | |
| FastClick.prototype.getTargetElementFromEventTarget=function(eventTarget){if(eventTarget.nodeType===Node.TEXT_NODE)return eventTarget.parentNode;return eventTarget}; | |
| FastClick.prototype.onTouchStart=function(event){var targetElement,touch,selection;targetElement=this.getTargetElementFromEventTarget(event.target);touch=event.targetTouches[0];if(this.deviceIsIOS){selection=window.getSelection();if(selection.rangeCount&&!selection.isCollapsed)return true;if(!this.deviceIsIOS4){if(touch.identifier===this.lastTouchIdentifier){event.preventDefault();return false}this.lastTouchIdentifier=touch.identifier;this.updateScrollParent(targetElement)}}this.trackingClick=true; | |
| this.trackingClickStart=event.timeStamp;this.targetElement=targetElement;this.touchStartX=touch.pageX;this.touchStartY=touch.pageY;if(event.timeStamp-this.lastClickTime<200)event.preventDefault();return true};FastClick.prototype.touchHasMoved=function(event){var touch=event.changedTouches[0];if(Math.abs(touch.pageX-this.touchStartX)>10||Math.abs(touch.pageY-this.touchStartY)>10)return true;return false}; | |
| FastClick.prototype.findControl=function(labelElement){if(labelElement.control!==undefined)return labelElement.control;if(labelElement.htmlFor)return document.getElementById(labelElement.htmlFor);return labelElement.querySelector("button, input:not([type=hidden]), keygen, meter, output, progress, select, textarea")}; | |
| FastClick.prototype.onTouchEnd=function(event){var forElement,trackingClickStart,targetTagName,scrollParent,touch,targetElement=this.targetElement;if(this.touchHasMoved(event)){this.trackingClick=false;this.targetElement=null}if(!this.trackingClick)return true;if(event.timeStamp-this.lastClickTime<200){this.cancelNextClick=true;return true}this.lastClickTime=event.timeStamp;trackingClickStart=this.trackingClickStart;this.trackingClick=false;this.trackingClickStart=0;if(this.deviceIsIOSWithBadTarget){touch= | |
| event.changedTouches[0];targetElement=document.elementFromPoint(touch.pageX-window.pageXOffset,touch.pageY-window.pageYOffset)}targetTagName=targetElement.tagName.toLowerCase();if(targetTagName==="label"){forElement=this.findControl(targetElement);if(forElement){this.focus(targetElement);if(this.deviceIsAndroid)return false;targetElement=forElement}}else if(this.needsFocus(targetElement)){if(event.timeStamp-trackingClickStart>100||this.deviceIsIOS&&window.top!==window&&targetTagName==="input"){this.targetElement= | |
| null;return false}this.focus(targetElement);if(!this.deviceIsIOS4||targetTagName!=="select"){this.targetElement=null;event.preventDefault()}return false}if(this.deviceIsIOS&&!this.deviceIsIOS4){scrollParent=targetElement.fastClickScrollParent;if(scrollParent&&scrollParent.fastClickLastScrollTop!==scrollParent.scrollTop)return true}if(!this.needsClick(targetElement)){event.preventDefault();this.sendClick(targetElement,event)}return false}; | |
| FastClick.prototype.onTouchCancel=function(){this.trackingClick=false;this.targetElement=null};FastClick.prototype.onMouse=function(event){if(!this.targetElement)return true;if(event.forwardedTouchEvent)return true;if(!event.cancelable)return true;if(!this.needsClick(this.targetElement)||this.cancelNextClick){if(event.stopImmediatePropagation)event.stopImmediatePropagation();else event.propagationStopped=true;event.stopPropagation();event.preventDefault();return false}return true}; | |
| FastClick.prototype.onClick=function(event){var permitted;if(this.trackingClick){this.targetElement=null;this.trackingClick=false;return true}if(event.target.type==="submit"&&event.detail===0)return true;permitted=this.onMouse(event);if(!permitted)this.targetElement=null;return permitted}; | |
| FastClick.prototype.destroy=function(){var layer=this.layer;if(this.deviceIsAndroid){layer.removeEventListener("mouseover",this.onMouse,true);layer.removeEventListener("mousedown",this.onMouse,true);layer.removeEventListener("mouseup",this.onMouse,true)}layer.removeEventListener("click",this.onClick,true);layer.removeEventListener("touchstart",this.onTouchStart,false);layer.removeEventListener("touchend",this.onTouchEnd,false);layer.removeEventListener("touchcancel",this.onTouchCancel,false)}; | |
| FastClick.notNeeded=function(){var metaViewport;if(typeof window.ontouchstart==="undefined")return true;if(/Chrome\/[0-9]+/.test(navigator.userAgent))if(FastClick.prototype.deviceIsAndroid){metaViewport=document.querySelector("meta[name=viewport]");if(metaViewport&&metaViewport.content.indexOf("user-scalable=no")!==-1)return true}else return true;return false};FastClick.attach=function(layer){return new FastClick(layer)};if(typeof define!=="undefined"&&define.amd)define(function(){return FastClick}); | |
| if(typeof module!=="undefined"&&module.exports){module.exports=FastClick.attach;module.exports.FastClick=FastClick}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment