使用箭头键浏览4x4网格? [英] 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.
我需要像这样的东西,我的DOM中有一个带div的4x4网格.我希望能够在选择器周围移动,然后使用enter选择聚焦的div.
I need something on the lines like that, I have a 4x4 grid with divs in my DOM. I want to be able to move around a selector and then be able to select focused div with enter.
粘贴一些代码,
这是我意识到自己走在完全错误的轨道上时正在尝试的事情:
This is what I was trying when I realized I was on the total wrong track:
function arrowKeys(input){
var keyCode = input.keyCode;
var border = document.getElementById(keyid);
var removeBorder = border.removeAttribute("style");
if(keyCode == 38 || keyCode == 39 || keyCode == 40 || keyCode == 37){
var addborder = border.setAttribute("style", "border:2px solid red");
border.addborder;
if(keyCode == 38){
removeBorder;
keyid = keyid - 4;
border.addborder;
console.log(keyid + " upp");
}
if (keyCode == 39){
removeBorder;
keyid++;
border.addborder;
console.log(keyid + " right");
}
if(keyCode == 40){ // right arrowkey
removeBorder;
keyid = keyid + 4;
border.addborder;
console.log(keyid + " down");
}
if (keyCode == 37){ // left arrowkey
removeBorder;
keyid--;
border.addborder;
console.log(keyid + " left");
}
}
}
我认为您需要在这样的行上使用数组,
I figured you need to use an array something on the lines like this,
var navigationMap = [[1,2,3,4][5,6,7,8][9,10,11,12][13,14,15,16]];
可是我在那里一直卡住了.
But there Im stuck what so ever..
推荐答案
这是使用普通javascript做到这一点的一种方法.代码的第一部分是一些跨浏览器功能,用于添加事件处理程序和添加/删除类.您会注意到,我添加/删除了一个类以显示活动项目,而不是直接添加/删除样式.这会将样式放在CSS中,而不是在javascript中,这通常是个好主意.
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.
正在运行的演示: http://jsfiddle.net/jfriend00/yMMxX/
(function() {
// refined add event cross browser
function addEvent(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 functions
function listenHandler(e) {
var ret = fn.apply(this, arguments);
if (ret === false) {
e.stopPropagation();
e.preventDefault();
}
return(ret);
}
function attachHandler() {
// 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 too
var 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);
}
}
function addClass(elem, cls) {
var oldCls = elem.className;
if (oldCls) {
oldCls += " ";
}
elem.className = oldCls + cls;
}
function removeClass(elem, cls) {
var str = " " + elem.className + " ";
elem.className = str.replace(" " + cls + " ", " ").replace(/^\s+|\s+$/g, "");
}
function findItem(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 way
var code = e.which || e.keyCode;
// number of items across
var 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 keys
return false;
}
});
})();
这篇关于使用箭头键浏览4x4网格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!