/* * Keynav - jQuery Keyboard Navigation plugin * * Copyright (c) 2013 Nick Ostrovsky * * Licensed under the MIT license: * http://www.opensource.org/licenses/mit-license.php * * Project home: * http://www.firedev.com/jquery.keynav * * Version: 0.1 * */ ;(function($, window, document, undefined) { $.fn.keynav = function(checkNav) { var elements = this; var matrix; var x; var y; var current = this.filter('.selected'); var keyNavigationDisabled=false; if (current.length == 0) current = this.first(); current.addClass('selected'); function update() { var i=0; var row = Array(); var j = -1; var oldtop = false; var m=Array(); elements.each(function(){ if (!oldtop) oldtop = this.offsetTop; newtop=this.offsetTop; if (newtop != oldtop) { oldtop=newtop; m[i]=row; row = Array(); i++; j=0; row[j]=this; } else { j++; row[j]=this; } }); m[i]=row; matrix = m; coordinates=findCurrent(); x=coordinates[0]; y=coordinates[1]; return matrix; } function findCurrent() { i=0; j=0; found = false; try { for (i=0; i=matrix.length) i=0; if (j<0) j=(matrix[i].length-1); if (j>=matrix[i].length) j=0; current.removeClass('selected'); current = $(matrix[i][j]); current.addClass('selected'); x=i; y=j; } $(window).bind("resize", function(event) { update(); }); $(document).ready(function() { update(); }); $(document).keydown(function(e){ if (checkNav && checkNav()) return; if (e.keyCode == 37) { // left setCurrent(x,y-1); e.preventDefault(); } else if (e.keyCode == 38) { // up setCurrent(x-1,y); e.preventDefault(); } else if (e.keyCode == 39) { // right setCurrent(x,y+1); e.preventDefault(); } else if (e.keyCode == 40) { // down setCurrent(x+1,y); e.preventDefault(); } else if (e.keyCode == 13) { window.location = current.attr('href'); e.preventDefault(); } }); return this; } })(jQuery, window, document);