views:

3140

answers:

2

Is there a way in jQuery to animate the css3 border-radius property available in webkit and mozilla browsers?

I haven't found a plugin that will do it.

-webkit-border-radius

-moz-border-radius

Thanks.

+7  A: 

I expected something like...

$("selector")
  .css({WebkitBorderRadius: 10, MozBorderRadius: 10, BorderRadius: 10})
  .animate({WebkitBorderRadius: 30, MozBorderRadius: 30, BorderRadius: 30}, 900);

...would do to cover your bases. But, I was wrong: Webkit allows you to set the value for all four corners via WebkitBorderRadius, but won't let you read it back - so with the code above, the animation will always start at 0 instead of 10. Firefox will let you read it back, so everything works as expected there.

Well... I guess if it was all standardized, then they wouldn't need to be using the browser-specific prefixes.

Fortunately, there's a work-around: just specify each corner radius individually:

$("selector")
  .css({
    WebkitBorderTopLeftRadius: 10, 
    WebkitBorderTopRightRadius: 10, 
    WebkitBorderBottomLeftRadius: 10, 
    WebkitBorderBottomRightRadius: 10, 
    MozBorderRadius: 10, 
    BorderRadius: 10 })
  .animate({
    WebkitBorderTopLeftRadius: 30, 
    WebkitBorderTopRightRadius: 30, 
    WebkitBorderBottomLeftRadius: 30, 
    WebkitBorderBottomRightRadius: 30, 
    MozBorderRadius: 30, 
    BorderRadius: 30}, 900);
Shog9
Well, sort of. So far, if there's any radius set in the style sheet, the corners will snap square, then animate to 30 (to use your example) from there. It seems to want to go from 0 to whatever value is given. I get the same result in Safari and Firefox 3.5 beta.
patrick dw
Correction: It seems to work correctly in Firefox. Safari only wants to increment.
patrick dw
Looks like whatever direction I go, Safari always starts by snapping to 0. Any workaround ideas? Thanks.
patrick dw
@patrick: yes, i've found a work-around. Can't test in Safari, but works in Chrome (which exhibited the same bug, so i'd assume it's a general Webkit thing). I've updated the answer, give it a try...
Shog9
Perfect!!! Thank you Shog9 for the answer and workaround!
patrick dw
A: 

What about IE??? border-radius.htc adds 2 wrapper divs, which makes selecting in jquery more complicated, plus, well there's a whole slew of problems with animating border radius in ie...

IE doesn't even have native support for border radius (for now at least), jQuery wasn't meant to make miracles.
Kindred