Skip to content

Instantly share code, notes, and snippets.

@makeusabrew
Created September 27, 2011 13:28
Show Gist options
  • Save makeusabrew/1245039 to your computer and use it in GitHub Desktop.
Save makeusabrew/1245039 to your computer and use it in GitHub Desktop.
keypress nav
<script>
$(function() {
// let's make a bespoke handler for a keypress, so we can potentially
// do something like animate the A or whatever
$("li.prev a, li.next a").bind('keynav', function(e) {
// $(this) is the A element at this point
// for now, just go to the href, but we could do more fun stuff here
window.location = $(this).attr("href");
});
// bind the keydown event and listen out for left and right keys only
$(window).keydown(function(e) {
switch (e.which) {
case 37: // left
$("li.prev a").trigger("keynav");
break;
case 39: // right
$("li.next a").trigger("keynav");
break;
default:
// nada
break;
}
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment