最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

css - Border Radius Using Javascript - Stack Overflow

programmeradmin3浏览0评论

Using CSS, separate border radius's are set like so:

border-top-left-radius: 10px;   
border-top-right-radius: 10px;   
border-bottom-left-radius: 10px;   
border-bottom-right-radius: 10px;

But how would I go along setting these separate border radius's using Javascript?

Using CSS, separate border radius's are set like so:

border-top-left-radius: 10px;   
border-top-right-radius: 10px;   
border-bottom-left-radius: 10px;   
border-bottom-right-radius: 10px;

But how would I go along setting these separate border radius's using Javascript?

Share Improve this question edited Jun 10, 2014 at 21:36 Lodder asked Jun 10, 2012 at 20:27 LodderLodder 19.7k11 gold badges62 silver badges102 bronze badges 1
  • You're going to have to be more explicit about what you're looking for. – Paul Tomblin Commented Jun 10, 2012 at 20:29
Add a ment  | 

3 Answers 3

Reset to default 8
<div id="target">Lorem Ipsum</div>

<script type="text/javascript">
    var target = document.getElementById("target");
    target.style.borderTopLeftRadius = "20px";
</script>

Live Example

element.style['border-top-left-radius'] = '4px';
element.style['border-top-right-radius'] = '4px';
element.style['border-bottom-left-radius'] = '4px';
element.style['border-bottom-right-radius'] = '4px';

Or even smaller:

element.style['border-radius'] = '4px';

You can also use:

element.style.borderTopLeftRadius = '4px';
element.style.borderTopRightRadius = '4px';
element.style.borderBottomLeftRadius = '4px';
element.style.borderBottomRightRadius = '4px';

But remember that it's not (yet) a web-standard, so each browser has it's own declaration:

element.style['border-radius']//Future standard
element.style['-webkit-border-radius']//Webkit(Safari and Chrome)
element.style['-moz-border-radius']//Mozilla Firefox
element.style['-o-border-radius']//Opera

If at all possible, it's likely best to make CSS classes with each corner's rounding specified and then use simple JS to swap between classes. That way you can specify fallbacks a bit easier if a browser needs -webkit-border-radius or border-radius or is IE, for example.

发布评论

评论列表(0)

  1. 暂无评论