来源:http://www.cnblogs.com/binyong/archive/2009/12/11/1621484.html

下载地址:http://files.cnblogs.com/binyong/4.rar

bRoundCurve 1.0.js 代码

/*#############################################################
Name: b_RoundCurve Css圆角框组件--冰极峰
Version: 1.0
Author: biny
Email:szbiny@163.com
冰极峰博客地址:http://www.cnblogs.com/binyong
你可以免费使用和修改代码,但请保留完整的版权信息。

有如下五种调用方法:
b_RoundCurve("bottom","#F8B3D0","#FFF5FA",1);//普通圆角框
b_RoundCurve("left1","#AE0474","#FB7D3F",3,"h3","","image/bg5.gif");//标题用背景图片
b_RoundCurve("right1","#863313","#84D4CA",3,"h3","#BAB556");//标题只用纯色背景
b_RoundCurve("right2","orange","",3,"h3","");//标题不带背景色
b_RoundCurve("top","#4C7C9B","",4);//圆角背景图片
b_RoundCurve("img","#999","#FFF5FA",2);//圆角IMG图片
#################################################################*/

/*
圆角函数--Author: biny
传递7个参数
1.class类名
2.边框色
3.主体内容区背景色
4.风格切换方式,从1-5,5种圆角框样式,针对不同环境使用。
5.标题的html结构标签名
6.标题背景色
7.标题背景图片路径(和6是相斥的,两个选择一个)
*/
function b_RoundCurve(classname,b_c,bg_c,state,tagname,titlebg,titleimg){
var divs=getElementsByClassName(classname);
for(var i=0;i<divs.length;i++){
var obj=divs[i];
var path=window.location.href;//当前页面的路径
path=path.substring(0,path.lastIndexOf('/')+1);
//b标签的通用样式
var comstyle="height:1px; font-size:1px;overflow:hidden; display:block;";
//b标签的结构样式
var b1="margin:0 5px;";//和b8相同
var b2="margin:0 3px;border-right:2px solid; border-left:2px solid;";//和b7相同
var b3="margin:0 2px;border-right:1px solid; border-left:1px solid;";//和b6相同
var b4="margin:0 1px;border-right:1px solid; border-left:1px solid;height:2px;";//和b5相同
var content="border-right:1px solid;border-left:1px solid;overflow:hidden;position:relative;";
var bgColor="background:"+bg_c+";";//背景色

//img图片的背景定位
var imgPos2="background-position:-4px top;";
var imgPos3="background-position:-2px -1px;";
var imgPos4="background-position:-1px -2px;";
var conPos="background-position:left -4px;";
var imgPos5="background-position:-1px bottom;";
var imgPos6="background-position:-2px bottom;";
var imgPos7="background-position:-4px bottom;";

//定义一些变量,这些变量包含不同的样式,在各种风格中拼合组装
var imgBgStr,imgPos3,imgPos4,conPos,imgPos5,imgPos6,imgPos7;
var b_img2,b_img3,b_img4,b_img5,b_img6,b_img7,c_img,imgurl;

//五种不同的风格切换
if(state==1){//最通常使用的线框
//组装样式
b_img2=bgColor;
b_img3=bgColor;
b_img4=bgColor;
c_img=bgColor;
b_img5=bgColor;
b_img6=bgColor;
b_img7=bgColor;
}
if(state==2){//如果是img图片方式,则用背景模拟圆角,注意IMG标签必须显式指定宽高,否则在safari中显示不出来。
var imgObj=divs[i].getElementsByTagName('img')[0];
var imgheight=imgObj.height;//图片高度
var contentheight=imgheight-10;//中间图片的高度
var imgweight=imgObj.width;//图片宽度
obj.style.width=(imgweight+2)+"px";
var imgsrc=imgObj.src.replace(path,'');//图片文件的相当路径
var imgBgStr="background:url("+imgsrc+") no-repeat;";
//组装样式
conPos="height:"+contentheight+"px;width:"+imgweight+"px;overflow:hidden;";

b_img2=imgBgStr+imgPos2;
b_img3=imgBgStr+imgPos3;
b_img4=imgBgStr+imgPos4;
c_img=conPos;
b_img5=imgBgStr+"background-position:-1px -"+ (imgheight-4)+"px;";
b_img6=imgBgStr+"background-position:-2px -"+ (imgheight-2)+"px;";
b_img7=imgBgStr+"background-position:-4px -"+ (imgheight-1)+"px;";
imgurl=imgsrc;
}
if(state==3){//如果是带标题方式,又可分为两种情况,一种直接用背景色,一种是用背景图片方式
var objh3=obj.getElementsByTagName(tagname)[0];
if(titleimg!=null){//标题带水平平铺的背景图片
var bgimg="background:url("+titleimg+") repeat-x;";
b_img2=bgimg+imgPos2;
b_img3=bgimg+imgPos3;
b_img4=bgimg+imgPos4;
//标题栏样式
objh3.style.background="url("+titleimg+") repeat-x left -4px";
objh3.style.borderBottomColor=b_c;
}
else{//标题不带水平平铺的背景图片
var bg_c="background:"+titlebg+";";//背景色
b_img2=bg_c;
b_img3=bg_c;
b_img4=bg_c;
//标题栏样式
objh3.style.background=titlebg;
objh3.style.borderBottomColor=b_c;
}
//组装样式
c_img=bgColor;
b_img5=bgColor;
b_img6=bgColor;
b_img7=bgColor;

}
if(state==4){//如果是背景图片方式,则。。。
//先从样式表中获取背景图片的样式,要求加入图片的容器明确定义宽度和高度,和背景图片,这是用在JS用来搜寻的依据。
var bgimg=getStyle(obj,"backgroundImage");
var bgWidth=getStyle(obj,"width");
bgimg=bgimg.replace(path,"");
bgimg=bgimg.substring(4,bgimg.length);
bgimg=bgimg.substring(0,bgimg.length-1);
var bgimgheight=getStyle(obj,"height");//图片的高度

bgimgheight=bgimgheight.replace("px","");
var contentheight=bgimgheight-10;//中间图片的高度,包含上下边框2px宽度
bgWidth=bgWidth.replace("px","");
bgWidth=bgWidth-2;

bgimg=bgimg.replace("url(\"","");
bgimg=bgimg.replace("\")","");//获得背景图片的全部径。
path=path.substring(0,(path.lastIndexOf('/')+1));//页面地址
bgimg=bgimg.replace(path,"");
imgBgStr="background:url("+bgimg+") no-repeat;";
obj.style.background="none";//将原始的背景图片隐藏
//组装样式
b_img2=imgBgStr+imgPos2;
b_img3=imgBgStr+imgPos3;
b_img4=imgBgStr+imgPos4;
c_img=imgBgStr+conPos+"height:"+contentheight+"px;width:"+bgWidth+"px;";

//关键代码,特别是对图片的定位,需要知道图片的高度。
b_img5=imgBgStr+"background-position:-1px -"+ (bgimgheight-4)+"px;";
b_img6=imgBgStr+"background-position:-2px -"+ (bgimgheight-2)+"px;";
b_img7=imgBgStr+"background-position:-4px -"+ (bgimgheight-1)+"px;";
}
if(state==2 || state==4){
conDivHeight="";
}
else{
var H=getStyle(obj,"height");//获到容器的高度
H=H.replace("px","");//去掉单位
conDivHeight="height:"+(H-8)+"px";//容器的高度伪装成css中的设置的一样
}
/*创建一个容器结构体*/
var rDivStr="<b style='"+ comstyle+b1+"background:"+b_c+"'></b>";
rDivStr+="<b style='"+ comstyle+b2+"border-color:"+b_c+";"+b_img2+"'></b>";
rDivStr+="<b style='"+ comstyle+b3+"border-color:"+b_c+";"+b_img3+"'></b>";
rDivStr+="<b style='"+ comstyle+b4+"border-color:"+b_c+";"+b_img4+"'></b>";
rDivStr+="<div style='"+content+"border-color:"+b_c+";"+c_img+conDivHeight+"'>";
rDivStr+="@d_P";
rDivStr+="</div>";
rDivStr+="<b style='"+ comstyle+b4+"border-color:"+b_c+";"+b_img5+"'></b>";
rDivStr+="<b style='"+ comstyle+b3+"border-color:"+b_c+";"+b_img6+"'></b>";
rDivStr+="<b style='"+ comstyle+b2+"border-color:"+b_c+";"+b_img7+"'></b>";
rDivStr+="<b style='"+ comstyle+"margin:0 5px;background:"+b_c+"'></b>";

var htmlText=divs[i].innerHTML;
if(state==2){
var str1=htmlText.replace("src=\"","src=\"*");
var strsplit1=str1.split('*')[0];
var strsplit2=str1.split('*')[1];
var url=strsplit2.substring(0,strsplit2.indexOf('\"'));
htmlText=strsplit1+imgurl+"\" style='border:0px;position:absolute;top:-4px;left:0px;'/>";
}
rDivStr=rDivStr.replace('@d_P',htmlText);
divs[i].innerHTML=rDivStr;//替换结构
}
}

// 说明:准确获取指定元素 CSS 属性值
// 此函数的两个参数,elem为要获取的对象,name为样式属性,如“backgroundColor”
function getStyle( elem, name )
{
//如果该属性存在于style[]中,则它最近被设置过(且就是当前的)
if (elem.style[name])
{
return elem.style[name];
}
//否则,尝试IE的方式
else if (elem.currentStyle)
{
return elem.currentStyle[name];
}
//或者W3C的方法,如果存在的话
else if (document.defaultView && document.defaultView.getComputedStyle)
{
name = name.replace(/([A-Z])/g,"-$1");
name = name.toLowerCase();
//获取style对象并取得属性的值(如果存在的话)
var s = document.defaultView.getComputedStyle(elem,"");
return s && s.getPropertyValue(name);
//否则,就是在使用其它的浏览器
}
else
{
return null;
}
}

/*根据类名获得对象
调用方法:var topicnum=getElementsByClassName("classname");
*/
function getElementsByClassName(searchClass, node,tag){
if(document.getElementsByClassName){return document.getElementsByClassName(searchClass)}
else{
node = node || document;
tag = tag || "*";
var classes = searchClass.split(" "),
elements = (tag === "*" && node.all)? node.all : node.getElementsByTagName(tag),
patterns = [],
returnElements = [],
current,
match;
var i = classes.length;
while(--i >= 0){patterns.push(new RegExp("(^|\\s)" + classes[i] + "(\\s|$)"));}
var j = elements.length;
while(--j >= 0){
current = elements[j];
match = false;
for(var k=0, kl=patterns.length; k<kl; k++){
match = patterns[k].test(current.className);
if (!match) break;
}
if (match) returnElements.push(current);
}
return returnElements;
}
}

示例代码:demo.html

<!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=gb2312" />
<title>冰极峰原创作品--css圆角框组件 v1.0</title>
<style type="text/css">
/****************************************
CSS圆角框组件 v1.0
冰极峰原创
更多内容请访问:http://binyong.cnblogs.com/
*****************************************/
* {margin:0;padding:0;}
body {background:url(image/bg.gif);}
a:link,a:visited{ text-decoration:none;color:#fff;}
a:hover{color:orange;border-bottom:1px orange solid;}
h3 {height:26px;line-height:26px;font-size:12px;border-bottom:1px #E3E197 solid;padding:0 10px;color:#fff;}
h4 {font-size:12px;text-indent:20px;border-bottom:none;}
p {line-height:22px;text-indent:10px;padding:0 10px;}
.top{width:490px;height:80px;margin:10px auto;overflow:hidden;background:url(image/topbg.gif) no-repeat right bottom;text-align:center;font-size:12px;font-weight:bold;}
.top p{color:#fff;font-weight:bold;line-height:24px;}
h1 {font-size:20px;height:20px;color:#fff;}
.wrapper {width:490px;margin:0px auto;font-size:14px;overflow:hidden;margin-bottom:10px;}
.left {float:left;width:70%;}
.left1 {font-size:12px;}
.img {float:left;display:inline;margin:10px 0 0px 10px;}
.left2 {font-size:12px;overflow:hidden;margin-top:10px;}
.right {float:right;width:28%;}
.right1 {margin-bottom:10px;font-size:12px;}
.right2 {margin-bottom:10px;font-size:12px;color:#fff;}
.right3 {margin-bottom:10px;font-size:12px;}
.rightbgimg {background:url(image/img2.jpg) no-repeat right bottom;width:138px;height:104px;text-align:center;font-size:12px;color:#ffffff;}
.bottom {clear:both;width:490px;margin:0px auto;text-align:center;font-size:12px;font-weight:bold;height:50px;line-height:50px;}
.bottom a:link,.bottom a:visited{color:#000;}
.bottom a:hover{color:orange;border-bottom:1px orange solid;}
.hasH{height:100%;overflow:hidden;padding-bottom:8px;}/*for ie6*/
</style>
</head>
<body>
<div class="top">
<p>冰极峰原创作品</p>
<h1>css圆角框组件 v1.0</h1>
<p>博客地址:<a href="http://binyong.cnblogs.com" title="冰极峰博客">http://binyong.cnblogs.com</a></p>
</div>
<div class="wrapper">
<div class="left">
<div class="left1">
<h3>标题带有背景图片</h3>
<div class="hasH">
<p><b>组件优点:</b></p>
<p>1. 全面兼容所有浏览器</p>
<p>2. 圆角不需要图片,直接代码生成,省去制图的麻烦。</p>
<p>3. 自适应外框的大小,可广泛应用于布局区块中。</p>
<p>4. 封装难以控制的CSS代码,调用灵活方便。</p>
<p>5. 封装HTML结构,你只需定义你想要的结构,无冗余,更语义化。</p>
<p><b>组件缺点:</b></p>
<p>1. 不能定义线框的大小。如果你需要改变线框大小,本组件不适合你。</p>
<p>2. 圆角不够圆滑,如果你对圆角图片的精度要求较高,不宜采用本组件。</p>
<p>3. 线框颜色和背景色不宜采用对比太强烈的颜色,容易看出锯齿。</p>
</div>
</div>
<div class="left2">
<h3>下面圆角图片是img图片,而非背景图片。</h3>
<div class="hasH">
<div class="img"><a href="http://binyong.cnblogs.com/" title="漂亮女孩1" target="_blank"><img src="data:image/girl-1.jpg" height="115" width="154" alt="漂亮女孩1"/></a></div>
<div class="img"><a href="http://binyong.cnblogs.com/" title="漂亮女孩2" target="_blank"><img src="data:image/girl-2.jpg" height="115" width="154" alt="漂亮女孩2"/></a></div>
</div>
</div>
</div>

<div class="right">
<div class="right1">
<h3>标题带背景色</h3>
<div class="hasH">
<p>标题栏的背景色与内容区背景可以定义不同的颜色。这些颜色值都可以直接在CSS中定义。</p>
</div>
</div>
<div class="right3">
<h3>标题带有背景图片</h3>
<div class="hasH">
<p>标题栏也可以在样式表中定义背景图片,这张背景图片自动圆角化。</p>
</div>
</div>
<div class="right2">
<h3>标题栏透明</h3>
<div class="hasH">
<p>你可以只要线框,不加任何颜色和背景图片。</p>
</div>
</div>
<div class="rightbgimg">
<p><br/>装饰性背景图片<br/>
也可以圆角化</p>
</div>
</div>
</div>
<div class="bottom"><a href="http://binyong.cnblogs.com" title="更多原创">冰极峰</a> 版权所有</div>
<script type="text/javascript" src="js/bRoundCurve 1.0.js"></script>
<script type="text/javascript">
b_RoundCurve("bottom","#F8B3D0","#FFF5FA",1);//普通圆角框
b_RoundCurve("left2","#E0750F","#FFF2A5",3,"h3","","image/bg3.gif");//标题用背景图片
b_RoundCurve("left1","#AE0474","#FB7D3F",3,"h3","","image/bg2.gif");//标题用背景图片
b_RoundCurve("right1","#863313","#84D4CA",3,"h3","#BAB556");//标题只用纯色背景
b_RoundCurve("right2","orange","",3,"h3","");//标题不带背景色
b_RoundCurve("right3","#A0C044","#E9F2CC",3,"h3","","image/bg1.gif");//标题用背景图片
b_RoundCurve("rightbgimg","#F38E1A","",4);//圆角背景图片
b_RoundCurve("top","#4E271A","",4);//圆角背景图片
b_RoundCurve("img","#999","#FFF5FA",2);//圆角IMG图片
</script>
</body>
</html>

转载于:https://www.cnblogs.com/hcbin/archive/2010/03/19/1690215.html

[JS,CSS] - CSS圆角框组件相关推荐

  1. 用CSS制作圆角框的方法一,二

    CSS产生之初,只能制作直角边框,虽然可以制作圆角的CSS3已经问世,但还不普及.曲线设计在网页中的广泛应用,使圆角成为了时髦的CSS技术之一.创建圆角的方法有很多种,每种要根据实际情况应用.下面我 ...

  2. 用 :focus-within 实现纯 CSS 下拉框组件

    :focus-within 伪类:当本节点或其子节点获得焦点时被激活. 借用 MDN 的例子 https://jsfiddle.net/d4w8h2ge/ 当点击表单中的文本框时需高亮整个表单元素.但 ...

  3. 7种CSS圆角框解决方案

    7种CSS圆角框解决方案时间: 2009-03-11 / 分类: 前端开发 / 浏览次数: 14,913 次 / 7个评论 发表评论 本文探讨的是前端设计种最常用的页面圆角框效果的7种不同方法实现解决 ...

  4. 纯CSS实现圆角矩形框

    为什么80%的码农都做不了架构师?>>>    实现原理: 纯CSS方式实现圆角框的原理在网络上已经有很多人详细解说了,下面这个示意图是我将其中的一个圆角进行放大后的效果. 图一 从 ...

  5. JS配合css实现slide文字框缩放伸展效果

    <!DOCTYPE html> <html> <head> <meta http-equiv="content" content=&quo ...

  6. html css好看的提示框,div对话框,js+div+css实现好看的提示框效果(转)

    div对话框,js+div+css实现好看的提示框效果(转) (2012-02-18 20:46:23) 标签: html div css 杂谈 提示窗都越来越人性化了,呵呵,有的时候老板就和你要那么 ...

  7. html复选框全选怎么实现,js html css实现复选框全选与反选

    本文实例为大家分享了js复选框全选与反选实现代码,供大家参考,具体内容如下 html+css+js实现复选框全选与反选 table,tr,td { border:1px solid red; } fu ...

  8. html设置文本框只读属性,juqery/js/css设置文本框只读属性的方法

    一.设置HTML表单文本框为只读的几种方式 有时候,我们希望表单中的文本框是只读的,让用户只能查看信息而不能修改其中的信息,使 input type="text" name=&qu ...

  9. html中正方形圆角框,CSS高级技巧:圆角矩形

    所谓的CSS高级技巧...其实是一些对基本技巧的创意组合, 任何手段和技巧都是解决问题的方法. 只要学会这种思考问题的方法, 你也能拥有属于自己的CSS秘籍. CSS2 还没有办法创造出曲线边框, 明 ...

最新文章

  1. PHP入门指导:如何学习PHP?
  2. oracle里面查询重复数据的方法
  3. 谈SOA在国内推进状况
  4. 用GDB调试程序(11)──查看运行时数据(2)-转
  5. python工资一般多少-Python就业都有哪些岗位?每个岗位薪资多少?
  6. PHP 中的对象传递
  7. linux如何实现c语言程序,在Linux下如何利用C语言来实现一个Sniffer
  8. fn键台式计算机在哪,fn键在哪里
  9. 数据新闻大趋势 释放可视化报道的力量------读后随笔
  10. 公用电信网间互联管理规定
  11. 如何开发手游联运平台?
  12. win10升级后蓝牙不见了,设备管理器里没有,多了个串行控制器里的未知USB设备?...
  13. 零基础怎么做好海报重叠设计
  14. SQL Server基础——T-SQL语句
  15. CentOS 6 安装配置教程【完整版】
  16. 3DsMax安装教程
  17. 【手绘漫画】图解LeetCode之两两交换链表中的节点(LeetCode 24)
  18. 一、二、三代测序原理
  19. openoffice java awt_OpenOffice开发者指南笔记
  20. get,post区别

热门文章

  1. 回归 统计绘图_手把手教绘制回归分析结果的森林图「GraphPad Prism和Excel」
  2. 蓝凌ekp开发_蓝凌EKP8.0产品白皮书(简).doc
  3. python搭积木_从零实现”搭积木式实现策略“的回测系统 part VI
  4. 力扣(LeetCode)刷题,简单题(第19期)
  5. 基于pytorch后量化(mnist分类)---浮点训练vs多bit后量化vs多bit量化感知训练效果对比
  6. 【lidar】基于YOLO的3D目标检测(激光雷达点云)课程设计
  7. php字符串数组访问,php – 按字符串键x访问数组,其中x是“123”
  8. 事物的级别_浅谈MySQL并发控制:隔离级别、锁与MVCC
  9. apache安装_Apache+PHP 安装 ---windows
  10. ATS 5.3.0命令行工具traffic_via