jQuery animate css border-radius property (webkit, mozilla)

被刻印的时光 ゝ 提交于 2019-12-17 15:28:35

问题


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

回答1:


I originally expected that something like...

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

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

Well... border-radius has sort of a history of implementation differences.

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

$("selector")
  .css({
    borderTopLeftRadius: 10, 
    borderTopRightRadius: 10, 
    borderBottomLeftRadius: 10, 
    borderBottomRightRadius: 10 })
  .animate({
    borderTopLeftRadius: 30, 
    borderTopRightRadius: 30, 
    borderBottomLeftRadius: 30, 
    borderBottomRightRadius: 30}, 900);

Note that if you wish to maintain compatibility with older browsers, you can go all-out and use the old browser-prefixed names:

$("selector")
  .css({
    borderTopLeftRadius: 10, 
    borderTopRightRadius: 10, 
    borderBottomLeftRadius: 10, 
    borderBottomRightRadius: 10,
    WebkitBorderTopLeftRadius: 10, 
    WebkitBorderTopRightRadius: 10, 
    WebkitBorderBottomLeftRadius: 10, 
    WebkitBorderBottomRightRadius: 10, 
    MozBorderRadius: 10 
  })
  .animate({
    borderTopLeftRadius: 30, 
    borderTopRightRadius: 30, 
    borderBottomLeftRadius: 30, 
    borderBottomRightRadius: 30,
    WebkitBorderTopLeftRadius: 30, 
    WebkitBorderTopRightRadius: 30, 
    WebkitBorderBottomLeftRadius: 30, 
    WebkitBorderBottomRightRadius: 30, 
    MozBorderRadius: 30 
  }, 900); 

This starts to get pretty crazy though; I would avoid it if possible.




回答2:


Use cssHooks.

This will help you out:

http://www.webmuse.co.uk/articles/border_radius_csshook_with_internet_explorer_support/

Links to the cssHooks:

https://github.com/brandonaaron/jquery-cssHooks

Good luck!




回答3:


Juste an advice, we can use a function to detect browser's CSS prefix Here a sample code.. http://jsfiddle.net/molokoloco/f6Z3D/



来源:https://stackoverflow.com/questions/1010058/jquery-animate-css-border-radius-property-webkit-mozilla

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!