简体   繁体   English

jQuery fadeToggle-无法正确设置可见性

[英]jQuery fadeToggle - Not setting visibility correctly

I have a jQuery toggle question. 我有一个jQuery切换问题。 The following code works fine. 以下代码可以正常工作。

// unhide the more when clicked
$(".more").click(function () {
    $(this).next(".moreDetails").toggle();
    // check state of the toggle
    if ($(this).next(".moreDetails").is(":visible")) {
        $(this).html('Show Less Details ... <img src="images/smallarrowup.png" />');
    } else {
        $(this).html('Show More Details ... <img src="images/smallarrowdown.png" />');
    }
})

When someone clicks the .more class DIV it toggles the .moreDetails DIV and then changes the HTML within the .MORE DIV. 当有人单击.more类DIV时,它将切换.moreDetails DIV,然后更改.MORE DIV中的HTML。 It works because when it toggles the .moreDetails DIV it must then set somewhere in the .moreDetails :visible, and vice-versa, when it is toggles again it must set somewhere in the .moreDetails :hidden. 之所以起作用,是因为当它切换.moreDetails DIV时,必须将其设置在.moreDetails:visible中,反之亦然;当再次切换时,必须将其设置在.moreDetails:hidden中。

This is all great, however the toggle is a show/hide effect. 一切都很好,但是切换是显示/隐藏效果。 I am wanting a nice fade effect. 我想要一个不错的淡入淡出效果。 but as soon as I change 但是一旦我改变

$(this).next(".moreDetails").toggle();

to

$(this).next(".moreDetails").fadeToggle();

it breaks. 坏了 It does not seems at each toggle the visibility of the .moreDetails DIV is always true even though you cannot see it on the DOM. 尽管您在DOM上看不到它,但似乎在每次切换时.moreDetails DIV的可见性始终是正确的。

Any ideas? 有任何想法吗? Thanks 谢谢

This is because you are checking :visibility before finishing fade in / out effect. 这是因为您在完成淡入/淡出效果之前正在检查:visibility

Try this : put if / else inside fadeToggle function 试试这个:如果/ else放在fadeToggle函数中

// unhide the more when clicked
$(".more").click(function () {
    var $this = $(this);
    $(this).next(".moreDetails").fadeToggle( "fast", function() {
    if ($(this).is(":visible")) {
        $this.html('Show Less Details ... <img src="images/smallarrowup.png" />');
    } else {
        $this.html('Show More Details ... <img src="images/smallarrowdown.png" />');
    }
   });
});

You can use "slow" / "fast" as per requirement. 您可以根据需要使用"slow" / "fast"

For more details on fadeToggle click here . 有关衰落切换的更多详细信息,请单击此处

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

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