简体   繁体   English

W3学校灯箱jQuery添加键绑定

[英]W3 school lightbox jquery add keybinds

I am trying to add keybinds to the exemple lightbox ( link ) But as much as I try I can't get it to work. 我正在尝试将键绑定添加到示例灯箱( 链接 )中,但是我尝试了很多却无法正常工作。 Anyone that has any ideas? 有任何想法吗?

I know that I have to do it in JS And I know the keycodes for Left and Right. 我知道我必须在JS中完成,而且我知道Left和Right的键码。 But I am quite new to JS and I don't understand where I should do the function and how I would make it connect to next/prev. 但是我对JS还是很陌生,我不知道该在哪里执行该函数以及如何使它连接到next / prev。

Then I would like to be able to get ESC to function as a close as well. 然后,我希望能够使ESC也能正常运行。 Any help would be appreciated. 任何帮助,将不胜感激。

The original version: 原始版本:

function plusSlides(n) {
showSlides(slideIndex += n);
}

My version so far, don't know if I am on the right track: 到目前为止,我的版本不知道我走的路是否正确:

function plusSlides(n) {
            window.onkeydown = keydown;
            function keydown(e){
                if(e.which == 37 && next)
            }
            showSlides(slideIndex += n);
        }

Got the idea from the original text: 从原始文本中得到了这个主意:

<a class="prev" onclick="plusSlides(-1)">&#10094;</a>
      <a class="next" onclick="plusSlides(1)">&#10095;</a>

With help from this community: 在这个社区的帮助下:

document.onkeydown = function (e) {
   e = e || window.event;
   if(e.keyCode === 39) {
       showSlides(slideIndex +=1);
   } else if(e.keyCode === 37) {
       showSlides(slideIndex -=1);
   } else if(e.keycode === 27) {
      closeModal();
   }

}; };

Strangely enough will not escape work. 奇怪的是无法逃脱工作。 I have tried with keyup as well. 我也尝试过keyup。

document.onkeydown = function (e) {
    e = e || window.event;
    if(e.keyCode === 39) {
        showSlides(slideIndex +=1);
    } else if(e.keyCode === 37) {
        showSlides(slideIndex -=1);
    } else if(e.keyCode === 27) {
        closeModal();
    }
};

This works, it can't go inside a function because you want to capture the actual keypress all the time. 这行得通,因为您想一直捕获实际的按键,所以它不能进入​​函数内部。 You'll also want to do something like make sure the modal is actually open before even capturing the keypresses, otherwise every time someone types a key on your site this code runs. 您还需要执行类似的操作,例如在捕获按键之前确保该模式已真正打开,否则每次有人在您的站点上键入一个键时,该代码就会运行。

For the record 46 is the period, 44 is the comma 记录46是句点,44是逗号

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

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