Css3 one-way transition?
Achieving CSS one-way transitions involves assigning the transition
property to the element's default state. When activated, it creates a smooth transition, but the effect is instant upon a return to the default state:
This code snippet changes the background color to red over 1 second when hovered, but instantly turns it back to blue when no longer hovered.
Utilize pseudo-classes for one-way transitions
Pseudo-classes like :active
or :focus
are fantastic for one-way transitions. When applied to a button using :active
, it creates a press-down effect that is instantaneously removed when released:
This effect results in a prevalent transition out and an immediate return to the original state.
Making your transition smooth
A ease-in
timing function brings a natural feel to your one-way transitions, enabling a more gradual transition-out effect:
Compatibility matters
Browser compatibility shouldn't be an afterthought. Using vendor prefixes like -webkit-
or -moz-
ensures cross-browser compatibility. Although modern browsers are often forgiving, a serious CSS player never forgets about legacy support:
Input feedback with zero-duration transitions
Transitions can provide immediate feedback too. By assigning a transition duration of 0s
, the changed property appears instantaneously, giving a responsive feel to your user input. It is highly effective for interactive elements like button clicks:
Property-specific transitions
Be selective and animate specific properties, rather than using the general all
keyword. This not only improves performance but also avoids unwanted transitions on other properties:
Preventing transitions on page load
At times, you may want to prevent initial transitions on page load. You can do so by adding a class with JavaScript after the initial render to activate the transition:
Was this article helpful?