基于mootools的圓角邊框擴(kuò)展代碼
更新時(shí)間:2010年02月07日 11:10:20 作者:
做圓角邊框一般有兩種方法,背景圖片或者DIV+CSS拼出來。
JQuery下面有個(gè)擴(kuò)展是用純JS生成的圓角,不過和DIV+CSS拼出來是一樣的道理,圓角看上去都比較粗糙。
用背景圖片要好看得多,問題是不能拉伸,最簡單做法就是用四個(gè)角小圖片加邊框拼出來。不過這樣多出N多圖片,一堆亂七八糟的代碼,相當(dāng)不爽。
有一個(gè)很有技巧的方法,用一張大圖片+CSS來做,原理如下。
用一張大的背景圖片做圓角,用CSS分別取四個(gè)角和邊再拼成一個(gè)DIV。這樣不僅可以解決圓角,還可以生成其它特殊的邊框(比如陰影)。
但是每次使用都要加CSS也很不爽,于是用mootools寫了一個(gè)Element類的擴(kuò)展。
setBorder
Element.implement({
setBorder: function(pic, len) {
/// <summary>
/// 設(shè)定容器邊框(圖片).
/// 已測div
/// </summary>
/// <param name="pic">圖片地址</param>
/// <param name="len">邊框?qū)挾?lt;/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;
}
});
這樣在頁面上直接調(diào)用setBorder方法傳個(gè)背景圖片,邊框?qū)挾冗M(jìn)去就行了。
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>
/// 設(shè)定容器邊框(圖片).
/// 已測div
/// </summary>
/// <param name="pic">圖片地址</param>
/// <param name="len">邊框?qū)挾?lt;/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.jb51.net/js/mootools_yj/demo.htm
打包下載
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁面才能執(zhí)行]
以前用Jquery也寫過一個(gè),居然找不著了,不過原理是一樣的。
用背景圖片要好看得多,問題是不能拉伸,最簡單做法就是用四個(gè)角小圖片加邊框拼出來。不過這樣多出N多圖片,一堆亂七八糟的代碼,相當(dāng)不爽。
有一個(gè)很有技巧的方法,用一張大圖片+CSS來做,原理如下。

用一張大的背景圖片做圓角,用CSS分別取四個(gè)角和邊再拼成一個(gè)DIV。這樣不僅可以解決圓角,還可以生成其它特殊的邊框(比如陰影)。
但是每次使用都要加CSS也很不爽,于是用mootools寫了一個(gè)Element類的擴(kuò)展。
復(fù)制代碼 代碼如下:
setBorder
Element.implement({
setBorder: function(pic, len) {
/// <summary>
/// 設(shè)定容器邊框(圖片).
/// 已測div
/// </summary>
/// <param name="pic">圖片地址</param>
/// <param name="len">邊框?qū)挾?lt;/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;
}
});
這樣在頁面上直接調(diào)用setBorder方法傳個(gè)背景圖片,邊框?qū)挾冗M(jìn)去就行了。
HTML代碼
復(fù)制代碼 代碼如下:
<!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>
/// 設(shè)定容器邊框(圖片).
/// 已測div
/// </summary>
/// <param name="pic">圖片地址</param>
/// <param name="len">邊框?qū)挾?lt;/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.jb51.net/js/mootools_yj/demo.htm
打包下載
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁面才能執(zhí)行]
以前用Jquery也寫過一個(gè),居然找不著了,不過原理是一樣的。
相關(guān)文章
分享一個(gè)用Mootools寫的鼠標(biāo)滑過進(jìn)度條改變進(jìn)度值的實(shí)現(xiàn)代碼
分享一個(gè)用Mootools寫的鼠標(biāo)滑過進(jìn)度條改變進(jìn)度值的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-12-12Mootools 1.2教程 Fx.Morph、Fx選項(xiàng)和Fx事件
今天,我們繼續(xù)探索一下這個(gè)庫的Fx部分2009-09-09Mootools 1.2教程 選項(xiàng)卡效果(Tabs)
今天將不只是受限于這個(gè)庫和一些基本的編程知識,我們來做一個(gè)簡單的小項(xiàng)目。通過使用我們目前為止已經(jīng)學(xué)過的一些知識,有幾種方式來創(chuàng)建當(dāng)鼠標(biāo)移上去或者點(diǎn)擊時(shí)顯示相應(yīng)內(nèi)容的tab。2009-09-09使用Mootools動態(tài)添加Css樣式表代碼,兼容各瀏覽器
這個(gè)函數(shù)很有用處,尤其是當(dāng)我們在使用Mootools開發(fā)插件的時(shí)候,例如Tips等,當(dāng)我們需要額外的Css來支撐插件內(nèi)的效果時(shí),通常我們的做法是把Css放到單獨(dú)的css文件里調(diào)用2011-12-12