HTML and CSS Reference
In-Depth Information
Listing 8-22. Adding Variables to Allow You to Offset the Starting and Ending Points of a Loop
var scrollX = 0, // Offset in the X axis
scrollY = 0; // Offset in the Y axis
// Listen for keypresses
window.addEventListener('keydown', function(e) {
switch (e.keyCode) {
case 37: // Left
case 65: // A
// Make sure that we can't scroll past our minimum value (0)
if (scrollX > 0) {
scrollX -= 10;
}
break;
case 39: // Right
case 68: // D
// Make sure that we can't scroll past our maximum value
// (Width of the Cell * Number of Columns)
if (scrollX < (cellWidth * gridCols)) {
scrollX += 10;
}
break;
case 38: // Up
case 87: // W
// Make sure that we can't scroll past our minimum value (0)
if (scrollY > 0) {
scrollY -= 10;
}
break;
case 40: // Down
case 83: // S
// Make sure that we can't scroll past our maximum value
// (Height of the Cell * Number of Rows)
if (scrollX < (cellHeight * gridRows)) {
scrollY += 10;
}
break;
}
}, false);
Search WWH ::




Custom Search