简体   繁体   English

如何在执行动画时一次只允许单击一个JavaScript“ click”方法?

[英]How to allow only one JavaScript 'click' method to be clicked at a time while animation is executing?

When an element is clicked, a div slides off the screen into the center. 单击一个元素时,一个div从屏幕滑出到中心。 When another element is clicked, other div in the center slides off the screen and another div slides in. 当点击另一个元件时,其他div在中心滑出屏幕和另一div中滑动。

The issue occurs when another element is clicked before the initial div has the time to slide to the center of the screen. 当在初始div有时间滑动到屏幕中心之前单击另一个元素时,就会发生此问题。

$('#about').click(function(){
   /* makes any visible div slide out and slides in the 'about' div */ });

$('#contact').click(function(){
   /* makes any visible div slide out and slides in the 'contact' div */ });

In other words, when you click about and right away click contact then they both slide in inconsistently. 换句话说,当您单击“ about并立即单击“ contact它们都会不一致地滑入。

I already tried "attrRemove", "unbind", "off"... and although they work to disable clicks, I cannot enable them again. 我已经尝试过“ attrRemove”,“ unbind”,“ off” ...,尽管它们可以禁用点击,但是我无法再次启用它们。

One way you could approach this is through the use of a variable that indicates if an action is currently in effect. 解决此问题的一种方法是使用变量,该变量指示某个操作当前是否有效。 If you want to queue the event (so you don't entirely disregard the click (as the example below is doing), then the code could be modified to add it to a queue of some sort. 如果您想对事件进行排队(因此,您不必完全忽略点击(如下面的示例所示),那么可以修改代码以将其添加到某种队列中。

var slideInProgress = false;

$('#about').click(function(){
    if (!slideInProgress) {
        slideInProgress = true;
        $("#slider").slideToggle(5000, function(){
        slideInProgress = false;
    });
  }
});

A very simple, quickly put together Fiddle for this example is here: https://jsfiddle.net/t8tcr0rp/ 在此示例中,一个非常简单,快速组合在一起的小提琴在这里: https : //jsfiddle.net/t8tcr0rp/

This code may help. 此代码可能会有所帮助。

function aboutFunc() {
  $('#about').off('click');
  $('#contact').off('click');

  //do your stuff

  $('#about').on('click', aboutFunc);
  $('#contact').on('click', contactFunc);
}

function contactFunc() {
  $('#about').off('click');
  $('#contact').off('click');

  //do your stuff

  $('#about').on('click', aboutFunc);
  $('#contact').on('click', contactFunc);
}


$('#about').on('click', aboutFunc);
$('#contact').on('click', contactFunc);

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

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