把 DIV 转为圆角矩形,支持背景图

piaoling  2011-04-26 19:17:15

感谢6楼的帮助,现在 IE、FF、OP 均通过测试!(代码已更改)

背景图仅仅支持平铺……

还有个不好的地方,控制样式必须写在style属性里,关联的class不行。。。。

偶做了一下午,希望大家一起来改进。。

调用方法:
1、把 JS 函数 copy 到页面里先。
2、给要变圆角的 div 设置一个 id,比如 abc
3、在 div 后面写:<script> yj("abc"); </script> 即可

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
<script>
function yj(id){
 /* circular bead 2008
    code by : weiweiok
    thanks : goodlucklg
 */
 var obj = document.getElementById(id);
 var content = obj.innerHTML;
 var s = obj.style;
 
 var width = s.width.replace(/px/gi,"");
 var height = s.height.replace(/px/gi,"");
 var bc = s.borderColor;
 var bg = s.backgroundImage;
 var bgc = s.backgroundColor;
 var p = s.padding;
 var pt = s.paddingTop.replace(/px/gi,"");
 var pr = s.paddingRight.replace(/px/gi,"");
 var pb = s.paddingBottom.replace(/px/gi,"");
 var pl = s.paddingLeft.replace(/px/gi,"");
 
 s.padding = "";
 s.width = "";
 s.height = "";
 s.border = "";
 s.background = "";
 bc = (bc == "") ? "#000000" : bc;
 if(window.navigator.userAgent.indexOf("Firefox")>=0){
  bc = String(bc).toLowerCase().replace(/rgb(.+?)/g,function(){return "#"+arguments[0].replace(/(s|(|)|rgb)/g,"").replace(/d+,*/g,function(){return parseInt(arguments[0]).toString(16).toLowerCase();});}).substring(0,7);
 }
 bgc = (bgc == "") ? "none" : bgc;
 pt = (pt == "") ? 0 : Math.round(pt);
 pr = (pr == "") ? 0 : Math.round(pr);
 pb = (pb == "") ? 0 : Math.round(pb);
 pl = (pl == "") ? 0 : Math.round(pl);
 width = (width == "") ? (20+pl+pr) : Math.round(width)+pl+pr+2;
 height = (height == "") ? (20+pt+pb) : Math.round(height)+pt+pb+2;
 a1 = "<div style='font:0px;margin:0px;margin-left:5px;width:" + (width-10) + "px;height:1px;background:"+bc+" '></div>";
 a2 = "<div style='font:0px;margin:0px;margin-left:3px;width:" + (width-10) + "px;height:1px;background:"+bgc+" "+bg+" -4px 0px;border-left:2px solid "+bc+";border-right:2px solid "+bc+";'></div>";
 a3 = "<div style='font:0px;margin:0px;margin-left:2px;width:" + (width-6) + "px;height:1px;background:"+bgc+" "+bg+" -2px -1px;border-left:1px solid "+bc+";border-right:1px solid "+bc+";'></div>";
 a4 = "<div style='font:0px;margin:0px;margin-left:1px;width:" + (width-4) + "px;height:2px;background:"+bgc+" "+bg+" -1px -2px;border-left:1px solid "+bc+";border-right:1px solid "+bc+";'></div>";
 a5 = "<div style='margin:0px;padding:"+p+";width:" + (width-2-pl-pr) + "px;height:" + (height-10-pt-pb) + "px;background:"+bgc+" "+bg+" 0px -4px;border-left:1px solid "+bc+";border-right:1px solid "+bc+";'>" + content + "</div>";
 a6 = "<div style='font:0px;margin:0px;margin-left:1px;width:" + (width-4) + "px;height:2px;background:"+bgc+" "+bg+" -1px -"+(height-2-10+4)+"px;border-left:1px solid "+bc+";border-right:1px solid "+bc+";'></div>";
 a7 = "<div style='font:0px;margin:0px;margin-left:2px;width:" + (width-6) + "px;height:1px;background:"+bgc+" "+bg+" -2px -"+(height-2-10+6)+"px;border-left:1px solid "+bc+";border-right:1px solid "+bc+";'></div>";
 a8 = "<div style='font:0px;margin:0px;margin-left:3px;width:" + (width-10) + "px;height:1px;background:"+bgc+" "+bg+" -4px -"+(height-2-10+7)+"px;border-left:2px solid "+bc+";border-right:2px solid "+bc+";'></div>";
 a9 = "<div style='font:0px;margin:0px;margin-left:5px;width:" + (width-10) + "px;height:1px;background:"+bc+" '></div>";
 obj.innerHTML = a1+a2+a3+a4+a5+a6+a7+a8+a9;
}
</script>
</head>
<body>
<div id="a" style="width:100px;height:100px;border:1px solid red">
 你好,圆角!
</div>
<div id="b" style="width:150px;height:110px;border:1px solid #33aa33;background:#66ff66;margin:20px;padding:10px;">
 要改变样式,只需在div里写css即可。。。比如我的背景色、边框色
</div>
<div id="c" style="width:300px;height:100px;background:url(http://www.sxrjsc.com/Tmp/a.jpg);padding:10px;color:#ffffff">
 还支持背景图。。。不过。。。只支持平铺方式的……
</div>
<script>
document.write("<p><a href='javascript:bian()'>给我圆!!!</a></p>");
function bian(){
    yj("a");
    yj("b");
    yj("c");
}
</script>
</body>
</html>

类别 :  JS(21)  |  浏览(3562)  |  评论(0)
发表评论(评论将通过邮件发给作者):

Email: