CSS/JS: Animate Inline Element Upon Text Change

前端 未结 3 1631
再見小時候
再見小時候 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:04

    Here an Update: https://jsfiddle.net/ky3c5Lec/3/

    $("div").on("click", function() {
        //get the current Dimensions, as Start-value for the animation
        var $this = $(this),
            sw = $this.width(),
            sh = $this.height();
    
        $this.text("New text");
        var tw = $this.width(),
            th = $this.height();
    
        $this.css({
            //since jQuery.animate() doesn't have sth. like Tween.from() 
            //we have to reset the styles to the initial values
            width: sw, height: sh
        }).animate({
            //and then animate 
            width: tw, height: th
        }, function(){
            //and when the animation is done, we clean up after ourselves
            $this.css({
                width: "", height: ""
            });
        })
    });
    

提交回复
热议问题