Homepage takeover with existing skyscraper ads

14th February, 2015 - Posted by david

UPDATE: Originally I was using Javascript to position the 2 banner divs along the main content, but have since figured out how to do this with just CSS alone. I’ve left the article as it was for posterity; there should be enough here to still figure out what you need to do!

Anyways, to achieve this with just CSS it’s actually quite simple. For the left-banner and right-banner div, remove the right and left CSS values below and then just reload the page. The 2 banners will still be fixed in position at the top, but will be arbitrarily placed on top of the content. To move them to the sides of the content, simply use margin-left to position them where you want! Most likey, the left banner will need a negative margin and the right will need a positive. For IE and it’s different versions, you may need slightly different margins, but the desired effect should still be achievable. The banners should also hold their position correctly when you resize the window.

(original article follows here)

Sometimes in work we can be asked to do things we don’t like and recently I was asked to look into implementing one of those homepage takovers. Personally, I think these are awful and would like to think I wouldn’t degrade my site by implementing one, but they do make money and have a high click rate, so I can see why sites like to use them.

Normally they’re done using a fixed background wallpaper that’s clickable all the way to the edge of the page. However, I was asked to simulate this look using 2 existing skyscraper ads, 170px in width, to be positioned either side of the main content and fixed to the top of the page. Since it wasn’t entirely straightforward, I thought I’d block about it here, to help anyone else in a similar situation. I’m not going to go into the specifics of displaying the ads, simply the CSS and Javascript involved in positioning them where you want them.

I should point out, this might be possible with just CSS, but changing a site’s fundamental structure to accomodate the new columns isn’t always possible. Also, you might only want the takeover on the homepage and not other pages. This solution should have minimal impact, as it simply adds 2 divs, that can go anywhere in the HTML.

So, to describe the set-up, let’s say our main content is 1000px in width, centred in the page and we want 2 170px x 1068px divs to contain our ads and line up on the right and left of that content, as well as for the 2 ads to remain fixed at the top of the page, no matter how far we scroll down. We’ll give each of these divs a class of side-banner, with left-banner and right-banner IDs. Since these are going to be positioned explicitly, it doesn’t really matter where in the HTML you put them, maybe just inside your main content div. Initially, we’re simply going to position them in the extreme top corners of each side. I’m also going to give them different background colours, so we can know they’re positioned correctly without having to worry about loading the ads (which can come later).

.side-banner {
  width: 170px;
  height: 1068px;
  position: fixed;
  top: 0;
}
#right-banner {
  right: 0;
  background: red;
}
#left-banner {
  left: 0;
  background: green;
}

To align these alongside the content, I needed to write a Javascript function (position_banners()) to position them correctly. This function is called when the page finishes loading, as well as when the window is resized. It simply gets the body’s width, subtracts the width of our main content (remember 1000px), divides the result by 2 (as we’ve 2 sides), then further subtracts the width of our banners. This fairly basic formula works out the amount to move each div in from their corresponding edge, to line up with our main content. Then, we just use CSS left and right to position them nicely.

$(document).ready(function() {
  position_banners();
  $(window).resize(position_banners);
});

function position_side_divs() {
  var margin = ($('body').width() - 1000) / 2 - $('#left-banner').width(),
      left = Math.floor(margin),
      right = Math.ceil(margin);
  $('#left-banner').css({left: left + 'px'});
  $('#right-banner').css({right: left + 'px'});
}

I know this code isn’t the tidiest, but should be enough to get the idea of what you need to do.

To further enhance the ‘takeover’ effect, you could display a 970px x 250px ‘billboard’ right at the top of your main content.

Tags: css javascript jquery | david | 14th Feb, 2015 at 12:13pm | No Comments

No Comments

Leave a reply

You must be logged in to post a comment.