Forum Moderators: open
I've recently found a very cool tutorial that taught me how to create rounded corners for my DIV's using some JavaScript. The script is called ShadedBorders and I was very excited at first to use it, until I realized the JavaScript could only apply the Rounded Corners to ONE element of my page with the class = bordered.
However, there where 4 other DIV elements on my page that had the class = bordered, but the JavaScript simply ignored them and didn't apply the effect I was expecting.
This is very weird, do you guys know what might have caused this and how I could fix it? If you want I can post the JavaScript code here if you need it.
Thank you in advance. Any help is highly appreciated.
The source JavaScript is this one:
var RUZEE = window.RUZEE ¦¦ {};RUZEE.ShadedBorder = {
create: function(opts) {
var isie = /msie/i.test(navigator.userAgent) &&!window.opera;
var isie6 = isie &&!window.XMLHttpRequest;
function sty(el, h) {
for(k in h) {
if (/ie_/.test(k)) {
if (isie) el.style[k.substr(3)]=h[k];
} else el.style[k]=h[k];
}
}
function crdiv(h) {
var el=document.createElement("div");
el.className = "sb-gen";
sty(el, h);
return el;
}
function op(v) {
v = v<0? 0 : v;
if (v>0.99999) return "";
return isie? " filter:alpha(opacity=" + (v*100) + ");" : " opacity:" + v + ';';
}
var sr = opts.shadow ¦¦ 0;
var r = opts.corner ¦¦ 0;
var bor = 0;
var bow = opts.border ¦¦ 0;
var boo = opts.borderOpacity ¦¦ 1;
var shadow = sr!= 0;
var lw = r > sr? r : sr;
var rw = lw;
var th = lw;
var bh = lw;
if (bow > 0) {
bor = r;
r = r - bow;
}
var cx = r!= 0 && shadow? Math.round(lw/3) : 0;
var cy = cx;
var cs = Math.round(cx/2);
var iclass = r > 0? "sb-inner" : "sb-shadow";
var sclass = "sb-shadow";
var bclass = "sb-border";
var edges = opts.edges ¦¦ "trlb";
if (!/t/i.test(edges)) th=0;
if (!/b/i.test(edges)) bh=0;
if (!/l/i.test(edges)) lw=0;
if (!/r/i.test(edges)) rw=0;
var p = { position:"absolute", left:"0", top:"0", width:lw + "px", height:th + "px",
ie_fontSize:"1px", overflow:"hidden", margin:"0", padding:"0" }; var tl = crdiv(p);
delete p.left; p.right="0"; p.width=rw + "px"; var tr = crdiv(p);
delete p.top; p.bottom="0"; p.height=bh + "px"; var br = crdiv(p);
delete p.right; p.left="0"; p.width=lw + "px"; var bl = crdiv(p);
var tw = crdiv({ position:"absolute", width:"100%", height:th + "px", ie_fontSize:"1px",
top:"0", left:"0", overflow:"hidden", margin:"0", padding:"0" });
var t = crdiv({ position:"relative", height:th + "px", ie_fontSize:"1px",
margin:"0 "+ rw + "px 0 " + lw + "px", overflow:"hidden", padding:"0" });
tw.appendChild(t);
var bw = crdiv({ position:"absolute", left:"0", bottom:"0", width:"100%", height:bh + "px",
ie_fontSize:"1px", overflow:"hidden", margin:"0", padding:"0" });
var b = crdiv({ position:"relative", height:bh + "px", ie_fontSize:"1px",
margin:"0 "+ rw + "px 0 " + lw + "px", overflow:"hidden", padding:"0" });
bw.appendChild(b);
var mw = crdiv({ position:"absolute", top:(-bh)+"px", left:"0", width:"100%", height:"100%",
overflow:"hidden", ie_fontSize:"1px", padding:"0", margin:"0" });
function corner(el,t,l) {
var w = l? lw : rw;
var h = t? th : bh;
var s = t? cs : -cs;
var dsb = []; var dsi = []; var dss = [];
var xp=0; var xd=1; if (l) { xp=w-1; xd=-1; }
for (var x=0; x<w; ++x) {
var yp=h-1; var yd=-1; if (t) { yp=0; yd=1; }
var finished=false;
for (var y=h-1; y>=0 &&!finished; --y) {
var div = '<div style="position:absolute; top:' + yp + 'px; left:' + xp + 'px; ' +
'width:1px; height:1px; overflow:hidden; margin:0; padding:0;';
var xc = x - cx; var yc = y - cy - s;
var d = Math.sqrt(xc*xc+yc*yc);
var doShadow = false;
if (r > 0) {
// draw border
if (xc < 0 && yc < bor && yc >= r ¦¦ yc < 0 && xc < bor && xc >= r) {
dsb.push(div + op(boo) + '" class="' + bclass + '"></div>');
} else
if (d<bor && d>=r-1 && xc>=0 && yc>=0) {
var dd = div;
if (d>=bor-1) {
dd += op((bor-d)*boo);
doShadow = true;
} else dd += op(boo);
dsb.push(dd + '" class="' + bclass + '"></div>');
}
// draw inner
var dd = div + ' z-index:2;' + (t? 'background-position:0 -' + (r-yc-1) + 'px;' : 'background-image:none;');
var finish = function() {
if (!t) dd = dd.replace(/top\:\d+px/, "top:0px");
dd = dd.replace(/height\:1px/, "height:" + (y+1) + "px");
dsi.push(dd + '" class="' + iclass + '"></div>');
finished = true;
};
if (xc < 0 && yc < r ¦¦ yc < 0 && xc < r) {
finish();
} else
if (d<r && xc>=0 && yc>=0) {
if (d>=r-1) {
dd += op(r-d);
doShadow = true;
dsi.push(dd + '" class="' + iclass + '"></div>');
} else {
finish();
}
} else doShadow = true;
} else doShadow = true;
// draw shadow
if (sr > 0 && doShadow) {
d = Math.sqrt(x*x+y*y);
if (d<sr) {
dss.push(div + ' z-index:0; ' + op(1-(d/sr)) + '" class="' + sclass + '"></div>');
}
}
yp += yd;
}
xp += xd;
}
el.innerHTML = dss.concat(dsb.concat(dsi)).join('');
}
function mid(mw) {
var ds = [];
ds.push('<div style="position:relative; top:' + (th+bh) + 'px; height:2048px; ' +
' margin:0 ' + (rw-r-cx) + 'px 0 ' + (lw-r-cx) + 'px; ' +
' padding:0; overflow:hidden;' +
' background-position:0 ' + (th > 0? -(r+cy+cs) : '0') + 'px;"' +
' class="' + iclass + '"></div>');
var dd = '<div style="position:absolute; width:1px;' +
' top:' + (th+bh) + 'px; height:2048px; padding:0; margin:0;';
if (sr>0) {
for (var x=0; x<lw-r-cx; ++x) {
ds.push(dd + ' left:' + x + 'px;' + op((x+1.0)/lw) +
'" class="' + sclass + '"></div>');
}
for (var x=0; x<rw-r-cx; ++x) {
ds.push(dd + ' right:' + x + 'px;' + op((x+1.0)/rw) +
'" class="' + sclass + '"></div>');
}
}
if (bow > 0) {
var su = ' width:' + bow + 'px;' + op(boo) + '" class="' + bclass + '"></div>';
ds.push(dd + ' left:' + (lw-bor-cx) + 'px;' + su);
ds.push(dd + ' right:' + (rw-bor-cx) + 'px;' + su);
}
mw.innerHTML = ds.join('');
}
function tb(el, t) {
var ds = [];
var h = t? th : bh;
var dd = '<div style="height:1px; overflow:hidden; position:absolute; margin:0; padding:0;' +
' width:100%; left:0px; ';
var s = t? cs : -cs;
for (var y=0; y<h-s-cy-r; ++y) {
if (sr>0) ds.push(dd + (t? 'top:' : 'bottom:') + y + 'px;' + op((y+1)*1.0/h) +
'" class="' + sclass + '"></div>');
}
if (y >= bow) {
ds.push(dd + (t? 'top:' : 'bottom:') + (y - bow) + 'px;' + op(boo) +
' height:' + bow + 'px;" class="' + bclass + '"></div>');
}
ds.push(dd + (t? 'background-position-y:0; top:' :
'background-image:none; bottom:') + y + 'px;' +
' height:' + (r+cy+s) + 'px;" class="' + iclass + '"></div>');
el.innerHTML = ds.join('');
}
corner(tl, true, true); corner(tr, true, false);
corner(bl, false, true); corner(br, false, false);
mid(mw); tb(t, true); tb(b, false);
return {
render: function(el) {
if (typeof el == 'string') el = document.getElementById(el);
if (el.length!= undefined) {
for (var i=0; i<el.length; ++i) this.render(el[i]);
return;
}
el.className += " sb";
sty(el, { position:"relative", background:"transparent" });
// remove generated children
var node = el.firstChild;
while (node) {
var nextNode = node.nextSibling;
if (node.nodeType == 1 && node.className == 'sb-gen')
el.removeChild(node);
node = nextNode;
}
var iel = el.firstChild;
var twc = tw.cloneNode(true);
var mwc = mw.cloneNode(true);
var bwc = bw.cloneNode(true);
el.insertBefore(tl.cloneNode(true), iel); el.insertBefore(tr.cloneNode(true), iel);
el.insertBefore(bl.cloneNode(true), iel); el.insertBefore(br.cloneNode(true), iel);
el.insertBefore(twc, iel); el.insertBefore(mwc, iel);
el.insertBefore(bwc, iel);
if (isie6) {
el.onmouseover=function() { this.className+=" hover"; }
el.onmouseout=function() { this.className=this.className.replace(/ hover/,""); }
}
if (isie) {
function resize() {
twc.style.width = bwc.style.width = mwc.style.width = el.offsetWidth + "px";
mwc.firstChild.style.height = el.offsetHeight + "px";
}
el.onresize=resize;
resize();
}
}
};
}
}
// add our styles to the document
document.write('\
<style type="text/css">\
.sb, .sbi, .sb *, .sbi * { position:relative; z-index:1; }\
* html .sb, * html .sbi { height:1%; }\
.sbi { display:inline-block; }\
.sb-inner { background:#ddd; }\
.sb-shadow { background:#000; }\
.sb-border { background:#bbb; }\
</style>\
');
[edited by: jatar_k at 2:37 pm (utc) on Dec. 28, 2007]
[edit reason] no urls thanks [/edit]
function getElementsByClass(cName) {
var mEls = [];
var uEls = document.getElementsByTagName("*");
var l = uEls.length;
var pattern = new RegExp("(^¦\\\\s)"+cName+"(\\\\s¦$)");
for (var i = 0; i < l; i++) {
if ( pattern.test(uEls[i].className) ) {
mEls.push(uEls[i]);
}
}
return mEls;
}
var border = RUZEE.ShadedBorder.create({/*your options*/});
border.render(getElementsByClass('bordered'));
Thanks a lot for your help, but I've tried using your code and it didn't work. I have 4 DIVs with the class = bordered and only the first DIV has the Shaded Border effect, the remaining are not changed.
I chaged from "var border" to "shadowedBorder" in order to apply the necessary parameters to my DIV and give it rounded corners. Please note that the JS code I posted before is being called here on my code as an external file called shadedborder.js
Here is the code I used to test 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></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="shadedborder.js"></script>
<style type="text/css">
/* <![CDATA[ */
html { font-family:Arial, sans-serif; background:#444; color:#ddd; }
html, body { margin:0; padding:0; }
p { margin:5px 0; }
.headfoot { text-align:right; padding:10px; margin:0; position:relative; }
.headfoot h1 { font-size:1.3em; padding:0; margin:0; }
.headfoot a { color:#fff; }
#content { background:url(back.jpg); padding:10px 0; }
* html #content { posititon:relative; }
/* For the first shadowed border */
#shadowed-border { padding:20px; width:80%; margin:20px auto; color:#fff; }
#shadowed-border, #shadowed-border .sb-inner { background:#444 url(grad.png) repeat-x; }
/* The split heading/content element */
#split { margin:20px auto; width:33%; }
#split h3 { padding:4px; text-align:center; margin:0; color:#fff; }
#split h3, #split h3 .sb-inner { background:#222 url(grad2.png) repeat-x; }
#split .content { padding:4px 10px; color:#444; }
#split .content, #split .content .sb-inner { background:#ddd url(grad3.png) repeat-x; }
#trans-border { width:280px; margin:20px auto; padding:14px 20px; background:#444; color:#222; }
#trans-border .sb-border { background:#000; }
/* Partial stuff */
ul#partial { overflow:hidden; width:490px; margin:20px auto; padding:0; list-style:none; }
* html ul#partial { height:1%; }
ul#partial li { float:left; width:50px; padding:12px 0; text-align:center;
margin:0 5px; color:#4c2; }
ul#partial li, ul#partial li .sb-inner { background:#444; }
ul#partial li .sb-border { background:#fff; }
/* The tabs */
#tabs { overflow:hidden; padding:0; margin:20px auto 0 auto; width:400px; color:#444; }
* html #tabs { height:1%; }
#tabs li { float:left; color:#000; list-style:none; padding:0; margin:0 0 0 5px; }
* html #tabs li { display:inline-block; }
#tabs li a { display:block; float:left; text-decoration:none; padding:5px; color:#000; }
#tabs li, #tabs li .sb-inner { background:#ccc; }
#tabs li:hover, #tabs li:hover .sb-inner { background:#eee; }
#tabs li.hover, #tabs li.hover .sb-inner { background:#eee; } /* for ie6 */
#tabs li .sb-border { background:#fff; }
#tabs-content { background:#ccc; width:380px; margin:-1px auto 20px auto; padding:10px;
border:1px solid #fff; color:#000; }
/* The links */
#links { color:#4c2; padding:20px; margin:20px; width:400px; margin:0 auto; }
#links, #links .sb-inner { background:#444 url(grad.png) repeat-x; }
#links .sb-border { background:#fff; }
#links a { color:#9f2; }
#links a .sb-shadow { background:transparent; }
#links a:hover .sb-shadow { background:#777; }
#links a.hover .sb-shadow { background:#777; } /* for ie6 */
#change-border { color:#fff; padding:20px; margin:20px auto; width:300px; }
.change-border0, .change-border0 .sb-inner { background:#444; }
.change-border1, .change-border1 .sb-inner { background:#888; }
/* ]]> */
</style>
<script language="javascript" type="text/javascript">
function getElementsByClass(cName) {
var mEls = [];
var uEls = document.getElementsByTagName("*");
var l = uEls.length;
var pattern = new RegExp("(^¦\\\\s)"+cName+"(\\\\s¦$)");
for (var i = 0; i < l; i++) {
if ( pattern.test(uEls[i].className) ) {
mEls.push(uEls[i]);
}
}
return mEls;
}
var shadowedBorder = RUZEE.ShadedBorder.create({ corner:8, shadow:16 });
shadowedBorder.render(getElementsByClass('bordered'));
</script>
</head>
<body>
<div class="headfoot">
<h1><a href="http://www.ruzee.com/blog/shadedborder">
RUZEE.ShadedBorder</a> Examples</h1>
</div>
<div id="content">
<div id="shadowed-border" class="bordered">
<p>This shadowed border has a width of 80% and scales with the size of
your browser window. Try it! ;-) Resize your browser window now, it's fun.</p>
</div>
<div id="shadowed-border" class="bordered">
<p>This shadowed border has a width of 80% and scales with the size of
your browser window. Try it! ;-) Resize your browser window now, it's fun.</p>
</div>
<div id="shadowed-border" class="bordered">
<p>This shadowed border has a width of 80% and scales with the size of
your browser window. Try it! ;-) Resize your browser window now, it's fun.</p>
</div>
<div id="shadowed-border" class="bordered">
<p>This shadowed border has a width of 80% and scales with the size of
your browser window. Try it! ;-) Resize your browser window now, it's fun.</p>
</div>
</div>
<div class="headfoot">
<div id="calcTime"></div>
<div id="renderTime"></div>
</div>
<script language="javascript" type="text/javascript">
if (!window.$) {
window.$ = function(id) { return document.getElementById(id); }
}
var currentChangeBorder=0;
function changeBorder() {
currentChangeBorder = (currentChangeBorder + 1) % changeBorders.length;
$('change-border').className = 'sb change-border' + currentChangeBorder;
changeBorders[currentChangeBorder].render('change-border');
}
var t = new Date().getTime();
shadowedBorder.render('shadowed-border');
// we have to render the bottom first because we need to exist JUST ONE DIV
// for getElementsByTagName and rendering top will insert loads of them ...
splitBorderBottom.render($('split').getElementsByTagName('div')[0]);
splitBorderTop.render($('split').getElementsByTagName('h3')[0]);
transBorder.render('trans-border');
linksBorder.render('links');
linkBorder.render($('links').getElementsByTagName('a'));
for (id in partialBorders) {
partialBorders[id].render(id);
}
var tabs = $('tabs').getElementsByTagName("li");
for (var i=0; i < tabs.length; ++i) {
tabBorder.render(tabs[i]);
}
changeBorder();
$('calcTime').innerHTML = calcTime;
$('renderTime').innerHTML = "Render time: " + ((new Date().getTime() - t) / 1000.0) + "s";
</script>
</body>
</html>
If you can, please copy and paste this to Dreamweaver or the text editor of your choice and create an external JavaScript file called shadedborder.js that contains the JS code I posted previously. You should see 4 dark gray divs, and only one div will have the effect. I don't know what I'm doing wrong here, hopefully you can help me on this one.
Thank you so much for all your help!
If possible, could you copy and paste the code that actually WORKS for you (all the DIV elements have the ShadedBorder script applied to them, instead of just the first one)? Or maybe you could just send me a quick PM, that would make things easier.
Thank you so much and sorry for my lack of JavaScript skills. I'm still new to JS.
Cosmoyoda
<!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>Examples</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="shadedborder.js"></script>
<style type="text/css">
/* <![CDATA[ */
body { posititon:relative; }
.bordered { padding:20px; width:80%; margin:20px auto; color:#fff; }
.bordered, .bordered .sb-inner { background:#444 url(grad.png) repeat-x; }
/* ]]> */
</style>
</head>
<body>
<div class="bordered">
<p>This shadowed border has a width of 80% and scales with the size of
your browser window. Try it! ;-) Resize your browser window now, it's fun.</p>
</div>
<div class="bordered">
<p>This shadowed border has a width of 80% and scales with the size of
your browser window. Try it! ;-) Resize your browser window now, it's fun.</p>
</div>
<div class="bordered">
<p>This shadowed border has a width of 80% and scales with the size of
your browser window. Try it! ;-) Resize your browser window now, it's fun.</p>
</div>
<div class="bordered">
<p>This shadowed border has a width of 80% and scales with the size of
your browser window. Try it! ;-) Resize your browser window now, it's fun.</p>
</div>
<script language="javascript" type="text/javascript">
function getElementsByClass(cName) {
var mEls = [];
var uEls = document.getElementsByTagName("*");
var l = uEls.length;
var pattern = new RegExp("(^¦\\\\s)"+cName+"(\\\\s¦$)");
for (var i = 0; i < l; i++) {
if ( pattern.test(uEls[i].className) ) {
mEls.push(uEls[i]);
}
}
return mEls;
}
var shadowedBorder = RUZEE.ShadedBorder.create({ corner:8, shadow:16 });
shadowedBorder.render(getElementsByClass('bordered'));
</script>
</body>
</html>
Are you sure that worked for you? I copy and pasted your code and had the shadedborder.js external javascript ready, but no DIV had the shaded border.
However, when I added the following code just after your JS at the bottom of the page...
<script language="javascript" type="text/javascript">
if (!window.$) {
window.$ = function(id) { return document.getElementById(id); }
} var t = new Date().getTime();
shadowedBorder.render('shadowed-border');
$('calcTime').innerHTML = calcTime;
$('renderTime').innerHTML = "Render time: " + ((new Date().getTime() - t) / 1000.0) + "s";
</script>
... and changed the ID of the 4 DIVs to "shaded-border", the first DIV had the rounded corners, but the other 3 had no rounded corners with shadows. =(
Don't you worry about this, I'm sorry I bothered you so much. If you can find a solution just tell me, but if its too hard don't worry. Thanks!
[edited by: tedster at 9:22 pm (utc) on Dec. 29, 2007]
var pattern = new RegExp("(^¦\\\\s)"+cName+"(\\\\s¦$)");
And don't worry about bothering me, if I didn't want to be helping i wouldn't be