1

I am aware that you can disable the collapse animation completely as is described in the accepted answer to this question.

But is there any way that I can disable the animation for a .collapse('show') or .collapse('hide') call as a one off? I would really like to be able to do something like this:

.collapse({'show', 
    animation : false        
});

But this option doesn't seem to exist.

Is this possible?

Community
  • 1
  • 1
Redtama
  • 1,603
  • 1
  • 18
  • 35

2 Answers2

1

If you don't want the animation then why to use the collapse feature at all. Just use JQuery's hide & show.

$(".my-prev-collapse-element").hide();
$(".my-prev-collapse-element").show();

Based on the suggestion from @Redtama, the animation can directly be prevented from:

$("my-prev-collapse-element").addClass("in");
$("my-prev-collapse-element").removeClass("in");

to hide or show the element without animation.

Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
  • Thank you very much for your answer! I tried using JQuery's functions but the bootstrap collapse element seems to get confused about what state it is in (e.g if I use JQuery `hide` then bootstrap still thinks the element is shown). – Redtama Jan 08 '16 at 09:34
  • However your answer has helped me to find a solution because I realised I could just use JQuery to do this `$("my-prev-collapse-element").addClass("in");` You can edit your answer to this if you like since you did help me to come up with it :) – Redtama Jan 08 '16 at 09:36
  • Glad, it helped. Updated the answer as you suggested. – Shashank Agrawal Jan 08 '16 at 09:44
0

OK so this is the solution I came up with using JQuery

$("myCollapseElement").addClass("in");

will expand the element with no animation and

$("myCollapseElement").removeClass("in");

will collapse the element with no animation.

This solution suits my purposes, however there is a feature request to be able to do it the way I suggested in my question: Bootstrap Issue enable/disable animation of Collapse on a per-show/hide basis.

So when the guys over at twitter do release this feature I will update my answer. But for the time being, using JQuery to add the bootstrap class in works for me.

Redtama
  • 1,603
  • 1
  • 18
  • 35
  • 1
    The classes have been changed in Boostrap 4, so here's the code that will work for that version: `$("myCollapseElement").addClass("show");` to show the element without animation `$("myCollapseElement").removeClass("show");` to hide the element without animation – TobyLL Apr 16 '19 at 14:11