Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const DOM_VK_LEFT = 37;
- const DOM_VK_RIGHT = 39;
- var fruitItemsCount = 0;
- function onBodyLoad() {
- var fruitsElem = document.getElementById("fruits");
- fruitsElem.addEventListener("focus", function(e) { onFruitsFocus(e); }, false);
- fruitsElem.addEventListener("keydown", function(e) { onFruitsKeyDown(e); }, false);
- fruitItemsCount = fruitsElem.getElementsByTagName("li").length;
- }
- function onFruitsFocus(event) {
- var elem = event.target;
- var activeId = elem.getAttribute("aria-activedescendant");
- var active = document.getElementById(activeId);
- active.focus();
- }
- function onFruitsKeyDown(event) {
- var elem = event.currentTarget;
- var active = elem.getAttribute("aria-activedescendant");
- var fruitNum = active;
- event.preventDefault();
- if (event.keyCode === DOM_VK_RIGHT) {
- ++fruitNum;
- if (fruitNum > fruitItemsCount) {
- fruitNum = 1;
- }
- } else if (event.keyCode === DOM_VK_LEFT) {
- --fruitNum;
- if (fruitNum < 1) {
- fruitNum = fruitItemsCount;
- }
- }
- if (fruitNum != active) {
- var currItem = document.getElementById(fruitNum);
- document.getElementById(active).className = "item";
- currItem.className = "selectedItem";
- elem.setAttribute("aria-activedescendant", fruitNum);
- currItem.focus();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement