From f435f3114b37aac600d56936e77a2052baf05fdb Mon Sep 17 00:00:00 2001 From: dontknow Date: Mon, 17 Jun 2024 15:53:37 +0530 Subject: [PATCH] Add keyboard navigation to image gallery --- Image-Gallery/script.js | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/Image-Gallery/script.js b/Image-Gallery/script.js index b3c3ba6c..9958ee45 100644 --- a/Image-Gallery/script.js +++ b/Image-Gallery/script.js @@ -5,16 +5,36 @@ const nextE1 = document.getElementById("next"); let x = 0; let timer; -prevE1.addEventListener("click", () => { + +function onPrevButtonClick() { x = x + 45; clearTimeout(timer); updateGallery(); -}); -nextE1.addEventListener("click", () => { +} + +function onNextButtonClick() { x = x - 45; clearTimeout(timer); updateGallery(); -}); +} + +prevE1.addEventListener("click", onPrevButtonClick); +nextE1.addEventListener("click", onNextButtonClick); + +function handleKeyDown(event) { + switch(event.key) { + case 'ArrowLeft': + // Simulate a click on the Prev button + onPrevButtonClick(); + break; + case 'ArrowRight': + // Simulate a click on the Next button + onNextButtonClick(); + break; + } +} + +document.addEventListener('keydown', handleKeyDown); function updateGallery() { imageContainerE1.style.transform = `perspective(1000px) rotateY(${x}deg)`; @@ -26,4 +46,3 @@ function updateGallery() { } updateGallery(); -