当前位置: 首页 > 图文教程 > 网络编程 > Javascript > 基于mootools的圆角边框扩展代码

Javascript
jquery 交替为表格添加样式的代码
jquery下操作HTML控件的实现代码
JQuery获取元素文档大小、偏移和位置和滚动条位置的方法集合
海量经典的jQuery插件集合
JavaScript获取鼠标坐标的函数(兼容IE、FireFox、Chrome)
JavaScript关于select的相关操作说明
jQuery的一些特性和用法整理小结
用jQuery扩展自写的 UI导航
JQuery 引发两次$(document.ready)事件
javascript实现的基于金山词霸网络翻译的代码
Span元素的width属性无效果原因及解决方案
javascript 不间断的图片滚动并可点击
利用onresize使得div可以随着屏幕大小而自适应的代码
extjs 为某个事件设置拦截器
javascript 构建一个xmlhttp对象池合理创建和使用xmlhttp对象
javascript 特性检测并非浏览器检测
20个非常有用的PHP类库 加速php开发
前淘宝前端开发工程师阿当的PPT中有JS技术理念问题
AJAX的跨域与JSONP(为文章自动添加短址的功能)
JavaScript学习笔记(十)

Javascript 中的 基于mootools的圆角边框扩展代码


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-02-27   浏览: 57 ::
收藏到网摘: n/a

做圆角边框一般有两种方法,背景图片或者DIV+CSS拼出来。 JQuery下面有个扩展是用纯JS生成的圆角,不过和DIV+CSS拼出来是一样的道理,圆角看上去都比较粗糙。
用背景图片要好看得多,问题是不能拉伸,最简单做法就是用四个角小图片加边框拼出来。不过这样多出N多图片,一堆乱七八糟的代码,相当不爽。
有一个很有技巧的方法,用一张大图片+CSS来做,原理如下。

用一张大的背景图片做圆角,用CSS分别取四个角和边再拼成一个DIV。这样不仅可以解决圆角,还可以生成其它特殊的边框(比如阴影)。
但是每次使用都要加CSS也很不爽,于是用mootools写了一个Element类的扩展。
复制代码 代码如下:

setBorder
Element.implement({
setBorder: function(pic, len) {
/// <summary>
/// 设定容器边框(图片).
/// 已测div
/// </summary>
/// <param name="pic">图片地址</param>
/// <param name="len">边框宽度</param>
/// <returns type="Element" />
var content = this.clone();
var width = this.getSize().x + len * 2;
var height = this.getSize().y + len * 2;
this.empty().setStyles({ 'width': width, 'height': height });
var lt = new Element('div', {
'styles': {
'width': len,
'height': len,
'float': 'left',
'background': 'url(' + pic + ') no-repeat left top'
}
});
var rt = new Element('div', {
'styles': {
'width': width - len,
'height': len,
'float': 'left',
'background': 'url(' + pic + ') no-repeat right top'
}
});
var lb = new Element('div', {
'styles': {
'width': len,
'height': height - len,
'float': 'left',
'background': 'url(' + pic + ') no-repeat left bottom'
}
});
var rb = new Element('div', {
'styles': {
'width': width - len,
'height': height - len,
'float': 'left',
'background': 'url(' + pic + ') no-repeat right bottom'
}
});
content.inject(rb, 'top');
lt.inject(this, 'top');
rt.injectBottom(this);
lb.injectBottom(this);
rb.injectBottom(this);
return this;
}
});


这样在页面上直接调用setBorder方法传个背景图片,边框宽度进去就行了。
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>
<title>Untitled Page</title>
<script type="text/javascript" src="mootools.js"></script>
<script type="text/javascript">
Element.implement({
setBorder: function(pic, len) {
/// <summary>
/// 设定容器边框(图片).
/// 已测div
/// </summary>
/// <param name="pic">图片地址</param>
/// <param name="len">边框宽度</param>
/// <returns type="Element" />
var content = this.clone();
var width = this.getSize().x + len * 2;
var height = this.getSize().y + len * 2;
this.empty().setStyles({ 'width': width, 'height': height });
var lt = new Element('div', {
'styles': {
'width': len,
'height': len,
'float': 'left',
'background': 'url(' + pic + ') no-repeat left top'
}
});
var rt = new Element('div', {
'styles': {
'width': width - len,
'height': len,
'float': 'left',
'background': 'url(' + pic + ') no-repeat right top'
}
});
var lb = new Element('div', {
'styles': {
'width': len,
'height': height - len,
'float': 'left',
'background': 'url(' + pic + ') no-repeat left bottom'
}
});
var rb = new Element('div', {
'styles': {
'width': width - len,
'height': height - len,
'float': 'left',
'background': 'url(' + pic + ') no-repeat right bottom'
}
});
content.inject(rb, 'top');
lt.inject(this, 'top');
rt.injectBottom(this);
lb.injectBottom(this);
rb.injectBottom(this);
return this;
}
});
window.addEvent('domready', function() {
$('demo').getElements('div').each(function(d) {
d.setBorder('border.png', 8);
});
});
</script>
</head>
<body>
<div id="demo">
<div style="width:150px; height:100px;">
<div style="width:100%; height:100%; background-color:Red;"></div>
</div>
<div style="width:80px; height:130px;">
<div style="width:100%; height:100%; background-color:Green;"></div>
</div>
</div>
</body>
</html>


显显示效果
mootools边框demo http://demo.ruanchen.com/"http://sh.ruanchen.com/" style="color: #000">打包下载

[Ctrl+A 全选 提示:你可先修改部分代码,再按运行]

以前用Jquery也写过一个,居然找不着了,不过原理是一样的。