Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Transition delay in but not out

Using CSS I'm trying to get a transition delay to work as follows. I want a 1s delay in but I want 0s delay out.

transition: width 0.3s ease-in 1s;
transition: width 0.3s ease-out 0s;

I have the following jsFiddle

like image 757
ngplayground Avatar asked Jul 17 '15 15:07

ngplayground


3 Answers

Have the delay under your .active block, since the element has the active class when it is transitioning to green:

.sample {
    padding: 20px;
    background-color: #efefef;
    transition: background-color 0.3s ease-out 0s;
}
.sample.active {
    background-color: green;
    transition: background-color 0.3s ease-in 1s;
}

JSFiddle

like image 125
George Avatar answered Oct 10 '22 13:10

George


slightly more concise and maintainable code, set the transition-delay property instead of rewriting the whole transition:

.sample {
    padding: 20px;
    background-color: #efefef;
    transition: background-color 0.3s ease-out 0s;
}
.sample.active {
    background-color: green;
    transition-delay: 1s;
}

demo

documentation: https://developer.mozilla.org/en/docs/Web/CSS/transition

like image 28
Luca Avatar answered Oct 10 '22 11:10

Luca


The following worked for me:

.sample {
    padding: 20px;
    background-color: #efefef;
    transition: background-color 0.3s ease-in 0s;
}
.sample.active {
    background-color: green;
    transition-delay: 1s;
}

You don't need an !important declaration because the cascade automatically prefers the later rule when you overwrite a property.

You also don't need to rewrite the entire transition rule since you specifically want to use the same transition with a different delay rather than a different transition.

The reason it's this way around (with the 0s delay by default) is that when you remove the .active class you stop applying its colour as well as its transition delay so the delay in the .sample class is applied.

like image 2
Drew Green Avatar answered Oct 10 '22 13:10

Drew Green