Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap: How to fade and then hide something using the default 'fade', 'hide', 'in' classes?

Bootstrap obviously makes use of the 'hide', 'fade' and 'in' classes for its transitions.

The problem I'm having is that using 'fade' and 'in' will change the opacity from 0 to 1. The transition effect is perfect, but the content is still there taking up space on the page, even if you can't see it. For example, if it's container has a border, there'll be a big white space before the border.

I want to make use of their exisiting CSS transitions by adding and removing the 'in' class, but I also want whatever element that's fading out, to be hidden, but only after the transition is over. So basically, exactly what they do in the modal, but I don't know how they do it.

In my example below, adding or remove the hide means the div appears or disappears instantly, before the fade effect can happen.

JS fiddle here

Example HTML:

<div class="control-group">
    <label class="control-label">Choose one:</label>
    <div class="controls">
        <label class="radio inline">
            <input type="radio" name="color-radio" id="yellow-trigger" value="yellow" />Yellow Box</label>
        <label class="radio inline">
            <input type="radio" name="color-radio" id="red-trigger" value="red" />Red Box</label>
    </div>
</div>
<div id="yellow-box" class="hide fade">
    <p>I'm yellow</p>
</div>
<div id="red-box" class="hide fade">
    <p>I'm red</p>
</div>

Example JS:

$(document).ready(function () {
    $('#yellow-trigger').click(function () {
        $('#yellow-box').addClass('in').removeClass('hide');
        $('#red-box').addClass('hide').removeClass('in');
    });

    $('#red-trigger').click(function () {
        $('#red-box').addClass('in').removeClass('hide');
        $('#yellow-box').addClass('hide').removeClass('in');
    });
});
like image 863
davidpauljunior Avatar asked May 30 '13 06:05

davidpauljunior


People also ask

Which method is used to hide element with fade effect?

The jQuery fadeIn() method is used to fade in a hidden element.

How do I create a fade in bootstrap?

Step 1: Add bootstrap CDN to your HTML code. Step 2: For making a bootstrap carousel you have to add class = “carousel” in your HTML div box. Step 3: To create the carousel fade in and fade out transition instead of a slider you have to add a class=”carousel-fade”.

Which is used to show and hide the content in JS using bootstrap?

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.

How do you fade a box in CSS?

In the CSS, use the @keyframes rule paired with fadeIn. At 0%, set the opacity to 0. At 100%, set the opacity to 1. This creates the fade-in effect.


2 Answers

Any reason not to just use jQuery for the fadeIn effect? Below is some code to make the fade in effect with jQuery.

Fiddle here

Removed "fade" class

<div id="yellow-box" class="hide">
<p>I'm yellow</p> </div>

Updated jQuery for fade in

$(document).ready(function () {

  $('#yellow-trigger').click(function () {
      $('#red-box').hide();
      $('#yellow-box').fadeIn('slow');
  });

  $('#red-trigger').click(function () {
      $('#yellow-box').hide();
      $('#red-box').fadeIn('slow');     
  });

});
like image 170
Zane Avatar answered Oct 25 '22 15:10

Zane


This is very old, but in case anyone else gets here, the answer is to use a single-shot handler for the synthetic event bsTransitionEnd.

example:

$(".alert").one('bsTransitionEnd',function() {
    $(this).addClass('hide');
});
window.setTimeout(function(){
    $(".alert").removeClass('in');
},1000);
like image 45
Seth Kingsley Avatar answered Oct 25 '22 15:10

Seth Kingsley