简体   繁体   English

jQuery keydown只工作一次

[英]Jquery keydown only works once

EDIT: I solved it by changing = to ==, but that didnt fully solve it but then I added a change to $currentSlide and now it works! 编辑:我通过将=更改为==来解决它,但是那并没有完全解决它,但是后来我对$ currentSlide添加了一个更改,现在可以使用了! Yay! 好极了!

            $(document).keydown(function(e) {
            if(e.keyCode == 39)
                {
                    if($currentSlide == $slide1){
                        slideShow(slide2);
                        $currentSlide = $slide2;
                    }
                    else if($currentSlide == $slide2){
                        slideShow(slide3);
                        $currentSlide = $slide3;
                    }
                    else if($currentSlide == $slide3){
                        slideShow(slide1);
                        $currentSlide = $slide1;
                    }
                }
        })

I have searched for an answer but haven't found anything that suits my question. 我已经找到了答案,但是没有找到适合我的问题的答案。 I am a noob on javascript so bear with me. 我是Java语言的菜鸟,所以请多多包涵。

I have a function that works as a slideshow. 我有一个可以幻灯片播放的功能。 (I use $ in front of my jquery variables, I have a lot of javascript variables too so I just use it to separate them.) (我在jquery变量之前使用$,我也有很多javascript变量,因此我只是用它来分隔它们。)

var $currentSlide = "#slide1";
var $slide1 = "#slide1";
var $slide2 = "#slide2";
var $slide3 = "#slide3";

function slideShow($slide) {
    if ($slide != $currentSlide){
        $($currentSlide).fadeOut(500);
        $($slide).delay(500).fadeIn(500);
        $currentSlide = $slide;
    }
};

To call this function, I use a simple link with parameter depending on which slide is active. 为了调用此函数,我使用一个带参数的简单链接,具体取决于哪个幻灯片处于活动状态。

onclick="slideShow(slide2)"

And then I want to change slide with keypress (to right). 然后,我想用按键更改幻灯片(向右)。 This is my code for the keypress: 这是我的按键代码:

$(document).keydown(function(e) {
    if(e.keyCode == 39) {
        if ($currentSlide = $slide1){
            slideShow(slide2);
        } else if($currentSlide = $slide2) {
            slideShow(slide3);
        } else if($currentSlide = $slide3) {
            slideShow(slide1);
        }
     }
})

It works perfectly when using the links but when I press key it behaves very weird. 当使用链接时,它可以完美工作,但是当我按下键时,它的行为却很奇怪。 First click works like a charm, but then it doesnt work any more. 第一次单击就像一个超级按钮一样,但随后不再起作用。 If I click to get the third slide, another click will put next slide on top of slide3 but slide3 never goes away. 如果单击以获取第三张幻灯片,则再次单击将下一张幻灯片放到slide3的顶部,但是slide3永远不会消失。

I realise there is some huge mistake by me here but I'm too much of a beginner to fix it. 我意识到我在这里犯了一个很大的错误,但是我太初学者无法解决。 Any ideas? 有任何想法吗?

your if-else conditions will always be true, because you used '=' instead of '=='. 您的if-else条件将始终为true,因为您使用的是'='而不是'=='。 since your first if condition will be true it always shows slide2 and it looks to you that it only worked once 因为您的第一个if条件为true,所以它始终显示slide2,并且您会发现它只工作一次

$(document).keydown(function(e) {
    if(e.keyCode == 39) {
        if ($currentSlide == $slide1){
            slideShow(slide2);
        } else if($currentSlide == $slide2) {
            slideShow(slide3);
        } else if($currentSlide == $slide3) {
            slideShow(slide1);
        }
     }
})

The second problem maybe caused by the clock on the slideshow, if you are using one. 如果您正在使用第二个问题,可能是由于幻灯片上的时钟造成的。 When you click the next/previous button you need to reset the clock of your slideshow. 当您单击下一个/上一个按钮时,您需要重置幻灯片的时钟。

There are two issues 有两个问题

  • You do an assignment in your if conditions, so they always are true. 您在if条件中进行分配,因此它们始终为真。 Instead use the comparator === ; 而是使用比较器=== ;
  • In the onclick attribute you specify an undefined variable, since the $ is missing from it onclick属性中,您指定了一个未定义的变量,因为它缺少$

Beside correcting this, I would suggest to use a class for your slide elements, not individual IDs. 除了纠正此问题外,我建议对幻灯片元素使用类,而不要使用单个ID。 So use class="slide" instead of id="slide1" in your HTML, and apply it to all slides -- they can share the same class. 因此,在您的HTML中使用class="slide"而不是id="slide1" ,并将其应用于所有幻灯片-它们可以共享同一类。

Then store the sequence number of the current slide, counting from 0. 然后存储当前幻灯片的序列号,从0开始计数。

I would also remove all the onclick attributes on the slide elements and deal with click handlers from code, which can be done quite concisely with $('.slide').click( ... ) : 我还将删除幻灯片元素上的所有onclick属性,并处理代码中的单击处理程序,这可以使用$('.slide').click( ... )相当简洁地完成:

var currentSlideNo = 0; // zero-indexed

function slideShow(slideNo) {
    if(slideNo != currentSlideNo){
        $('.slide').get(currentSlideNo).fadeOut(500);
        currentSlideNo = slideNo;
        $('.slide').get(currentSlideNo).delay(500).fadeIn(500);
    }
};

$('.slide').click(function () {
    slideShow((currentSlideNo + 1) % $('.slide').length);
});

$(document).keydown(function(e) {
    if (e.keyCode == 39) {
        slideShow((currentSlideNo + 1) % $('.slide').length);
    }
});

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

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