简体   繁体   English

JavaScript / JQuery事件处理程序问题

[英]JavaScript/JQuery event handler problem

Been playing around a bit with JavaScript/JQuery, and made a "shy" button, instead its just a p-tag with a word. 使用JavaScript / JQuery进行了一些测试,并制作了一个“害羞”按钮,取而代之的是带有单词的p标签。 It works ok, except the bool goDown doesn't seem to change, so it always goes down. 它工作正常,但布尔goDown似乎没有变化,因此它总是掉下来。 What's wrong with my code? 我的代码有什么问题?

Also JavaScript debugging in VS2010 doesn't seem to be working very good, is this a known problem? 另外,VS2010中的JavaScript调试似乎不能很好地工作,这是一个已知问题吗?

Thanks in advance! 提前致谢!

if (typeof naarBeneden == 'undefined') {
    var goDown = new Boolean(true);
}

$(document).ready(function () {

    $(".moveme").animate({ "left": "+=500px"
    }, 2000);
    $(".moveme").hover(move());
});


function move() {
    if (goDown) {
        $(".moveme").hover(function () {
            $(this).animate({ "top": "+=50px" }, 0)
        });
        goDown = false;
    }
    else {
        $(".moveme").hover(function () {
            $(this).animate({ "top": "-=50px" }, 0)
        });
        goDown = true;
    }
}

The main issue is here: 主要问题在这里:

$(".moveme").hover(move());

You're calling move() not using move as a handler, you need it without the parenthesis, like this: 您正在调用move()而不是将move用作处理程序,因此需要不带括号的它,如下所示:

$(".moveme").hover(move);

Even doing that though, you're assigning a new .hover() handler on each hover event, and I doubt that's really what you're after. 即使这样做,您.hover()在每个hover事件上分配一个新的.hover()处理函数,而我怀疑那确实是您要的内容。 It's hard to tell exactly what you're after, but I think this is it: 很难确切地说出您的追求,但是我认为是这样的:

$(function () {
  var goDown = typeof naarBeneden != 'undefined';
  $(".moveme").animate({ "left": "+=500px" }, 2000);
              .hover(function () {
                $(this).animate({ "top": goDown ? "+=50px" : "-=50px" }, 0);
              }, function() {
                $(this).animate({ "top": !goDown ? "+=50px" : "-=50px" }, 0);
              });
});

This assigns an up then down animation if goDown is true when you hover/leave the element, and it does the reverse if goDown is false . 这种分配一个上再向下的动画,如果goDowntrue ,当你将鼠标悬停/离开元素,和它相反的,如果goDownfalse

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

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