Created
December 7, 2012 13:41
-
-
Save jimmynotjim/4233339 to your computer and use it in GitHub Desktop.
Dropdown menu script
This file contains 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
// This doesn't work when tapping off the menu, but works when clicking off the menu on non-touch (desktop) | |
/* Main Mobile Nav */ | |
$(document).ready(function(){ | |
var trigger = $('.menu-trigger'); | |
var nav = $('.main-nav'); | |
var parItem = nav.find('.parent-item > a'); | |
var subMenus = nav.find('.sub-menu'); | |
$('body').on('click', function(e){ | |
var target = $(e.target); | |
var is_trigger = target.closest(trigger).length; | |
var is_parItem = target.closest(parItem).length; | |
if (is_trigger){ | |
subMenus.removeClass('active'); | |
nav.toggleClass('active'); | |
return false; | |
} else if (is_parItem){ | |
target.siblings('.sub-menu').toggleClass('active'); | |
return false; | |
} else { | |
subMenus.removeClass('active'); | |
nav.removeClass('active'); | |
} | |
}); | |
}); | |
/* End Mobile Nav */ |
This file contains 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
// This works when tapping off the menu, but when tapping menu links the tap registers behind the menu. Everything works on desktop | |
/* Special tap event */ | |
jQuery.event.special.tap = { | |
setup: function (a, b) { | |
var c = this, | |
d = jQuery(c); | |
if (window.Touch) { | |
d.bind("touchstart", jQuery.event.special.tap.onTouchStart); | |
d.bind("touchmove", jQuery.event.special.tap.onTouchMove); | |
d.bind("touchend", jQuery.event.special.tap.onTouchEnd); | |
} else { | |
d.bind("click", jQuery.event.special.tap.click); | |
} | |
}, | |
click: function (a) { | |
a.type = "tap"; | |
jQuery.event.handle.apply(this, arguments); | |
}, | |
teardown: function (a) { | |
if (window.Touch) { | |
$elem.unbind("touchstart", jQuery.event.special.tap.onTouchStart); | |
$elem.unbind("touchmove", jQuery.event.special.tap.onTouchMove); | |
$elem.unbind("touchend", jQuery.event.special.tap.onTouchEnd); | |
} else { | |
$elem.unbind("click", jQuery.event.special.tap.click); | |
} | |
}, | |
onTouchStart: function (a) { | |
this.moved = false; | |
}, | |
onTouchMove: function (a) { | |
this.moved = true; | |
}, | |
onTouchEnd: function (a) { | |
if (!this.moved) { | |
a.type = "tap"; | |
jQuery.event.handle.apply(this, arguments); | |
} | |
} | |
}; | |
/* End Tap Event */ | |
/* Main Mobile Nav */ | |
$(document).ready(function(){ | |
var trigger = $('.menu-trigger'); | |
var nav = $('.main-nav'); | |
var parItem = nav.find('.parent-item > a'); | |
var subMenus = nav.find('.sub-menu'); | |
$('body').on('tap', function(e){ | |
var target = $(e.target); | |
var is_trigger = target.closest(trigger).length; | |
var is_parItem = target.closest(parItem).length; | |
if (is_trigger){ | |
subMenus.removeClass('active'); | |
nav.toggleClass('active'); | |
return false; | |
} else if (is_parItem){ | |
target.siblings('.sub-menu').toggleClass('active'); | |
return false; | |
} else { | |
subMenus.removeClass('active'); | |
nav.removeClass('active'); | |
} | |
}); | |
}); | |
/* End Mobile Nav */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The answer is use the first one ('click' event) and add FastClick https://github.com/ftlabs/fastclick Problem solved