Solved

Problem with div links and hover state background

Posted on 2008-10-02
14
1,301 Views
Last Modified: 2012-08-13
http://csn.mc.com/test.aspx

I'm trying to implement a html/css horizontal menu where divs are the links using onclick events.

The divs have an inital background but when you hover over the div it will change the new background.

Here is some of the code from the link above to make this easier.


<%@ Page Language="C#" AutoEventWireup="true" Title="Converged Sensor Networking" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

 

<head runat="server">

    <link rel="Stylesheet" href="MASTERMaster_page.css" type="text/css" media="all" />

    <!--[if IE 6]>

    <link rel="stylesheet" type="text/css" href="ie6fix.css" />

    <![endif]-->

</head>

<body>

<div id="learn" onclick="location.href='http:/csn.mc.com/learn.aspx';" style="cursor:pointer;"></div>

<div id="differ" onclick="location.href='http://csn.mc.com/differ.aspx';" style="cursor:pointer;"></div>

<div id="solve" onclick="location.href='http://csn.mc.com/solve.aspx';" style="cursor:pointer;"></div>

<div id="contact" onclick="location.href='http://csn.mc.com/contact.aspx';" style="cursor:pointer;"></div>

</body>

<html>

 

 Here is the .css code
 

div#learn {

	background: url('img/global_nav/learn.jpg') 0 0 no-repeat;

	border:none;

	width:88px;

	height:27px;

	float:left;

	margin-top:80px;

}

div#differ {

	background: url('img/global_nav/differentiate.jpg') 0 0 no-repeat;

	border:none;

	width:173px;

	height:27px;

	float:left;

	margin-top:80px;

}

div#solve {

	background: url('img/global_nav/solve.jpg') 0 0 no-repeat;

	border:none;

	width:105px;

	height:27px;

	float:left;

	margin-top:80px;

}

div#contact {

	background: url('img/global_nav/contact.jpg') 0 0 no-repeat;

	border:none;

	width:108px;

	height:27px;

	float:left;

	margin-top:80px;

}

 

div#learn:hover {background: url('img/global_nav/learn-over.jpg') 0 0 no-repeat;}

div#differ:hover {background: url('img/global_nav/differentiate-over.jpg') 0 0 no-repeat;}

div#solve:hover {background: url('img/global_nav/solve-over.jpg') 0 0 no-repeat;}

div#contact:hover {background: url('img/global_nav/contact-over.jpg') 0 0 no-repeat;}

 

div#learn:visited {background: url('img/global_nav/learn.jpg') 0 0 no-repeat;}

div#differ:visited {background: url('img/global_nav/differentiate.jpg') 0 0 no-repeat;}

div#solve:visited {background: url('img/global_nav/solve.jpg') 0 0 no-repeat;}

div#contact:visited {background: url('img/global_nav/contact.jpg') 0 0 no-repeat;}

Open in new window

0
Comment
Question by:no158
  • 8
  • 4
  • 2
14 Comments
 
LVL 2

Expert Comment

by:shonk
ID: 22625747
That seems to be working here, at least in FF3 and IE7... What's the problem?

The only thing I would suggest is preloading the images so that there isn't a delay in them showing.
0
 

Author Comment

by:no158
ID: 22626285
Dose not seem to be working in IE6. The img wont switch.
0
 

Author Comment

by:no158
ID: 22626357
How do I preload the images ?
0
 
LVL 44

Expert Comment

by:scrathcyboy
ID: 22629636
There is no hover or horizontal menus working on IE6 at all, and no color change like Moz- FF2.  

Your CSS is lacking some basic link styling, like the following --

A:link       {color : #0000FF; text-decoration: none; background-color:#CCCCCC;}
A:visited   {color : #0000FF; text-decoration: none; background-color:#CCCCCC;}
A:active   {color : #0000FF; text-decoration: none;background-color:#CCCCCC;}
A:hover    {color: #CC0000; text-decoration: underline; background-color:#aa8844;}

Add that and adjust those values until you get what you want.

I don't see where or why you need to preload images on this page -- there are no images I can see.
0
 

Author Comment

by:no158
ID: 22634181
Each part of the menu is an img...

The background for the div is an image i.e. the background is the text ("Learn"), which is an img. When you hover over the img will switch from a gray looking img to a orange looking img.


How would you preload, because I can see how it would matter when you reload the page on IE7. There seems to be a little choppiness in the img.
0
 
LVL 2

Accepted Solution

by:
shonk earned 250 total points
ID: 22634346
Personally, I'd load the images in to the page and hide them using CSS. (display: hidden;) That way, it doens't rely on having Javascript turned on like a lot of the preloaders do.

There is still a chance that your visitors will notice some choppiness if they mouse over the images too soon, but this can't really be helped, unless you use one image for each menu item, and move it on mouseover. It's a technique I've used in the past very effectively.

See:
http://www.redvodkajelly.com/blog/2007/06/15/css-image-rollovers/ and
http://www.wellstyled.com/css-nopreload-rollovers.html

Hope this helps.
0
 
LVL 44

Expert Comment

by:scrathcyboy
ID: 22635807
shonk's preloading in place and hiding is fine.  

The other way we used to do it is load them all at the bottom of the screen, like this --
<IMG SRC = "images/wbln.jpg" NAME = "img11" HEIGHT="1" WIDTH="1">
 then use javascript to swap the images into the right place when mousing over.  Either way works.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:no158
ID: 22636450
I did the joined picture trick where you take the rollover state and the non-rollover state and join them side by side so they are only 1 picture and just moved the position.

It fixed the loading choppiness and also fixed a bug in opera but it still did not fix the problem with IE6. The image loads but it will not move the position on rollover.

Any ideas?

<head><link rel="Stylesheet" href="MASTERMaster_page.css" type="text/css" media="all" />

    <!--[if IE 6]>

    <link rel="stylesheet" type="text/css" href="ie6fix.css" />

    <![endif]-->

<title>

	Converged Sensor Networking

</title></head>

<body>

<div id="learn" onclick="location.href='http://csn.mc.com/learn.aspx';" style="cursor:pointer;"></div>

<div id="differ" onclick="location.href='http://csn.mc.com/differ.aspx';" style="cursor:pointer;"></div>

<div id="solve" onclick="location.href='http://csn.mc.com/solve.aspx';" style="cursor:pointer;"></div>

<div id="contact" onclick="location.href='http://csn.mc.com/contact.aspx';" style="cursor:pointer;"></div>

</body>

<html>
 

The new CSS:
 

div#learn {

	background: url('img/global_nav/learnC.jpg') 0 0 no-repeat;

	border:none;

	width:88px;

	height:27px;

	float:left;

	margin-top:78px;

	position:relative;

}

div#differ {

	background: url('img/global_nav/differentiateC.jpg') 0 0 no-repeat;

	border:none;

	width:173px;

	height:27px;

	float:left;

	margin-top:78px;

	position:relative;

}

div#solve {

	background: url('img/global_nav/solveC.jpg') 0 0 no-repeat;

	border:none;

	width:105px;

	height:27px;

	float:left;

	margin-top:78px;

	position:relative;

}

div#contact {

	background: url('img/global_nav/contactC.jpg') 0 0 no-repeat;

	border:none;

	width:108px;

	height:27px;

	float:left;

	margin-top:78px;

	position:relative;

}
 

div#learn:hover {background-position: -88px 0;}

div#differ:hover {background-position: -173px 0;}

div#solve:hover {background-position: -105px 0;}

div#contact:hover {background-position: -108px 0;}

Open in new window

0
 

Author Comment

by:no158
ID: 22636617
It seems the problem is because of this code:

div#learn:hover

is there another way to do the hover state for IE6?
0
 
LVL 44

Assisted Solution

by:scrathcyboy
scrathcyboy earned 250 total points
ID: 22637789
use javascript onMouseOver() to set any property of any link or image in the page after it is loaded, that is the easiest way to overcome CSS you can't change.  If you can, then just change it to what you want.
0
 

Author Comment

by:no158
ID: 22637893
if someone does not have javaScript enabled then this feature will not work. I'm trying to avoid it but I might have a fix for it using an htc file. It will scan the .css file and if it find the :hover it will run a function to take care of this problem. I'm just wondering if the htc is working like a javaScript.

Can be found at www.xs4all.nl/peterned/csshover.html

0
 

Author Comment

by:no158
ID: 22637978
Ahh forget it, I give up on trying to avoid javaScript. Everyone should have it enabled....

I'm already using onclick so I might as well use onmouseover.

Thanks guys.

You have both been a big help.
0
 
LVL 44

Expert Comment

by:scrathcyboy
ID: 22638691
So javascript was the solution then, not the one you accepted as the solution? You decided that my suggestion of on mouse over was the best solution -- just for the record?
0
 

Author Comment

by:no158
ID: 22638746
I'm using the javaScript and the trick with loading one picture. The mouseover will just change the position to the picture.

Thats why I split up the points evenly. I figured you were both so helpful, should spread the wealth.

For the record. I didn't know about the accepted solution and assisted solution till now. If i had to do it again I would put your answer as the accepted solution and shonk as the assisted.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now