Skip to content

Instantly share code, notes, and snippets.

@jimmynotjim
Forked from arielsalminen/nav.html
Created March 5, 2013 05:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimmynotjim/5088125 to your computer and use it in GitHub Desktop.
Save jimmynotjim/5088125 to your computer and use it in GitHub Desktop.
// Nav toggle for mobile
(function (w) {
w.navigation = function () {
var nav_open = false,
doc = w.document,
nav = doc.getElementById('nav'),
nav_toggle = doc.getElementById('nav-toggle'),
nav_function = function () {
if (nav_open === false) {
nav.className = nav.className.replace('closed', 'opened');
nav_open = true;
} else {
nav.className = nav.className.replace('opened', 'closed');
nav_open = false;
}
return false;
};
// Touchstart event fires before the mousedown event, and can wipe the mousedown event
nav_toggle.onmousedown = function () {
nav_function();
};
nav_toggle.ontouchstart = function () {
nav_toggle.onmousedown = null;
nav_function();
};
nav_toggle.onclick = function () {
return false;
};
}
// Run on domready (w.load as a fallback)
if (w.addEventListener) {
w.addEventListener("DOMContentLoaded", function () {
w.navigation();
// Run once only
w.removeEventListener("load", w.navigation, false);
}, false);
w.addEventListener("load", w.navigation, false);
} else if (w.attachEvent) {
w.attachEvent("onload", w.navigation);
}
})(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment