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?
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.
The transition-timing-function property is used to specify how the pace of the transition changes over its duration.
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 ...
CSS transitions allows you to change property values smoothly, over a given duration.
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;
}
:not()
There are two things to remember when using CSS transitions:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With