26th March 2004, late evening | Comments (6)
Status | |
---|---|
Code usage | No longer in use on this blog |
Updates | Available |
Demo | Available |
Downloads | Available |
Okay, revision time. Earlier today I posted an article describing how I'd built my new reading page. Five minutes after I put that live, Mark Wubben pointed out a couple of ways the script could have been improved, and he was right.
So, here’s the revised Javascript code, and I think you’ll agree it's a hell of a lot simpler than the first effort:
// Event Listener
// by Scott Andrew - http://scottandrew.com/
function addEvent(obj, evType, fn)
{
if (obj.addEventListener)
{
obj.addEventListener(evType, fn, false);
return true;
}
else if (obj.attachEvent)
{
var r = obj.attachEvent('on'+evType, fn);
return r;
}
else
{
return false;
}
}
// from your man at http://www.kryogenix.org/
addEvent(window, "load", resizeBooks);
addEvent(window, "resize", resizeBooks);
function resizeBooks(e)
{
// check this browser can cope with what we want to do
if (!document.getElementById) return;
var leftDiv = document.getElementById('left');
if (!leftDiv) return;
if (!leftDiv.offsetWidth) return;
// if div#left is wide enough to accept two or more floated <dl>s
if (leftDiv.offsetWidth >= 621)
{
// loop through <dl>s
defLists = leftDiv.getElementsByTagName('dl');
for (var i = 0; i < defLists.length; i++)
{
// set class to nada
defLists[i].className = '';
}
}
// if div#left is not wide enough to accept two or more floated <dl>s
else
{
// loop through <dl>s
defLists = leftDiv.getElementsByTagName('dl');
for (var i = 0; i < defLists.length; i++)
{
// set class to liquid
defLists[i].className = 'liquid';
}
}
}
And here’s the revised CSS (note the addition of the class ‘liquid’, which has replaced all the complicated Javascript pixel-sizing):
/* lay everything out for browsers like IE */
body#reading dl {
border-bottom: 1px solid #ddd;
float: left;
height: 170px;
margin: 0 25px 20px 0;
padding: 0 0 0 113px;
position: relative;
width: 150px;
}
/* Must have this level of specificity in order to override rules below it in the hacks */
body#reading div#left dl.liquid {
display: block;
float: none;
margin-right: 0;
width: auto;
}
body#reading dl dd div {
background: url('/blog/commonpics/bg_image.gif') no-repeat bottom right;
left: 5px;
position: absolute;
top: 5px;
}
/* reset float for Opera */
html > body#reading dl {
float: none;
}
/* reset float for FF */
html[xmlns] body#reading dl {
float: left;
}
Thanks to everyone who tested the first version out and told me it fell over in IE. You can see this new script working on the demo page.
Jump up to the start of the post ↑
A collection of miscellaneous links that don't merit a main blog posting, but which are interesting none-the-less.
Our enemies are innovative and resourceful, and so are we. They never stop thinking about new ways to harm our country and our people, and neither do we.— George W Bush (9)
Stuff from the intersection of design, culture and technology.(3)
A selection of blogs I read on a regular basis.