Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customizing the collapse transition in bootstrap 4

Bootstrap 4 uses the class .collapsing to animate the width/height of an .collapse-element while opening/closing it. Unfortunately the actual change is approached by adding the width/height as an inline style to the element and adding and removing the class at the start and end of the transition. Therefore it's quite hard to customize the transition (e.g. change timing or fade in/out instead of width transition).

What I've tried so far:

  • Adding the css property transition:none to the .collapsing class: This does help get rid of the transition but opening/closing is still delayed by the transition time, since the class still gets added for a few millis before the actual change takes place.
  • Adding custom css keyframes to the .collapsing class: Since the same class is used for opening and closing, the same animation is shown for both.

Is there any way to change the transition e.g. to fade in/out (change of opacity) or do I have to build a custom version of the bootstrap.js?

like image 466
TiPE Avatar asked Dec 10 '17 12:12

TiPE


People also ask

How do I style a bootstrap collapse?

Just add data-toggle="collapse" and a data-target to the element to automatically assign control of one or more collapsible elements. The data-target attribute accepts a CSS selector to apply the collapse to. Be sure to add the class collapse to the collapsible element.

What is bootstrap 4 collapse?

The collapse JavaScript plugin is used to show and hide content. Buttons or anchors are used as triggers that are mapped to specific elements you toggle. Collapsing an element will animate the height from its current value to 0 . Given how CSS handles animations, you cannot use padding on a .

Which BS 5 attribute and value is used to create a collapse system?

Generally, we recommend using a button with the data-bs-target attribute. While not recommended from a semantic point of view, you can also use a link with the href attribute (and a role="button" ). In both cases, the data-bs-toggle="collapse" is required.

How does Bootstrap collapse work?

Bootstrap collapse is used to show or hide content. Buttons or anchors are used to trigger the request and they are mapped to the specific element that needs to be collapsed. In Bootstrap, the collapsing element animates the height of the element from its current height to 0.


2 Answers

If your using SASS to compile bootstrap.css then change the time specified for the $transition-collapse variable in the _variables.scss file.

Default setting, for Bootstrap v4.2.1, is on line 254:

$transition-collapse: height .35s ease !default;

I changed .35s to .15s but you can set yours to something your happier with.

like image 127
Drew Avatar answered Sep 16 '22 20:09

Drew


It's about the transition style. You can specify it in s for seconds or ms for milliseconds. I have, through experimentation, discovered that this is the minimum html required. Note that Bootstrap uses the id and data-target to associate the button with the section to collapse. So, if you have more than one set of collapsible sections, you'll need separate id's.

<button
  data-toggle="collapse"
  data-target="#collapseExample">button name</button>
...
<div style="transition: 1s;" id="collapseExample">
your collapsible stuff goes here.
</div>

Additional information:

  • W3 School example and 'try it yourself' link
  • Collapse example in general with Bootstrap
like image 41
Display name Avatar answered Sep 18 '22 20:09

Display name