开发者

Is it posible to deactivate an .click(function) while it's proceeding?

Another Beginner-Question from myself:

Is it possible to disable an .click(function) while it's proceeding [sliding Up/Down and doing some id-switches]?

I have a bunch of anchors using the same function and I don't want users to click on another link while there's still some sl开发者_JAVA百科idingUp/Down is going on. In order to get this effect i've to block the function while it's proceeding.

Any ideas anyone??


You could check if any of the elements you care about are currently being animated using the :animated selector, like this:

$("a.myClass").click(function() {
  if($("a.myClass:animated").length) return false;
  //animate/do stuff
});

While any a.myClass elements are being animated, clicks will have no effect.


You can do it like this:

$('some_selector').click(function () {
    var running = false;

    return function () {
        if (running === false) {
            running = true;
            // do animation stuff;
            // put this in your callback:
            running = false;
        }

        return false;
    };
}());

This disables the animation while running equals true.


You can set a boolean flag in the click handler and unset it when the animation finishes.

Then, at the top of the handler, you can return false; if the flag is set.


Take a look at the :animated selector. As the documentation states, it will return all elements that are currently in progress. If you have specific element that you are checking for, you can use it's ID or class along with this selector to check if it's animation is running.

$("#myButton").click(function(){
  if ($("#myDiv:animated").length === 0) {
    // do awesome animation here
  } else {
    // brown explosions! Dumbledore dies, so does Hedwig!
    return false;
  } 
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜