Animating max-height with CSS transitions

前端 未结 6 2041
死守一世寂寞
死守一世寂寞 2020-12-02 21:58

I want to create an expand/collapse animation that\'s powered only by classnames (javascript is used to toggle the classnames).

I\'m giving one class max-heigh

6条回答
  •  温柔的废话
    2020-12-02 22:30

    The solution is actually quite simple. Make a child div, that has the content. The parent div will be the one that expands collapses.

    On load the parent div will have a max-height. when toggling, you can check the child height by writing document.querySelector('.expand-collapse-inner').clientHeight; and set the maxheight with javascript.

    In your CSS, you will have this

    .parent {
    transition: max-height 250ms;
    }
    

提交回复
热议问题