14

I am trying to rotate an image via svg's transform. This is the code I have:

<svg width="100" height="100"> 
   <image id="trns" transform="rotate(90,50,50)" width="100" height="100" xlink:href="logo.png"/> 
</svg>

This successfully rotates logo.png by 90 degrees when the page loads. Also, when I change 90 to a different number in firbug's HTML tab the rotation changes accordingly. But when I try to change the value with jQuery, nothing happens:

$('#trns').attr('transform', 'rotate(60, 50,50)');

What does firebug do that my attr line does not?

Majid Fouladpour
  • 29,356
  • 21
  • 76
  • 127

4 Answers4

8

Working fine here (with jQuery 1.6.2): http://jsfiddle.net/niklasvh/k3Grd/

Make sure to call it once the DOM is ready:

$(function(){
 $('#trns').attr('transform', 'rotate(60,50,50)');
});
Niklas
  • 29,752
  • 5
  • 50
  • 71
5

Very strange indeed, this seems to work

$('#trns')[0].setAttribute('transform','rotate(20,50,50)')

Also, if u look at $('#trns').attr('transform'), you are getting an object.. Not enough time to look into that now.

Freek
  • 1,506
  • 1
  • 11
  • 25
  • That indeed is strange, apparently `attr()` in 1.6.2 is sophisticated enough to manipulate the object in the right way. But with 1.3.2, which I used originally, it does not work. – Majid Fouladpour Jul 02 '11 at 10:35
3

If you're using jquery >= 1.6 try using prop instead of attr.
Hope this helps. Cheers

Edgar Villegas Alvarado
  • 18,204
  • 2
  • 42
  • 61
0

.attr() works in 3.1.1 like this

.attr({ style: "prop : val; prop : val; prop : val" })

this might be a way around version problems.

Allan
  • 7
  • 3