Expanding the skill set: JavaScript and CSS Background Changer

February 28th, 2007

Once again I find myself confronted with having to use Javascript for a project. This time a dynamic background changer was needed (aka skin toggle). Using my feeble JavaScript Skills and a bit of copy and pasting I manage to put together a working template.

Here is an example. You can change the background of this site by clicking on the following thumnails:

How it works:
Firstly the background images are created/selected and placed in a folder together. Next name them the same with a just changing a number/letter/word at the end that. I recommend numbering them sequentially.

In an image editor crop small area of the image you will use as a button for it. You can of course use text buttons instead. In this case I have chosen to make the area around the buttons to change background to match the background image. If you want this effect then crop the large background images into smaller ones that match the size of the area you want to skin. Name the small background images the same as their larger counterparts adding a letter/number to the end. In the example I have used letter ‘a’.

Now your images are prepared start a new html document. In the head insert this javascript:

<script type="text/javascript">
function swapSkin(imgname) {
parent.document.body.style.backgroundPosition=”top center”;
parent.document.body.style.backgroundAttachment=”fixed”;
parent.document.body.style.backgroundRepeat=”no-repeat”;
url=”url(’../imageadds/bg”+imgname+”.jpg’)”;
urla=”url(’../imageadds/bg”+imgname+”a.jpg’)”;
parent.document.body.style.backgroundImage=url;
document.getElementById(’swapperheader’).style.backgroundImage=urla;
}
</script>

Replace the “../imageadds/bg” with the url of your background images folder. If they are not jpgs the change the extention too. If you did not use the letter ‘a’ after the filenames for your small backgrounds change this too.

Next the HTML. Here I have used inline styles so it will be easy for you to copy and paste and for me to use in an iframe.

<div id="swapperjobber" style="width: 460px; height: 200px; margin-top: 0; margin-right: auto; margin-bottom: 0px; margin-left: auto; overflow: hidden; background-image: url(../imageadds/bg11a.jpg);background-repeat: no-repeat; background-position: top center; background: transparent;">
<div id=”swapperheader” style=”height: 200px; width: 460px;”>
<div class=”links” style=”width: 435px; height: 50px; position: relative; top: 74px; left: 20px;”>
<a xhref=”javascript:swapSkin(’1′);” mce_href=”javascript:swapSkin(’1′);”><img xsrc=”../imageadds/but1.jpg” mce_src=”../imageadds/but1.jpg” style=”margin: 0 4px 0 0; width: 75px; height: 50px; border: 1px solid red;” /></a>
<a xhref=”javascript:swapSkin(’2′);” mce_href=”javascript:swapSkin(’2′);”><img xsrc=”../imageadds/but2.jpg” mce_src=”../imageadds/but2.jpg” style=”margin: 0 4px 0 0; width: 75px; height: 50px; border: 1px solid red;” /></a>
<a xhref=”javascript:swapSkin(’3′);” mce_href=”javascript:swapSkin(’3′);”><img xsrc=”../imageadds/but3.jpg” mce_src=”../imageadds/but3.jpg” style=”margin: 0 4px 0 0; width: 75px; height: 50px; border: 1px solid red;” /></a>
<a xhref=”javascript:swapSkin(’4′);” mce_href=”javascript:swapSkin(’4′);”><img xsrc=”../imageadds/but4.jpg” mce_src=”../imageadds/but4.jpg” style=”margin: 0 4px 0 0; width: 75px; height: 50px; border: 1px solid red;” /></a>
<a xhref=”javascript:swapSkin(’5′);” mce_href=”javascript:swapSkin(’5′);”><img xsrc=”../imageadds/but5.jpg” mce_src=”../imageadds/but5.jpg” style=”margin: 0 4px 0 0; width: 75px; height: 50px; border: 1px solid red;” /></a>
</div>
</div>
</div>

Notice the area that is to have it’s background changed is called ’swapperheader’ as referenced by the javascript. If you want to name the div differently then change the javascript too. Also change the url of the buttons to match where you have your buttons. Note the links are javascript and they assign a value to the “swapSkin” function. This in turn sets the background image url.

Give it a try and if you get stuck your welcome to comment and I will help out where possible.

If comepleted successfully you can embed the html into any page or save it as a separate page and call it via an iframe as I have done in this example.

For more Semantic mark up you can have the javascript in a separate file and call that in. If you are finding your background changes sluggish you can chose to preload the images.

Next in this series I will be looking at CSS Transparencies…

Have your say?

Comments welcome - you don't have to agree :)

4 Responses to “Expanding the skill set: JavaScript and CSS Background Changer”

  1. From The Front-End » Case Study: Gamespot UK Toggle Skin on April 13, 2007 9:53 am

    [...] The CSS transparency roll-over was used to create a roll-over effect for the buttons. This was especially useful as the buttons changed when the skin did to indicate which skin was currently active. The designer could have gone further and change the protagonists pose for each skin as it is part of a background image and doesn’t require a new button each time. [...]

  2. Stephen Blake on December 14, 2007 1:26 pm

    Nice article - Any idea how to only change the background of the div () and not the entire body?

  3. David Long on December 14, 2007 1:54 pm

    Take out the parts of the javascript that deal with the body background and just use the get element by ID and change the background of the div you want.

    You can see this in action on one of my recent sites in the gallery - http://www.beecleanlondon.co.uk

    David

  4. arama motoru on June 17, 2008 6:58 am

    css background examples , Properties , Attribute - - css-lessons.ucoz.com/background-css-examples.htm

Name (required)

Email (required)

Website


Speak your mind

Trackback URL | Comments RSS

    About

    Although originally designed to document my work and new web development tricks I learnt it has expanded to cover tech and news that I find interesting so in addition to tutorials and interviews expect to see product reviews and tech news too. If you enjoy please comment. David

    Blogroll
    • Adele Kirby - Freelance Writer and most chipper/perky person alive
    • Alex Pullin - Freelance Tech Writer and former PR queen
    • Display: Block - Blog of fellow developer Savell Martin
    • Molly E. Holzschlag - Web standards advocate and Genious
    • Nate Laxon - Funny guy and music guru
    • Paul Boag - Helping website owners and promoting webstandards. The King of Web Design Podcasts
    • Poached Online - news/current affairs/political comment: no eggs
    • Tale of 2 Blogs - Blog about my other blogs
    Admin
  • From Display:Block
    • SpringWidgets
      RSS Reader
      This widget is the staple of our platform. Read all your feeds right here with this one widget - Supported feeds are OPML, RSS, RDF, ATOM. Watch your favorite Podcast in the embedded Video Player on the Desktop or publish your own video playlist to your site for others to view!
      Visit the Widget Gallery