CSS/JS: Animate Inline Element Upon Text Change

前端 未结 3 1628
再見小時候
再見小時候 2021-01-06 03:15

When an inline element\'s text changes, it is usually the case that its computed width or height changes as well.

Usually it\'

3条回答
  •  渐次进展
    2021-01-06 04:02

    I suppose that you will need two elements to achieve this elegantly:

    $(".inner").on("click", function() {
      var $this = $(this);
      var $par = $this.parent();
      $par.css({
        width: $par.width()
      });
    
      $this.text("New text");
    
      $par.css({
        width: $this.outerWidth()
      });
    
    });
    .inner {
      display: inline-block;
      padding: 8px 16px;
      white-space: nowrap;
    }
    
    .outer {
      display: inline-block;
      transition: width 300ms ease-in-out;
      background-color: red;
      overflow: hidden;
    }
    
    
    Here's some text.

提交回复
热议问题