现在实现圆角普遍用图片来控制,这种方法有其优点(产生的圆角平滑)。 但同时他也要求有吻合的图片,如果要动态的改变div的样式颜色则有些力不从心。还有就是用js来实现。
实现后的调用代码 如下
复制代码 代码如下:
var objdiv = getrounddiv.call(document,solid 1px yellow,#dddddd)
objdiv.div.style.width=100px;
objdiv.content.style.margin=6 6 6 6
objdiv.content.innertext=这是一个圆角div测试
document.body.appendchild(objdiv.div);
这样就产生了一个圆角div
实现原理:原理其实很简单,在div的top和bottom 加上三条线,用这三条线的不同长度来产生圆角的效果。
实现过程: 如何实现这三条线呢。 用 这个元素,将其高度 设置为1px 。如果要显示边框则为其添加左边框和右边框。添加好线条以后,将内容div 和这三条线放在一个容器里,这个容器也是一个div。最终返回一个div类,这个类放两个属性,一个是容器div,通过这个容器div可以控制图形出现的位置和大小高度等属性。另一个属性是内容div,通过这个div可以设置这个div的内容,margin,字体颜色,背景颜色,字体大小,等属性。
注意的问题: 调用 getrounddiv 这个方法需要传递一个方法上下文。我的理解是方法上下文相当与一个指针,指向调用方法的对象。为什么要用这个方法上下文呢? 比如要在ie的 creatpopup 方法 产生出来的popup文档内新建一个圆角div的话,由于popup只能加载他自己创建的控件,所以可以将popup对象传递到方法内部,成为方法上下文指向的对象。 传递上下文的方法有两种function.call(obj,arg1,arg2) 类似与这样。 另一种是 function.apply(obj,arguments)
详细代码如下:
复制代码 代码如下:
/**************************************************************************/
/*rounddiv.js 产生一个圆角div
调用前需设置函数上下文(上下文是指,要创建div的窗口) 例如 var objdiv = getrounddiv.call(document,,#dddddd)
函数参数argborderstyle: 边框样式,字符串 例如 1px solid black
函数参数argbgcolor: 背景颜色,字符串 例如 #ffffff
现在只支持边框为1像素 如果超过1像素产生的图形会比较奇怪
如果不设置边框 则没有边框 可以正常使用
本函数返回的是一个rounddiv自定义类
如果要设置div的内容请用 obj.content.innerhtml 或 obj.content.innertext设置
如果要设置div的高度请用 obj.div.style.width obj.div.style.height设置
*/
/**************************************************************************/
/**************************************************************************/
//取得一个圆角div
function getrounddiv(argborderstyle,argbgcolor){
//创建元素
var divpane =this.createelement(div)
var divcontent =this.createelement(div)
var divcontentmax =this.createelement(div)
var btop =this.createelement(b)
var bbottom =this.createelement(b)
var btop1 =this.createelement(b)
var btop2 =this.createelement(b)
var btop3 =this.createelement(b)
var btop4 =this.createelement(b)
var bbottom1 =this.createelement(b)
var bbottom2 =this.createelement(b)
var bbottom3 =this.createelement(b)
var bbottom4 =this.createelement(b)
//背景设置
divpane.style.backgroundcolor=argbgcolor;
divcontent.style.backgroundcolor=argbgcolor;
divcontentmax.style.backgroundcolor=argbgcolor;
btop1.style.backgroundcolor=argbgcolor;
btop2.style.backgroundcolor=argbgcolor;
btop3.style.backgroundcolor=argbgcolor;
btop4.style.backgroundcolor=argbgcolor;
bbottom1.style.backgroundcolor=argbgcolor;
bbottom2.style.backgroundcolor=argbgcolor;
bbottom3.style.backgroundcolor=argbgcolor;
bbottom4.style.backgroundcolor=argbgcolor;
btop.style.backgroundcolor=#ffffff;
bbottom.style.backgroundcolor=#ffffff;
//样式设置
btop.style.overflow=hidden;
bbottom.style.overflow=hidden;
btop1.style.overflow=hidden;
btop2.style.overflow=hidden;
btop3.style.overflow=hidden;
btop4.style.overflow=hidden;
bbottom1.style.overflow=hidden;
bbottom2.style.overflow=hidden;
bbottom3.style.overflow=hidden;
bbottom4.style.overflow=hidden;
btop.style.display=block;
bbottom.style.display=block;
btop1.style.display=block;
btop2.style.display=block;
btop3.style.display=block;
btop4.style.display=block;
bbottom1.style.display=block;
bbottom2.style.display=block;
bbottom3.style.display=block;
bbottom4.style.display=block;
//高度设置
divcontent.style.height=100%;
divcontentmax.style.height=100%;
btop1.style.height=1px;
btop2.style.height=1px;
btop3.style.height=1px;
btop4.style.height=2px;
bbottom1.style.height=1px;
bbottom2.style.height=1px;
bbottom3.style.height=1px;
bbottom4.style.height=2px;
//边框设置
divcontentmax.style.borderleft=argborderstyle
divcontentmax.style.borderright=argborderstyle
btop1.style.borderleft=argborderstyle;
btop1.style.borderright=argborderstyle;
btop1.style.bordertop=argborderstyle;
btop2.style.borderleft=argborderstyle;
btop2.style.borderright=argborderstyle;
btop3.style.borderleft=argborderstyle;
btop3.style.borderright=argborderstyle;
btop4.style.borderright=argborderstyle;
btop4.style.borderleft=argborderstyle;
bbottom1.style.borderleft=argborderstyle;
bbottom1.style.borderright=argborderstyle;
bbottom1.style.bordertop=argborderstyle;
bbottom2.style.borderleft=argborderstyle;
bbottom2.style.borderright=argborderstyle;
bbottom3.style.borderleft=argborderstyle;
bbottom3.style.borderright=argborderstyle;
bbottom4.style.borderleft=argborderstyle;
bbottom4.style.borderright=argborderstyle;
//空白间距设置
btop1.style.margin=0 4px 0 4px
btop2.style.margin=0 3px 0 3px
btop3.style.margin=0 2px 0 2px
btop4.style.margin=0 1px 0 1px
bbottom1.style.margin=0 4px 0 4px
bbottom2.style.margin=0 3px 0 3px
bbottom3.style.margin=0 2px 0 2px
bbottom4.style.margin=0 1px 0 1px
//控件拼装
btop.appendchild(btop1);
btop.appendchild(btop1);
btop.appendchild(btop2);
btop.appendchild(btop3);
btop.appendchild(btop4);
bbottom.appendchild(bbottom4);
bbottom.appendchild(bbottom3);
bbottom.appendchild(bbottom2);
bbottom.appendchild(bbottom1);
divcontentmax.appendchild(divcontent)
divpane.appendchild(btop)
divpane.appendchild(divcontentmax)
divpane.appendchild(bbottom)
var objrounddiv = new rounddiv();
objrounddiv.div=divpane;
objrounddiv.content=divcontent;
return objrounddiv;
}
/**************************************************************************/
/**************************************************************************/
//自定义类(用来装载div对应内容)
function rounddiv(){
this.content=0;//div内容
this.div=0;//div容器
}
/**************************************************************************/