Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS3 transition only when class is added, not when removed

I have the following CSS example:

.message{
    background-color: red;
    transition: background-color 5s;
    -webkit-transition: background-color 5s; /* Safari */
    transition-delay: 2s;
    -webkit-transition-delay: 2s; /* Safari */
}

.unreadMessage{
    background-color: blue;
}

Then, i have a DIV with .message class, and by pressing a Button, i add the class .unreadMessage, and by pressing another Button, i remove it.

With this example, every time i change background-color, by adding or removing .unreadMessage, it does the CSS transition.

What i want to do, is, if possible, to have an instant color change when i add .unreadMessage, and have the transition only when removing it.

The first thing that come in my mind, was to have a different class containing the CSS transition properties, and add it after adding .unreadMessage.

But it is possible to do it with only one class, or using a Javascript workaround?

like image 511
Fr0z3n Avatar asked May 20 '13 09:05

Fr0z3n


People also ask

Is transition inherited CSS?

Specifically around properties, like transition properties, that are not inherited by default. Run this demo in my JavaScript Demos project on GitHub. First off, this is not an AngularJS-specific problem (as you might be lead to believe from the post title). This is just a byproduct of CSS behavior.

Which css3 transition specifies how the pace of transition changes over its duration?

The transition-timing-function property is used to specify how the pace of the transition changes over its duration.

How does transition CSS work?

CSS transitions let you decide which properties to animate (by listing them explicitly), when the animation will start (by setting a delay), how long the transition will last (by setting a duration), and how the transition will run (by defining a timing function, e.g., linearly or quick at the beginning, slow at the ...

Why do you use css3 transitions?

CSS transitions allows you to change property values smoothly, over a given duration.


2 Answers

If you want to only apply a transition when the .message element does not have the unreadMessage class, then put the transition properties in the .message:not(.unreadMessage) selector:

.message{
    background-color: red;
}
.message:not(.unreadMessage) {
    -webkit-transition: background-color 5s; /* Safari */
    transition: background-color 5s;
    -webkit-transition-delay: 2s; /* Safari */
    transition-delay: 2s;
}

.unreadMessage{
    background-color: blue;
}
  • Demo: http://jsfiddle.net/Hs8fa/
  • Documentation for :not()
like image 94
Rob W Avatar answered Sep 24 '22 08:09

Rob W


There are two things to remember when using CSS transitions:

  1. Transitions happen when an element's state is modified "using pseudo-classes like :hover or :active or dynamically set using JavaScript."
  2. You have to have a starting point and an ending point or they won't work.

The biggest issue with OP's question isn't their CSS, it's their naming structure. A major pattern of CSS transitions is to modify an element's class (or in the MDN's language "dynamically set using Javascript"). In OP's example they're not modifying an element's class structure, they're changing classes. CSS transitions won't work when an element changes from one class to another, but they will work when a class is added or taken away.

The easiest example of this is going from .element to .element.active. If we put the transition on the base class, .element, and then add a modifying class, .active, the transitions applied to .element will transition from .element settings to .element.active. settings.

Here's a JSFiddle example of modifying a base class

Secondly, and this is one I forget all the time, the base class must have a starting style. I can't transition left in the modified state if I don't have left set in the base state.

like image 21
Pixelsmith Avatar answered Sep 25 '22 08:09

Pixelsmith