Javascript function _ javascript skills that can achieve element rounded corners with simplified code
Last Update:2017-05-11
Source: Internet
Author: User
The JavaScript function that can implement element rounded corners with simplified code saw an implementation method for element rounded corners on blueidea last time, but that is too complicated. So I wrote a function to automatically round the elements.
Demo address: http://longbill.cn/down/sample/roundcorner.htm
Do not use the element with a padding value. It is best to set a layer on the outside. For details, see the demo address.
Code:
Function RoundCorner (obj, style)
{
/********
Page element rounded corner Function !!
By Longbill
Home: www.longbill.cn
********/
Var r = [];
Var styles = [
{Top: ["0 5px", "0 3px", "0 2px", "0 1px", "0 1px"], bottom: ["0 1px ", "0 1px", "0 2px", "0 3px", "0 5px"]},
{Top: ["0 5px", "0 3px", "0 2px", "0 1px", "0 1px"], bottom: ["0px ", "0px", "0px", "0px", "0px"]},
{Top: ["0 0 0 5px", "0 0 0 3px", "0 0 0 2px", "0 0 0 1px", "0 0 0 1px"], bottom: ["0 1 0 0px", "0 1 0 0px", "0 2 0 0px", "0 3 0 0px", "0 5 0 0px"]},
{Top: ["0 5 0 0px", "0 3 0 0px", "0 2 0 0px", "0 1 0 0px", "0 1 0 0px"], bottom: ["0 0 0 1px", "0 0 0 1px", "0 0 0 2px", "0 0 0 3px", "0 0 0 5px"]}
]; // Author: longbill.cn
If (! Style | style> styles. length) style = 1;
Style --;
Var btop = styles [style]. top, bbottom = styles [style]. bottom;
Obj = document. getElementById (obj );
If (! Obj) return;
Var HTML = obj. innerHTML;
Obj. innerHTML = "";
For (var istop = 1; istop> = 0; istop --)
{
Var topborder = document. createElement ("B ");
Topborder. style. display = "block ";
Topborder. style. height = "2px ";
Topborder. style. backgroundColor = (obj. parentNode. style. backgroundColor )? Obj. parentNode. style. backgroundColor: "# FFFFFF ";
For (var I = 0; I {
Var B = document. createElement ("B ");
If (obj. style. backgroundColor)
B. style. backgroundColor = obj. style. backgroundColor;
Else if (obj. className)
B. className = obj. className;
B. style. display = "block ";
B. style. margin = (istop )? Btop [I]: bbottom [I];
B. style. height = "1px ";
B. style. overflow = "hidden ";
B. style. width = "auto ";
Topborder. appendChild (B );
}
Obj. appendChild (topborder );
If (istop) obj. innerHTML + = HTML;
}
}