By Alex Walker

The Catfish – Part 1

By Alex Walker
Last chance to win! You'll get a... FREE 6-Month Subscription to SitePoint Premium Plus you'll go in the draw to WIN a new Macbook SitePoint 2017 Survey Yes, let's Do this It only takes 5 min

If you’ve visited the site at all in the last 2 months you would have most likely noticed our new Catfish book banners hugging the page bottom from time-to-time. Since they launched, we’ve been receiving around 3-4 comments/email per week asking how it’s done. So, rather than answering each email individually, we thought this is might be a good place to walk you through the finer points — for those interested.

The Catfish waits..

Of course, the code has always been there in public view, but if you have ever trawled through it you’ll know that SitePoint has a lot of deeply interwoven CSS and scripting, so we’re going to break out a streamlined version of the Catfish into a holding tank.


Firstly, some basics. Yes, the Catfish is ‘nought but a ‘garden-variety’ DIV with some CSS and a bit of JavaScript to make it shimmey — no crazy technology required.

The first ‘proof of concept’ was hacked together with no animation and pure CSS. At that stage the idea was just get a sense of how it looked on the page, so we set it up quickly using the ‘position:fixed’ CSS property and a little JavaScript to make it go away when asked.

The DIV was slotted in at the end, just before the closing BODY tag. We also padded the bottom of the HTML element to make sure the footer could never be obscured by the Catfish.


#catfish {
background:transparent url(images/catfish-tile.gif) repeat-x left bottom;
height: 79px;  /* includes transparent part */
cursor: pointer;
margin: 0; 
html {
padding:0 0 58px 0; /* 58px = height of the opaque part of the Catfish */

The contents of ‘DIV#catfish’ are totally up to you. You could conceivably use it for navigation, site announcements, log-in panels or a multitude of things. Space is obviously limited, so keeping things relatively simple is recommended.

After demoing it with some of the guys here, we all agreed the idea had some legs. At this point the major issue became the small matter that it didn’t work at all in Internet Explorer. If you’re viewing the demo in IE you’ll see that the DIV is behaving exactly as if it were ‘position: static‘ (the default). Our big challenge was to make IE play ‘nice-like’ — and that’s what I’ll be concentrating on here.

There has already been plenty of good work on the ‘fixed’ problem from (amongst others) Stu Nicholls, Simon Jessey and Petr ‘Pixy’ Stanicek. Although each differed in the fine print, they generally seemed to agree on some main tenets — position the ‘wannabe fixed DIV’ using ‘position:absolute’ and then wrap everything else in a ‘position:relative’ DIV to keep them apart. Sounded like a good place to start.

We also made another decision at this point. Since FireFox, Opera and Safari do a dandy job with the W3C standard ‘position:fixed’, why throw extra markup at them? — only IE would be getting the extra markup.

In this ‘sandbox’ version, I’m going to attach our IE-specific styles and scripting using ‘conditional comments’, although we actually use ‘object sniffing’ to target IE on the live version. I think conditional comments are great way to go at the moment as they invoke a purpose-built function within IE, rather than relying on fixable and likely transient browser bugs. With IE7 on the horizon, relying on bugs is more dangerous occupation than ever before.

Conditional Comments

<!--[if IE]>
<link rel="stylesheet" href="IEhack.css" type="text/css" />

The above markup will allow us to deliver different styles to IE only. Other browsers will read it as a ‘bog standard’ HTML comment, which means that HTML validators will also find it wholesome and satisfying. If IE7 supports ‘position:fixed’, it will be trivial to change the comment to make it only target IE6 and older ( e.g. ‘<!--[if lt IE 7]> ...‘ if less than IE7).

So, what extra CSS should we send IE ?

Not a great deal. We need to switch Catfish’s positioning to ‘absolute‘, set it’s z-index to ‘100‘ to keep it at the front, and set it’s overflow to ‘hidden‘.


#catfish {
position: absolute;
z-index: 100;
overflow: hidden;

Now we have our Catfish positioned correctly — that is, until we try to scroll, at which point it trundles off up the page. The problem is the browser calculates ‘bottom:0‘ as the exact point where the bottom of the viewport overlaps the BODY — when the BODY scrolls, that point moves with it.

So, in theory, we can fix this problem by taking the rather drastic-sounding action of forcibly preventing our BODY from scrolling under any circumstances. Using ‘overflow:hidden‘ and ‘height:100%' we can force the viewport, the HTML element and the BODY element to aquire exactly the same dimensions. No scrolling means the Catfish stays put.


html, body {
overflow: hidden;

Of course, this minor victory has been regretably soured by the fact that we now have no way of accessing any content outside our viewport. It’s now that we call on that wrapper DIV mentioned in other methods. I’ve called it ‘#zip‘ as we’re ‘zipping up’ all the non-catfish content up into it. In the markup it looks something like this.


<div id="zip">
<div id="masthead...

...</div>...<!-- close zip -->
<div id="catfish">...

...</div><!-- close catfish-->

This new ‘div#zip‘ is now bulging with most of the content on the page, so if we set it’s overflow to ‘auto‘, it will only too happy to give us some nice new scrollbars. These scrollbars will be almost indistinguishable from BODY‘s own default scrollbars. The CSS for this DIV is pretty unremarkable.


div#zip {
width: 100%;
height: 100%;
overflow: auto;
position: relative;

Ok, so now IE is playing nice and giving a fine imitation of a browser who knows what fixed positioning is,… just as long as we give it that extra DIV to work with.

But, as I said above, why burden better browsers with stuff they don’t use? It’s a DIV that is more likely to hinder than help them, so let’s inject it only into IE using the DOM.

We’re going to add a new function called ‘wrapFish‘. The script goes a little like this.


function wrapFish() {
 var catfish = document.getElementById('catfish'); 
   // pick the catfish out of the tree
 var subelements = [];
  for (var i = 0; i < document.body.childNodes.length; i++) {
  subelements[i] = document.body.childNodes[i]; 
    // write everything else to an array
 var zip = document.createElement('div');    
   // Create the outer-most div (zip) = 'zip';                     
   // give it an ID of  'zip'
 for (var i = 0; i < subelements.length; i++) {
    // pop everything else inside the new DIV
   // add the major div back to the doc
    // add the Catfish after the div#zip
 window.onload = wrapFish;  
    // run that function!

The comments give you the blow-by-blow on what it’s doing, but, in short, it:

  • takes the catfish out of the document,
  • creates the new DIV#zip,
  • copies everything else into the new DIV,
  • attaches that DIV to the document, and
  • tacks the catfish back on the end

Now all we need to do is call this script from inside our conditional comment. IE now has the extra ‘leg-up’ it needs and all the other little browsers will be none the wiser.

Conditional Comments

<!--[if IE]>
<link rel="stylesheet" href="IEhack.css" type="text/css" />
<script type="text/javascript" src="catfish.js">

So, there you have it. I’ve left a red dashed border on ‘div#zip‘ to demonstrate that only IE is rendering that extra DIV. We’ve patched IE without messing with anyone else.

So, is that all you have to know to get a Catfish-like system running ?

Not quite. It’s more than likely you only want to run the Catfish on certain pages, at certain times, so we need an intelligent system that knows if and when to inject the Catfish via the DOM. It would also be nice to be able to select from a library of different banners.

Tom will take tackle these and other exciting problems in Part II — coming soon.

Login or Create Account to Comment
Login Create Account
Get the most important and interesting stories in tech. Straight to your inbox, daily.
Is it good?Is it good?