Seulementzh00 2010-02-07
代码如下:
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; } });
代码如下:
<!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>