animating a smooth css grid-column change

前端 未结 2 1261
無奈伤痛
無奈伤痛 2021-01-18 17:18

I am using CSS grid layout to position various parts on a website. I use grid-column: x / x; and grid-row x / x; to set their position and size on

2条回答
  •  盖世英雄少女心
    2021-01-18 17:51

    There are no CSS transitions for grid-column or grid-row properties. In order to have any,

    grid-column: 2.5 / 9.5;
    

    ...would have to make sense from a rendering point of view.
    Imagine you're the guy coding the browser. How would you render a grid-column:2.5 / 9.5;?

    Expecting CSS transitions to work here is like expecting them to work in CSS columns when content moves between columns.

    Need animations with CSS Grid? JavaScript is your only friend.

    Here's how I'd go about it:

    1. clone contents of each grid element into absolutely positioned containers.
    2. fade real content out, using opacity
    3. apply the new grid-columns property to the grid
    4. animate cloned elements into new positions of real ones
    5. fade cloned content out and the real one back in
    6. delete clones.

    Making this work cross-browser/cross-device might pose a few problems, but it's doable.

    Note: You might want to consider adding a layer of wrappers between the contents of your grid elements and the grid elements, to help with two things:

    1. make sure each grid element has only one child (to simplify getting the width/height for calculating transition)
    2. simplify tracking each element so you know what to transition where

提交回复
热议问题