jquery animate for element attributes not style

前端 未结 5 497
心在旅途
心在旅途 2021-01-11 18:40

ASAIK jquery animate function accepts style properties only. but i want to animate the attributes of an element. consider a SVG element rectangle

         


        
5条回答
  •  一生所求
    2021-01-11 18:49

    I like the Hoffmann approach, but i think is more elegant without creating a virtual dom object.

    This is my coffeescript snippet

    $rects.each ->
      that = @
      $({width: 0}).animate
      width: parseInt($(@).attr('width'))
      ,
      duration: 2000
      easing: 'easeIn'
      step: ->
        $(that).attr 'width', Math.round(@.width)
      done: ->
        console.log 'Done'
    

    which compiles into

    return $rects.each(function() {
      var that;
      that = this;
      return $({
        width: 0
      }).animate({
        width: parseInt($(this).attr('width'))
      }, {
        duration: 1000,
        easing: 'easeIn',
        step: function() {
          return $(that).attr('width', Math.round(this.width));
        },
        done: function() {
          return console.log('Done');
        }
      });
    });
    

提交回复
热议问题