Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Transition acceleration

Tags:

html

css

How can I change the acceleration of how fast a CSS transition speeds up and slows down.

For example, if I run this code:

<style> 
div {
    width: 100px;
    height: 100px;
    background: red;
    transition-duration: 4s;
}

div:hover {
    width: 200px;
    background: blue;
}
</style>
</head>
<body>

<div></div>

it speeds up at a specific speed and starts slowing down at a specific speed.

How can I change the speed of how fast it speeds up and slows down (not the actual transition time, just the acceleration and deceleration)

like image 582
kirill2485 Avatar asked Jan 17 '15 04:01

kirill2485


2 Answers

CSS3 transition-timing-function Property

The transition-timing-function property specifies the speed curve of the transition effect.

This property allows a transition effect to change speed over its duration.

Syntax

transition-timing-function: ease|linear|ease-in|ease-out|ease-in-out|cubic-bezier()|initial|inherit;

DOCUMENTATION HERE w/ Examples

like image 186
Rafael Avatar answered Sep 21 '22 09:09

Rafael


For this, you need to use transition-timing-function.

From W3C

The transition-timing-function property describes how the intermediate values used during a transition will be calculated. It allows for a transition to change speed over its duration. These effects are commonly called easing functions.

Now, you want to create a deceleration transition. For this, you need to look at position - time graph of deceleration and create a cubic-bezier() function similar to it.

enter image description here

Alter the values of the cubic-bezier function to suit your needs.

Code Snippet :

div {
    width: 100px;
    height: 100px;
    background: red;
    transition-duration: 1s;
    transition-timing-function:cubic-bezier(0.2,0.5,0.3,1);
}

div:hover {
    width: 200px;
    background: blue;
}
<div></div>
like image 41
The Pragmatick Avatar answered Sep 20 '22 09:09

The Pragmatick