Using Arrowkeys To Navigate Through 4x4 Grid?
I've read this answer here Guide for writing arrowkey navigation on focusable elements? but its not elaborate enough for a newbie like me to understand. I need something on the lin
Solution 1:
Here's a way to do this using plain javascript. The first part of the code is some cross browser functions to add event handlers and add/remove classes. You'll notice that I add/remove a class to show the active item rather than directly adding/removing a style. This puts the style in the CSS rather than in the javascript which is generally a good idea.
Working demo: http://jsfiddle.net/jfriend00/yMMxX/
(function() {
// refined add event cross browserfunctionaddEvent(elem, event, fn) {
if (typeof elem === "string") {
elem = document.getElementById(elem);
}
// avoid memory overhead of new anonymous functions for every event handler that's installed// by using local functionsfunctionlistenHandler(e) {
var ret = fn.apply(this, arguments);
if (ret === false) {
e.stopPropagation();
e.preventDefault();
}
return(ret);
}
functionattachHandler() {
// set the this pointer same as addEventListener when fn is called// and make sure the event is passed to the fn also so that works the same toovar ret = fn.call(elem, window.event);
if (ret === false) {
window.event.returnValue = false;
window.event.cancelBubble = true;
}
return(ret);
}
if (elem.addEventListener) {
elem.addEventListener(event, listenHandler, false);
} else {
elem.attachEvent("on" + event, attachHandler);
}
}
functionaddClass(elem, cls) {
var oldCls = elem.className;
if (oldCls) {
oldCls += " ";
}
elem.className = oldCls + cls;
}
functionremoveClass(elem, cls) {
var str = " " + elem.className + " ";
elem.className = str.replace(" " + cls + " ", " ").replace(/^\s+|\s+$/g, "");
}
functionfindItem(items, target) {
for (var i = 0; i < items.length; i++) {
if (items[i] === target) {
return(i);
}
}
return(-1);
}
var keys = {up: 38, down: 40, left: 37, right: 39};
var cards = document.getElementById("game-board").getElementsByClassName("card");
addEvent(document, "keydown", function(e) {
// get key press in cross browser wayvar code = e.which || e.keyCode;
// number of items acrossvar width = 4;
var increment, index, newIndex, active;
switch(code) {
case keys.up:
increment = -width;
break;
case keys.down:
increment = width;
break;
case keys.left:
increment = -1;
break;
case keys.right:
increment = 1;
break;
default:
increment = 0;
break;
}
if (increment !== 0) {
active = document.getElementById("game-board").getElementsByClassName("active")[0];
index = findItem(cards, active);
newIndex = index + increment;
if (newIndex >= 0 && newIndex < cards.length) {
removeClass(active, "active");
addClass(cards[newIndex], "active");
}
// prevent default handling of up, down, left, right keysreturnfalse;
}
});
})();
Solution 2:
i've done this with jQuery. You can wire up to the event with something like this:
$(".some-css-class-on-your-divs").keydown(function (e)
{
HandleKeyDown(e, $(this));
})
then handle the event with another javascript method
function HandleKeyDown(event, jqNode)
{
event = event || window.event;
var charCode = event.charCode || event.keyCode;
if (charCode == 40)
{ // down arrow key/// do your thing here
}
// charcode 38 is up, and so on
}
Post a Comment for "Using Arrowkeys To Navigate Through 4x4 Grid?"