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

javascript - Change border-radius of div dynamically - Stack Overflow

programmeradmin1浏览0评论

I have a red div in the background with curved borders and a yellow div rendered over it with a curved border too. The yellow bar is slowly filling the red bar (to indicate progress, for example). See this fiddle:

/

Code

<div id="topDiv" style="height:20px; width:200px; background-color:red; border-radius:10px;">
    <div id="childDiv" style="height:20px; width:100px; background-color:yellow; border-top-left-radius:10px; border-bottom-left-radius:10px;">
    </div>
</div>
<br/>
<br/>    
<input id="change" type="button" value="Change Width" style="height:25px; width:100px;"></input>

$("#change").click(function(){
 $("#childDiv").width((parseInt($("#childDiv").width()) + 5) + "px");
});

Now, what I would like is for the yellow bar's right border to be flat (not curved) when it is not fully filling the red bar. But it should bee curved when it fully fills the red bar. Can someone suggest the best way to implement this?

I have a red div in the background with curved borders and a yellow div rendered over it with a curved border too. The yellow bar is slowly filling the red bar (to indicate progress, for example). See this fiddle:

http://jsfiddle/a6Xy9/3/

Code

<div id="topDiv" style="height:20px; width:200px; background-color:red; border-radius:10px;">
    <div id="childDiv" style="height:20px; width:100px; background-color:yellow; border-top-left-radius:10px; border-bottom-left-radius:10px;">
    </div>
</div>
<br/>
<br/>    
<input id="change" type="button" value="Change Width" style="height:25px; width:100px;"></input>

$("#change").click(function(){
 $("#childDiv").width((parseInt($("#childDiv").width()) + 5) + "px");
});

Now, what I would like is for the yellow bar's right border to be flat (not curved) when it is not fully filling the red bar. But it should bee curved when it fully fills the red bar. Can someone suggest the best way to implement this?

Share Improve this question asked Jun 20, 2014 at 17:00 AmarAmar 1,9362 gold badges16 silver badges29 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 8

Just add overflow: hidden to the #topDiv

JSFiddle

Since the #childDiv is a child of the #topDiv, this should do the trick:

#topDiv { overflow:hidden; }

To answer the question however, to dynamically change border-radius, try this:

$("#childDiv").css('border-radius', '10px');

But in your case, the overflow property is the best implementation.

Inside of your $("#change").click() function, place this conditional statement. It will pare the widths of the two divs and if th child width is greater than or equal to the top div, it will adjust your border radius of the child div.

$("#change").click(function(){
    $("#childDiv").width((parseInt($("#childDiv").width()) + 5) + "px");

    var filled = $("#childDiv").width() >= $("#topDiv").width();
    $("#childDiv").css('border-radius', filled ? '10px' : 0);
});
发布评论

评论列表(0)

  1. 暂无评论