简体   繁体   English

首次使用后,slideUp和slideDown动画不起作用

[英]slideUp and slideDown animation does not work after first use

I have used jQuery to build something like a dropdown, but it only works for the first two clicks, and then it doesn't. 我已经使用jQuery构建类似下拉列表的内容,但是它仅适用于前两次单击,然后无效。 How can I make a dropdown? 如何制作下拉菜单? Can it be done with a loop? 可以循环执行吗? (I have not learnt loop yet, so any solution would work.) (我还没有学习循环,所以任何解决方案都可以。)

For Each SLIDEUP and SLIDEDOWN I wanted to make different TIME.... 对于每个SLIDEUP和SLIDEDOWN,我都希望使用不同的时间。

 jQuery(document).ready(function() { jQuery(".click-on").click(function() { jQuery(".box").slideUp(2000, function() { jQuery(".click-on").click(function() { jQuery(".box").slideDown(500); }); return false; }); return false; }); }); 
 .box { width: 300px; height: 300px; background-color: skyblue; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <p id="fan">THIS IS A FAN</p> <p id="gun">THIS IS A GUN</p> <p class="click-on">Click Here</p> <div class="box"></div> 

Do you want to achieve something like that? 你想实现这样的目标吗?

$(document).ready(function(){
  $(".click-on").click(function(){
    $('.box').slideToggle();
  });
});

https://jsfiddle.net/jsrc9mbd/1/ https://jsfiddle.net/jsrc9mbd/1/

The answer by @hetious is what I would have given - but having just seen the comment that slide-up and slide-down should have different times, you'll have to do this instead. @hetious的答案是我会给出的-但刚刚看到有关向上滑动和向下滑动的时间应该不同的评论,您必须改为执行此操作。 Basically, check when you click whether the box is visible or not, and either slideUp or dlideDown accordingly: 基本上,检查何时单击该框是否可见,并相应地选择slideUp或dlideDown:

jQuery(document).ready(function() {
  jQuery(".click-on").click(function() {
    var box = jQuery(".box");
    if (box.is(":visible")) {
      box.slideUp(2000);
    }
    else {
      box.slideDown(500);
    }
  });
);

(Note that I have extracted a variable for jQuery(".box") , just to save some typing. And you can also use $ as an alias for jQuery to save yet more (the only reason this wouldn't work is if you are using another library which defines a global $ variable, which a few do.) (请注意,我已经为jQuery(".box")提取了一个变量,只是为了保存一些类型。而且,您还可以将$用作jQuery的别名,以保存更多内容(唯一的原因是,如果您正在使用另一个定义全局$变量的库,有几个这样做。)

This is because you misunderstand the meaning of the .click() function. 这是因为您误解了.click()函数的含义。

.click() sets the handler function each time when the click event is triggered from the selected DOM. 每次从选定DOM触发click事件时, .click()设置处理函数。

Since you have called another .click() within the callback of .slideUp() , you are actually replacing the handler function. 既然你已经调用另一个.click()的回调中.slideUp()你实际上是在更换处理函数。 In your current logic, the obvious fix is to do infinite callback after each click like: 在您当前的逻辑中,明显的解决方法是在每次单击后执行无限回调,例如:

jQuery(".click-on").click(function(){
    jQuery(".box").slideUp(2000, function(){
        jQuery(".click-on").click(function(){
            jQuery(".box").slideDown(500,function(){
                jQuery(".click-on").click(function(){ 
                    jQuery(".box").slideUp(2000, function(){//Repeating callbacks... ...
            });
        });
    });
});

and seriously it is very bad. 严重的是,这是非常糟糕的。 Such implementation should not be done. 不应执行这种实现。

Instead, it is better for you to have a conditional checking for each click, so the logic will determine itself either .slideUp() or .slideDown() should be called. 相反,最好对每次单击进行条件检查,因此逻辑将确定自己应调用.slideUp()还是.slideDown() It should be like 应该像

$(".click-on").click(function(){//you can also use $ instead of jQuery
    //do some conditional check here
    if(isSlidedUp)$(".box").slideDown(1000);
    else $(".box").slideUp(1000)
});

or even better you use .slideToogle() . 甚至更好的方法是使用.slideToogle()

$(".click-on").click(function(){
    $(".box").slideToggle(1000)
}

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

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