From e50c604a80a7cadb8afb8f6f56417ed597bb2e68 Mon Sep 17 00:00:00 2001 From: Dan Cojocaru Date: Wed, 13 Jul 2022 00:56:41 +0300 Subject: [PATCH] Implemented circular selection When up from first item, go to last When down from last item, go to first --- items.js | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/items.js b/items.js index 3125c78..ba684b9 100644 --- a/items.js +++ b/items.js @@ -2,21 +2,14 @@ var currentIndex = 0 function nav(offset) { var items = document.querySelectorAll('.items:not(.disabled)') - if (offset === -1) { - if (currentIndex <= 0) { - return - } - } - else if (offset === 1) { - if (currentIndex >= items.length - 1) { - return - } - } - else { - console.error(`nav called with unknown offset: ${offset}`) - } currentIndex += offset + if (currentIndex < 0) { + currentIndex += items.length + } + if (currentIndex >= items.length) { + currentIndex -= items.length + } items[currentIndex].focus() items[currentIndex].addEventListener('keydown', handleKeyDown) }