Add CSS3 transition expand/collapse

后端 未结 5 901
难免孤独
难免孤独 2020-12-13 05:59

How do I add an expand/collapse transition?

5条回答
  •  既然无缘
    2020-12-13 06:23

    This is my solution that adjusts the height automatically:

    function growDiv() {
      var growDiv = document.getElementById('grow');
      if (growDiv.clientHeight) {
        growDiv.style.height = 0;
      } else {
        var wrapper = document.querySelector('.measuringWrapper');
        growDiv.style.height = wrapper.clientHeight + "px";
      }
      document.getElementById("more-button").value = document.getElementById("more-button").value == 'Read more' ? 'Read less' : 'Read more';
    }
    #more-button {
      border-style: none;
      background: none;
      font: 16px Serif;
      color: blue;
      margin: 0 0 10px 0;
    }
    
    #grow input:checked {
      color: red;
    }
    
    #more-button:hover {
      color: black;
    }
    
    #grow {
      -moz-transition: height .5s;
      -ms-transition: height .5s;
      -o-transition: height .5s;
      -webkit-transition: height .5s;
      transition: height .5s;
      height: 0;
      overflow: hidden;
    }
    
    
    
    Here is some more text: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum vitae urna nulla. Vivamus a purus mi. In hac habitasse platea dictumst. In ac tempor quam. Vestibulum eleifend vehicula ligula, et cursus nisl gravida sit amet. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.

    I used the workaround that r3bel posted: Can you use CSS3 to transition from height:0 to the variable height of content?

提交回复
热议问题