Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 尝试使用按键制作动画_Javascript_Jquery - Fatal编程技术网

Javascript 尝试使用按键制作动画

Javascript 尝试使用按键制作动画,javascript,jquery,Javascript,Jquery,这是我当前的代码,我似乎无法让动画与键盘一起工作。有什么帮助吗 $(文档).ready(函数(){ })) 函数animationkeydown(元素,动画){ }什么是#c4,为什么要循环遍历每一个?如果它是一个ID,那么它应该只有一个元素 不管怎样,您正在考虑添加一个onkeydown事件(这是jQuery)。假设您有一组id为#c4的DOM元素(您不应该这样做,而是将它们设为类),您应该执行以下操作: $(document).ready(function(){ $('#c4').

这是我当前的代码,我似乎无法让动画与键盘一起工作。有什么帮助吗

$(文档).ready(函数(){

}))

函数animationkeydown(元素,动画){

}

什么是
#c4
,为什么要循环遍历每一个?如果它是一个ID,那么它应该只有一个元素

不管怎样,您正在考虑添加一个
onkeydown
事件(这是jQuery)。假设您有一组id为
#c4
的DOM元素(您不应该这样做,而是将它们设为类),您应该执行以下操作:

$(document).ready(function(){
     $('#c4').on('keydown', function(e) {
        if (e.which == 81) {// 81 is the value of q
            animationClick(this, 'push');
        }
}); 
这会将
keydown
事件应用于与该id匹配的每个元素。在回调中,
e
包含事件,因此
e.which
给出了按下的键

编辑:在使用代码
动画的注释后,单击
,假设其中一个有效

$(document).ready(function(){
     $('#c4').on('keydown', function(e) {
        var that = this; // to mimic the way you pass the element
        var animation = "push";
        if (e.which == 81) {// 81 is the value of q
            that.addClass('animated push ' + animation); //waits for animation to finish before removing
            window.setTimeout(function() {
                that.removeClass('animated ' + animation);
            }, 100);
        }
     });
}); 

我们需要查看
animationClick()
的代码!什么没有?什么坏了?给我们看更多的代码,否则我们真的帮不了你。请更清楚地了解问题所在。添加console.log行并尝试对此进行调试。当你说不起作用时,哪个部分失效了?你的页面有反应吗?控制台中是否有错误?您是否检查过jquery选择器是否返回任何内容?如果你想让我们帮你,我们需要一些帮助。我想不出一种方法,使动画与键盘一起工作。例如,Q是注释c4。我试过使用你的代码,但不起作用。我已经让他们使用鼠标点击:$('c4')。每个(函数(){animationClick(这个,'push');});对不起,你问的问题还是没有道理。您可以发布
动画单击的代码吗?那可能会有帮助
$(document).ready(function(){
     $('#c4').on('keydown', function(e) {
        if (e.which == 81) {// 81 is the value of q
            animationClick(this, 'push');
        }
}); 
$(document).ready(function(){
     $('#c4').on('keydown', function(e) {
        var that = this; // to mimic the way you pass the element
        var animation = "push";
        if (e.which == 81) {// 81 is the value of q
            that.addClass('animated push ' + animation); //waits for animation to finish before removing
            window.setTimeout(function() {
                that.removeClass('animated ' + animation);
            }, 100);
        }
     });
});