jquery “animate” variable value

后端 未结 9 780
说谎
说谎 2021-02-04 05:36

I need to \"animate\" a variable with jquery.

Example: The variable value is 1. The value should be 10 after 5 seconds. It should be increase \"smoothl

9条回答
  •  礼貌的吻别
    2021-02-04 06:07

    As addition to Ties answer - you dont event need to append dummy element to the DOM. I do it like this:

    $.fn.animateValueTo = function (value) {
    
        var that = this;
    
        $('')
            .css('display', 'none')
            .css('letter-spacing', parseInt(that.text()))
            .animate({ letterSpacing: value }, {
                duration: 1000,
                step: function (i) {
                    that.text(parseInt(i));
                }
            });
    
        return this;
    };
    

提交回复
热议问题