Created
April 27, 2020 05:53
-
-
Save prendradjaja/55bb76770a2b2080f10bab1af6fbc5e1 to your computer and use it in GitHub Desktop.
grid interactive demo for nat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<style> | |
td { | |
border: 1px solid black; | |
height: 50px; | |
width: 50px; | |
} | |
td.highlighted { | |
background: #ffcccc; | |
} | |
</style> | |
<table> | |
<tr> | |
<td></td> | |
<td></td> | |
<td></td> | |
</tr> | |
<tr> | |
<td></td> | |
<td></td> | |
<td></td> | |
</tr> | |
<tr> | |
<td></td> | |
<td></td> | |
<td></td> | |
</tr> | |
</table> | |
<script> | |
window.onload = () => { | |
const rows = Array.from(document.querySelectorAll('tr')); | |
const cells = rows.map( | |
r => Array.from(r.querySelectorAll('td')) | |
) | |
let active = { | |
row: 0, | |
col: 0 | |
}; | |
document.addEventListener("keydown", event => { | |
if (event.key === 'ArrowDown') { | |
active.row = active.row + 1; | |
updateHighlights(); | |
} | |
}); | |
function updateHighlights() { | |
cells.forEach(row => { | |
row.forEach(cell => { | |
cell.classList.remove('highlighted'); | |
}); | |
}); | |
cells[active.row][active.col].classList.add('highlighted'); | |
} | |
updateHighlights(); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment