(JQuery)按键对角移动

时间:2012-02-06 22:46:21

标签: jquery

$("html").keydown(function(event){

        if(event.which == "37")
            $("#hero").animate({"left" : "-=30px"});

        if(event.which == "39")
            $("#hero").animate({"left" : "+=30px"});

        if(event.which == "38")
            $("#hero").animate({"top" : "-=30px"});

        if(event.which == "40")
            $("#hero").animate({"top" : "+=30px"});
});

如何启用#hero对角移动?

2 个答案:

答案 0 :(得分:1)

修改:检查我更新的DEMO

参考:Can jQuery .keypress() detect more than one key at the same time?

另请查看来自 Sime Vidas 的评论..好动画..他的帖子:how to move a div with arrow keys

---老邮报---

在这里查看我的demo ..

过渡并不顺利,有一些问题......我试图找出原因..

答案 1 :(得分:0)

.animate()的参数是一个哈希值,因此您可以包含多个属性。这将向左移动#hero 30px,向左移动30px:

$("#hero").animate({"left" : "-=30px", "top" : "-=30px"});
相关问题