简体   繁体   English

为什么我的div不能慢慢隐藏?

[英]Why is my div not hiding slowly?

I have a div containing a table wrapped in a div so parent child relation, I am displaying and hiding the div on click of a text title(table display:hidden initially). 我有一个div,其中包含一个包装在div中的表,因此是父子关系,单击文本标题时,我将显示并隐藏div(表显示:最初是隐藏的)。 My logic works, I can get the table to show and hide on click however, when I pass hide millisecond values, the table still disappears instantly. 按照我的逻辑,我可以使表在单击时显示和隐藏,但是,当我传递hide毫秒值时,表仍然会立即消失。 Here is my structure. 这是我的结构。

var ctdown = false;
jQuery(".table-dropdown").click(function () {
    var table_height = $(".table-dropdown div").show().height()+20;
    $(".table-dropdown div").hide();
    if (ctdown) {
        jQuery(this).animate({
            height: "20px"
        }, 500, function () {
            $(".table-dropdown div").hide(1000000);
        });
    }
    else {
        jQuery(this).animate({
            height: table_height
        }, 500, function () {

        });
        jQuery(".table-dropdown div").show();
    }
    ctdown = !ctdown;
});

I put in 100000 just to test if it was actually doing anything, which it is not. 我输入了100000只是为了测试它是否实际上在做任何事情,事实并非如此。 Any suggestions? 有什么建议么? Is there some basic principle here that I am over looking? 这里有我要关注的一些基本原则吗? Thanks! 谢谢!

UPDATE! 更新! I derived an answer from the marked answer in the post and another member who's answer was removed for some reason. 我从帖子中的标记答案中得出了一个答案,而另一个成员的答案由于某种原因而被删除。 Here is my final solution. 这是我的最终解决方案。 Thank you to all that helped, greatly appreciate it! 感谢您提供的所有帮助,非常感谢!

var ctdown = false;
$(".table-dropdown").click(function () {
if (ctdown) {
    $(this).animate({
        height: "20px"
    }, 500, function () {
        $("div", this).hide().fadeOut(500);
    });
}
else {
    var table_height = $("div", this).show().height()+20;
    $("div", this).hide();
    $(this).animate({
        height: table_height
    }, 500, function () {

    });
    $("div", this).show();
}
ctdown = !ctdown;
});

You can use fadeIn or fadeOut for that. 您可以为此使用fadeInfadeOut

In you code this could be: 在您的代码中,可能是:

var ctdown = false;

jQuery(".table-dropdown").click(function () {

    var table_height = $(".table-dropdown div").show().fadeIn(500).height()+20;
    $(".table-dropdown div").hide().fadeOut(500);

        if (ctdown) {
            jQuery(this).animate({
                height: "20px"
            }, 500, function () {
        $(".table-dropdown div").hide().fadeOut(500);
            });
        }
        else {
            jQuery(this).animate({
                height: table_height
            }, 500, function () {

            });
        jQuery(".table-dropdown div").show().fadeIn(500);
        }
        ctdown = !ctdown;
    });

Have you tried to remove the line that hide the div in this block? 您是否尝试过删除在此块中隐藏div的行?

var table_height = $(".table-dropdown div").show().height()+20;
$(".table-dropdown div").hide();

I have tested that this .hide will hide the div and ignores your second .hide() 我已经测试过,这个.hide将隐藏div并忽略第二个.hide()

Edit 1 - Example added: https://jsfiddle.net/9eoq22ts/9/ 编辑1-添加示例: https : //jsfiddle.net/9eoq22ts/9/

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

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