Search code examples
javascriptsvggsap

Greensock animation not working on SVG height


I just wanted to do a very simple tweenMax animation, where the height of the bar goes from "0" to "100%". Now my SVG looks like below:

<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
     viewBox="0 0 69 64" style="enable-background:new 0 0 69 64;" xml:space="preserve">
            <style type="text/css">
                .st0{fill:#712215;}
                .st1{fill:none;stroke:#712215;stroke-miterlimit:10;}
            </style>
            <path id="XMLID_8_" class="st0" d="M23,47.4h-5.4c-0.6,0-1-0.5-1-1v-6.9c0-0.5,0.4-1,1-1H23c0.5,0,1,0.5,1,1v6.9
                C24,47,23.5,47.4,23,47.4z"/>
            <path id="XMLID_7_" class="st0" d="M31.9,47.4h-5.4c-0.5,0-1-0.5-1-1V32.9c0-0.5,0.5-1,1-1h5.4c0.6,0,1,0.5,1,1v13.5
                C32.9,47,32.5,47.4,31.9,47.4z"/>
            <path id="XMLID_6_" class="st0" d="M41.9,47.4h-5.4c-0.6,0-1-0.5-1-1V36c0-0.5,0.4-1,1-1h5.4c0.5,0,1,0.5,1,1v10.5
                C42.9,47,42.5,47.4,41.9,47.4z"/>
            <path id="XMLID_5_" class="st0" d="M51,47.4h-5.4c-0.5,0-1-0.5-1-1V30c0-0.5,0.5-1,1-1H51c0.6,0,1,0.5,1,1v16.4
                C52,47,51.5,47.4,51,47.4z"/>
            <polygon id="XMLID_4_" class="st0" points="16.6,35 18,36.4 29.6,25.6 38.9,33 49.3,21.9 47.3,20 38.9,29.5 29.2,21.8 15.9,34.5 "/>
            <polygon id="XMLID_3_" class="st0" points="43.2,17.8 51.7,17.9 51.9,26 "/>
            <rect id="XMLID_2_" x="2.5" y="11.6" class="st1" width="63.5" height="40.8"/>
            </svg>

Fiddle here. Now I have the following JavaScript to animate the bars:

$(function () {   
    var tx = new TimelineMax();
    TweenMax.fromTo($('path')[0] , 2 , { css : { height : 0 } } , { css : { height : '100px' } });
});

But the height animation doesn't seem to be working. What am I doing wrong?


Solution

  • The problem is that height isn't an attribute supported by the path element;

    an easy fix would be using transform:scaleY :

      TweenMax.fromTo($('path')[0], 2, {
        css: {
          transform: 'scaleY(0)'
         // height: 0
        }
      }, {
        css: {
         transform: 'scaleY(1)'
         //height: '100px'
        }
      });
    

    fiddle

    For a more customized solution you might look into changing the d attribute...