Last active
June 7, 2023 16:26
-
-
Save dpw1/4d444bb459682fb4684d165c5c40cd3f to your computer and use it in GitHub Desktop.
How to change product images with arrow keys
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
| {% if template == 'product' %} | |
| <script> | |
| document.addEventListener('keydown', function (event) { | |
| function isThumbnailsOrArrows(){ | |
| // has arrows | |
| if (window.getComputedStyle(document.querySelector(`.slider-buttons`)).display !== 'none'){ | |
| return 'arrows'; | |
| } | |
| //has thumbnails | |
| if (window.getComputedStyle(document.querySelector(` [id*='GalleryThumbnails'] `)).display !== 'none'){ | |
| return 'thumbnails'; | |
| } | |
| return null; | |
| } | |
| function isLastElement(element) { | |
| return element.nextElementSibling === null; | |
| } | |
| if (!isThumbnailsOrArrows()){ | |
| return; | |
| } | |
| function goToPreviousImage(){ | |
| if (isThumbnailsOrArrows() === 'arrows'){ | |
| const $prev = document.querySelector(`.slider-button--prev`); | |
| $prev.click(); | |
| } else if (isThumbnailsOrArrows() === 'thumbnails'){ | |
| const $current = document.querySelector(`.is-active`); | |
| if (!$current){ | |
| return | |
| } | |
| const id = $current.getAttribute(`data-media-id`); | |
| const $el = document.querySelector(`[data-target='${id}']`); | |
| let $thumbnail; | |
| $thumbnail = getPreviousSibling($el, `li:not([class*='--variant'])`); | |
| if ($thumbnail){ | |
| let $button = $thumbnail.querySelector(`button`); | |
| $button.click(); | |
| return; | |
| } else{ | |
| let $button; | |
| $button = isLastElement($el) ? document.querySelector(`li[id*='main-0'] > button`) : document.querySelector(`[id*='Thumbnails'] > li:last-of-type:not([class*='--variants']) > button`); | |
| $button.click(); | |
| } | |
| } | |
| } | |
| function getPreviousSibling (elem, selector) { | |
| var sibling = elem.previousElementSibling; | |
| if (!selector) return sibling; | |
| while (sibling) { | |
| if (sibling.matches(selector)) return sibling; | |
| sibling = sibling.previousElementSibling; | |
| } | |
| }; | |
| function goToNextImage(){ | |
| if (isThumbnailsOrArrows() === 'arrows'){ | |
| const $next = document.querySelector(`.slider-button--next`); | |
| $next.click(); | |
| } else if (isThumbnailsOrArrows() === 'thumbnails'){ | |
| const $current = document.querySelector(`.is-active`); | |
| if (!$current){ | |
| return | |
| } | |
| const id = $current.getAttribute(`data-media-id`); | |
| const $thumbnail = document.querySelector(`[data-target='${id}'] ~ li:not([class*='--variant']) > button`); | |
| if ($thumbnail){ | |
| $thumbnail.click(); | |
| return; | |
| } else{ | |
| const $first = document.querySelector(`li[id*='main-0'] > button`); | |
| $first.click(); | |
| } | |
| } | |
| } | |
| if (event.key === 'ArrowLeft') { | |
| goToPreviousImage(); | |
| } else if (event.key === 'ArrowRight') { | |
| goToNextImage(); | |
| } | |
| }); | |
| (() => {var e=["background: linear-gradient(-47deg,#8731e8,#4528dc)","border: 1px solid #3E0E02","color: white","display: block","text-shadow: 0 1px 0 rgba(0, 0, 0, 0.3)","box-shadow: 0 1px 0 rgba(255, 255, 255, 0.4) inset, 0 5px 3px -5px rgba(0, 0, 0, 0.5), 0 -13px 5px -10px rgba(255, 255, 255, 0.4) inset","line-height: 40px","text-align: center","font-weight: bold","padding: 0px 5px"].join(";");function r(e){return(e+"").replace(/&#\d+;/gm,function(e){return String.fromCharCode(e.match(/\d+/gm)[0])})}var n=r(`Website powered by https://ezfycode.com`);console.log(`%c ${n}`,e);})() | |
| </script> | |
| {% endif %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment