Is it possible to animate Flexbox inserts & removes?

前端 未结 6 2111
时光取名叫无心
时光取名叫无心 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:57

    Remember that the Flexible Box Model and Grid Layout specifications are changing constantly, even the properties and valid values. The browser implementations are far from complete as well. That being said, you can transition on the flex property so that the elements transition smoothly, then just listen for TransitionEnd to finally remove the node from the DOM tree.

    Here is an example JSFiddle, running in Chrome 21: http://jsfiddle.net/5kJjM/ (click the middle div)

    var node = document.querySelector('#remove-me');
    
    node.addEventListener('click', function(evt) {
      this.classList.add('clicked');
    }, false);
    
    node.addEventListener('webkitTransitionEnd', function(evt) {
      document.querySelector('#flexbox').removeChild(this);
    }, false);
    #flexbox {
      display: -webkit-flex;
      -webkit-flex-flow: row;
    }
    
    .flexed {
      border: 1px solid black;
      height: 100px;
      -webkit-flex: 1 0 auto;
      -webkit-transition: -webkit-flex 250ms linear;
    }
    
    .clicked {
      -webkit-flex: 0 0 auto;
    }

    Edit: To further clarify, when you remove a node, you should set its flex to 0, then remove it from the DOM. When adding a node, add it in with flex: 0, then transition it to flex:1

提交回复
热议问题