Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS transitions for layout changes (as other elements are revealed in the DOM)?

When a new block is displayed, other blocks may move around the page.

Is it possible to use CSS transitions to animate the movement of a block, when another block is displayed?

Initial state:

enter image description here

Subsequent state:

enter image description here

Eg:

  • Initially only the blue block is displayed
  • The green block is displayed, pushing the blue block down
  • I would like the blue block to animate moving to its new position

Obligatory JSFiddle

Is it possible to animate the blue block moving to its new position? I have CSS transition set on both the blocks and their parents.

Or (as I'm beginning to suspect) are CSS transitions only for actual CSS changes, not the subsequent effects of those CSS changes?

like image 608
mikemaccana Avatar asked May 02 '14 10:05

mikemaccana


People also ask

What does the transition do in CSS?

CSS transitions provide a way to control animation speed when changing CSS properties. Instead of having property changes take effect immediately, you can cause the changes in a property to take place over a period of time.

How many types of transitions are there in CSS?

transition-property. transition-duration. transition-timing-function. transition-delay.

How are CSS transitions triggered?

Triggering transitions You can trigger CSS transitions directly with pseudo classes like :hover (activates when the mouse goes over an element), :focus (activates when a user tabs onto an element, or when a user clicks into an input element), or :active (activates when user clicks on the element).


2 Answers

You can make layout changes with CSS3 as you can animate margin, position, width, height...

The problem is triggering those layout changes. but you can use help from JS to trigger those animations via a class change.

For your example, you could do this :

FIDDLE

CSS :

.item {
    width: 150px;
    height: 150px;
    margin: 20px;
    overflow: auto;
    transition: all 0.5s ease-out;
}
.first {
    margin-top:-170px;
}

JS :

setTimeout(function () {
    document.querySelector('.first').classList.remove("first");
}, 2 * 1000)
like image 114
web-tiki Avatar answered Dec 24 '22 23:12

web-tiki


There is a technique for this called FLIP (First, Last, Invert, Play) and a library implementing it called Flipping.js. It is described in this article.

like image 31
thSoft Avatar answered Dec 25 '22 00:12

thSoft