简体   繁体   English

如何使用箭头键浏览元素

[英]How to navigate through elements with arrow keys

I have the following list of images:我有以下图像列表:

<figure>
  <span>
    <img src="images/img_01.jpg">
  </span>
</figure>

<figure>
  <span>
    <img src="images/img_02.jpg">
  </span>
</figure>

<figure>
  <span>
    <img src="images/img_03.jpg">
  </span>
</figure>

Clicking on a <figure> element assigns class 'open' to it.单击<figure>元素会将 class 'open' 分配给它。

The behaviour I wish to achieve:我希望实现的行为:

  1. Press left-arrow-key: .removeClass('open');按左箭头键: .removeClass('open'); from current figure and .addClass('open');从当前图和.addClass('open'); to previous figure (or the last figure in line when the current figure is the first one).到前一个图(或当当前图是第一个时,行中的最后一个图)。

  2. Press right-arrow-key: ( .removeClass('open'); from current figure and .addClass('open'); to next figure (or the first figure in line when the current figure is the last one).按右箭头键:( .removeClass('open');从当前图和.addClass('open');到下一个图(或当前图为最后一个时的第一个图)。

  3. When there is no figure element with class '.open' assign this class to the first figure element in line.当没有 class '.open' 的图形元素时,将此 class 分配给行中的第一个图形元素。

I am quite new to jQuery, and believe I have to first make an array of the figure elements.我对 jQuery 很陌生,相信我必须首先制作一个图形元素数组。 Then detect if an arrow button has been pressed.然后检测是否按下了箭头按钮。

What I have tried:我试过的:

$currentImage = $('.open');

$('body').on("keyup", function(e) {              
    var code = e.which;

    if (code == 37) { e.preventDefault();
      $currentImage = $currentImage.prev();
    }

    else if (code == 39) {
        e.preventDefault();
      $currentImage = $currentImage.next();
    };
});

I'm having trouble understanding how to make this work, and could not find another Stackoverflow question similar to mine.我无法理解如何进行这项工作,并且找不到与我类似的另一个 Stackoverflow 问题。 Any help or pointing in the right direction would be very much appreciated!任何帮助或指出正确的方向将不胜感激!

For answer purpose I would answer with other HTML code (which does not include images):出于回答目的,我将使用其他 HTML 代码(不包括图像)来回答:

The idea is to store what index is currently having your class open , then apply and remove the class from elements that applies your idea想法是存储当前open class 的索引,然后从应用您的想法的元素中应用并删除 class

 let selectedIndex = -1; const $elements = $('span').toArray(); $('body').on("keyup", function(e) { var code = e.which; if (code == 37) { e.preventDefault(); if (selectedIndex === -1) { selectedIndex = 0; } else if (selectedIndex === 0) { $($elements[selectedIndex]).removeClass('open'); selectedIndex = $elements.length - 1; } else { $($elements[selectedIndex]).removeClass('open'); --selectedIndex; } $($elements[selectedIndex]).addClass('open'); } else if (code == 39) { e.preventDefault(); if (selectedIndex === -1) { selectedIndex = 0; } else if (selectedIndex === $elements.length - 1) { $($elements[selectedIndex]).removeClass('open'); selectedIndex = 0; } else { $($elements[selectedIndex]).removeClass('open'); ++selectedIndex; } $($elements[selectedIndex]).addClass('open'); }; });
 .open { border: 2px solid red; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <span>blabla1</span> <span>blabla2</span> <span>blabla3</span>

Try like below.尝试如下。 Explanation is in comments.解释在评论中。

 $('body').on("keyup", function(e) { var code = e.which; // get current.open element var $currentImage = $('.open'); // if any of the figure is having class open then only proceed if ($currentImage.length > 0) { if (code == 37) { // left arrow e.preventDefault(); // get previous figure element var prev = $currentImage.prev('figure'); // if length is 0 then it means current one is first. So select last sibling if (prev.length == 0) { prev = $($currentImage).siblings("figure:last") } // remove all open class $('.open').removeClass('open'); // add open class to prev element $(prev).addClass('open'); } else if (code == 39) { // right arrow e.preventDefault(); // get next figure element var next = $currentImage.next('figure'); // if length is 0 then it means current one is last. So select first sibling if (next.length == 0) { next = $($currentImage).siblings("figure:first") } // remove all open class $('.open').removeClass('open'); // add open class to next element $(next).addClass('open'); }; } }); $('figure').on('click', function(e) { e.preventDefault(); $('.open').removeClass('open'); $(this).addClass('open'); });
 figure.open { border: 1px solid blue; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script> <figure> <span> <img src="images/img_01.jpg"> </span> </figure> <figure class='open'> <span> <img src="images/img_02.jpg"> </span> </figure> <figure> <span> <img src="images/img_03.jpg"> </span> </figure>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM