Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Twitter Bootstrap alerts fade in as well as out?

When I first saw the alerts in Bootstrap I thought they would behave like the modal window does, dropping down or fading in, and then fading out when closed. But it seems like they are always visible. I guess I could have them sit in a layer above my app and manage showing them but I was wondering if the functionality was built in?

thanks!

Edit, what I have so far:

<div id="saveAlert" class="alert-message success fade in" data-alert="alert" style="top:0">   <a class="close" href="#">×</a>   <p><strong>Well done!</strong> You successfully read this alert message.</p> </div> 
like image 595
kreek Avatar asked Oct 06 '11 15:10

kreek


People also ask

How do I make bootstrap alerts automatically disappear?

To close the alert message, add a . alert-dismissible class to the alert container. Then add class="close" and data-dismiss="alert" to a link or a button element (when you click on this the alert box will disappear).

Which bootstrap 4 class can be used to create successful alert?

Bootstrap 4 provides an easy way to create predefined alert messages. Alerts are created with the . alert class, followed by one of the eight contextual classes .

How do I center a bootstrap alert?

Use center-block instead of text-center with alert if you want to center align the whole alert. Show activity on this post. To align the alert to the center without adding extra containers, I customized the Bootstrap alert class to look as follows.


1 Answers

I strongly disagree with most answers previously mentioned.

Short answer:

Omit the "in" class and add it using jQuery to fade it in.

See this jsfiddle for an example that fades in alert after 3 seconds http://jsfiddle.net/QAz2U/3/

Long answer:

Although it is true bootstrap doesn't natively support fading in alerts, most answers here use the jQuery fade function, which uses JavaScript to animate (fade) the element. The big advantage of this is cross browser compatibility. The downside is performance (see also: jQuery to call CSS3 fade animation?).

Bootstrap uses CSS3 transitions, which have way better performance. Which is important for mobile devices:

Bootstraps CSS to fade the alert:

.fade {   opacity: 0;   -webkit-transition: opacity 0.15s linear;   -moz-transition: opacity 0.15s linear;   -o-transition: opacity 0.15s linear;   transition: opacity 0.15s linear; } .fade.in {   opacity: 1; } 

Why do I think this performance is so important? People using old browsers and hardware will potentially get a choppy transitions with jQuery.fade(). The same goes for old hardware with modern browsers. Using CSS3 transitions people using modern browsers will get a smooth animation even with older hardware, and people using older browsers that don't support CSS transitions will just instantly see the element pop in, which I think is a better user experience than choppy animations.

I came here looking for the same answer as the above: to fade in a bootstrap alert. After some digging in the code and CSS of Bootstrap the answer is rather straightforward. Don't add the "in" class to your alert. And add this using jQuery when you want to fade in your alert.

HTML (notice there is NO in class!)

<div id="myAlert" class="alert success fade" data-alert="alert">   <!-- rest of alert code goes here --> </div> 

Javascript:

function showAlert(){   $("#myAlert").addClass("in") } 

Calling the function above function adds the "in" class and fades in the alert using CSS3 transitions :-)

Also see this jsfiddle for an example using a timeout (thanks John Lehmann!): http://jsfiddle.net/QAz2U/3/

like image 162
DivZero Avatar answered Oct 17 '22 21:10

DivZero