Home arrow HTML arrow Page 3 - Background Images for a Menu for All Browsers
HTML

Background Images for a Menu for All Browsers


In this seventh part of a nine-part series on building a menu that works in all browsers, we complete the discussion of the JavaScript functions. We go on to see how we can use background images for menu items instead of background colors. We shall start winding up the series at the end of this part.

Author Info:
By: Chrysanthus Forcha
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
June 17, 2009
TABLE OF CONTENTS:
  1. · Background Images for a Menu for All Browsers
  2. · Using Background Images
  3. · Changes to the Code
  4. · Review of the Project

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Background Images for a Menu for All Browsers - Changes to the Code
(Page 3 of 4 )

We have to make some changes to the code in order to support the images. For simplicity, we shall make the changes to affect only the drop down and sub menus and not the main menu. We begin with the CSS. Change all the following expressions in the CSS:

background-color:brown

to

background-image:url("norm.gif");

So, instead of a background color, we have a background image for the cells. Add the following expression next to each of the above code in the CSS:

background-repeat: repeat-x;

This expression causes the image to repeat along the cell (horizontally). So it does not matter how wide the table cell is; you will have a bar as the background of the cell due to the "repeat-x" property.

Let us see the changes to be made to the JavaScript code. Replace all the "backgroundColor" properties with "backgroundImage." Replace all the "brown" values with "url('norm.gif')." Replace all the "firebrick" values with "url('ov.gif')."

For the non-image code, the toChoose() function was:

function toChoose(ID)

{

currCellBgColor = document.getElementById(ID).style.backgroundColor; //background color of the current cell

 

if ((currCellBgColor == "firebrick")||(currCellBgColor == "#b22222")||(currCellBgColor == "rgb(178, 34, 34)"))

subMenuJustClicked = true;

}


Now with the images, it is:


function toChoose(ID)

{

currCellBgColor = document.getElementById(ID).style.backgroundImage; //background color of the current cell

 

if (currCellBgColor == "url('ov.gif')")

subMenuJustClicked = true;

}


Note that in the if-condition we have no longer taken into consideration the peculiarity of any browser. The expression "url('ov.gif')" in the if-condition is recognized by all of the five major browsers we use for testing. The other statements in the toChoose() function remain the same; only the test of the if-condition has changed.

That is all we need for the changes. I tested the code and it worked with the five browsers (Internet Explorer, Mozilla Firefox, Netscape, Opera and Safari). There is just a small problem with Opera. When you open the page with Opera and then move the mouse pointer over a main menu item, the default image (norm.gif) will go away, but you will not see the onmouseover image (ov.gif). This happens only once, for the first time; after that it does not happen again. After that you will be seeing the ov.gif image for any menu item (main menu, drop down menu or sub menu) that the mouse pointer goes over. This problem is negligible. So we will ignore it.

I want you to note that the code with background images does not really have any code segment specialized for any particular browser. The non-image code had the if-statement with bits for different browsers. This if-statement for the image code does not have these bits. So we can say that with the image code, we have attained our goal of having code that works for all browsers, without any code segment specialized for any browser. The trouble is that there is a condition, which is that you need to have background images and not background colors.

The complete image code can be downloaded from here:

versatileCommonMenuImg.ZIP

The image files are included. After downloading, unzip in one directory and double click the HTML file.


blog comments powered by Disqus
HTML ARTICLES

- HTML5 Boilerplate: Working with jQuery and M...
- HTML5 Boilerplate Introduction
- New API Platform for HTML5
- BBC Adopts HTML 5, Mozilla Addresses Issues
- Advanced Sticky Footers in HTML and CSS
- HTML and CSS Sticky Footers
- Strategy Analytics Predicts HTML5 Phones to ...
- HTML5 Guidelines for Web Developers
- Learning HTML5 Game Programming
- More Engaging CSS3 and HTML Background Effec...
- Engaging HTML and CSS3 Background Effects
- More Web Columns with CSS3 and HTML
- Columns with CSS3 and HTML
- Creating Inline-Block HTML Elements with CSS
- Drag and Drop in HTML5: Parsing Local Files

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials