Is it possible to animate Flexbox inserts & removes?

前端 未结 6 2118
时光取名叫无心
时光取名叫无心 2020-12-09 07:41

When I remove an item from a flexbox, the remaining items \"snap\" into their new positions immediately rather than animating.

Conceptually, since the items are chan

6条回答
  •  天涯浪人
    2020-12-09 07:52

    I have been trying to animate rows in my flexbox. This was not possible through just css. So I did it with a bit of javascript and an extra parent for my flexbox rows.

    HTML :

    Row number 1

    CSS :

    .row{
      height : 40px;
      width : 200px;
      border: 1px solid #cecece;
      text-align : center;
      padding-top : 20px;
    }
    
    .outer-container {
      height : 500px;
      border : 1px solid blue;
      width : 250px;
      display: flex;
      justify-content : center;
      align-items: center;
    }
    
    .inner-container { 
      height : 42px;
      transition : height 0.3s;
     }
    
    button {
      width : 200px;
      height: 30px;
      margin-left : 80px;
      margin-top : 10px;
    }
    

    Javascript :

    (() => {
        let count = 1;
        document.querySelector("#add").addEventListener('click', () => {
            const template = document.createElement('div');
            count += 1;
            template.textContent = `Row number ${count}`;
            template.className = 'row';
    
            const innerContainer = document.querySelector('.inner-container');
            innerContainer.appendChild(template);
            innerContainer.style.height = `${innerContainer.clientHeight + 42}px`;
        })
    })();
    

    Working demo : https://jsfiddle.net/crapbox/dnx654eo/1/

提交回复
热议问题