jQuery Slidetoggle - Open/closed detection

Asked

Viewed 247 times

0

I would like to remove the "active" class from "liToggle" in case you closed a Slidetoggle.

I tried to use is(':hidden') but unsuccessfully. If I put right after the open parole, it closes at the same time. I am without ideas for logic.

Can someone help me?

$("aside #menu-busca ul > li a").click(function(){
    if($(this).attr('href') == "#"){

        var ulToggle = $(this).parent().children("ul");
        var liToggle = $(this).parent();
        ulToggle.slideToggle("slow");

        // * Se estiver aberto
        if (ulToggle.is(":visible")){
            $(liToggle).addClass("ativo");
        }

        return false;
    }
});
  • ulToggle is a collection of several elements right? which one do you want to know if it is visible? Puts a jsFiddle that makes it easier to understand the problem.

  • Checks if the css display value is block.

  • @Sergio, Segue! http://codepen.io/maykelesser/pen/RKdgvY... I want to know if the clicked li is visible or not.

  • @MaykelEsser http://codepen.io/sergiocrisostomo/pen/vgPJgb

  • @Sergio saw that you put a console.log to check if it’s open or closed, but see that if I close, it returns 2 booleans (true and true). If it is no longer Visible, why is it returning TRUE yet?

  • @Maykelesser http://codepen.io/sergiocrisostomo/pen/wgOrWd had 2 console.log in that code. I had moved to the first line because then you know for sure that slideToggle hasn’t started yet. However, it may still be running a slide toggle, so it would be better to have a flag. What do you want to do in the source after knowing if it is open and/or closed?

  • @Sergio When Slidetoggle is open, add an "active" class to the clicked LI. This I have already done. When closing, I would like it removed. I tried with ELSE, is(":Hidden"), but without success.

Show 2 more comments

1 answer

0

$('#collapseDiv').on('shown.bs.collapse', function () {
   console.log("Opened")
});

$('#collapseDiv').on('hidden.bs.collapse', function () {
   console.log("Closed")
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.