Forum Moderators: open

Message Too Old, No Replies

a:hover state

         

gleddy

3:36 am on Sep 2, 2005 (gmt 0)

10+ Year Member



hello, hello.

I am just curious whether it is possible to test if an element is currently active in its hover state.

specifically if an elements pseudo:hover is active?

ex.
#thisDiv:hover

active == true
active == false.

something like that.

cheers!

gleddy

3:38 am on Sep 2, 2005 (gmt 0)

10+ Year Member



sorry. to expand on this... this would be for the mozilla browser or any that do support the pseudo:hover attribute for any element.

Rambo Tribble

4:14 am on Sep 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<style type="text/css">
p{height:300px;background:#dee;}
p:hover{background:#eed;} /* a.k.a. rgb(238,238,221) */
</style>
<script type="text/javascript">
</script>
</head>
<body>
<p onclick="alert(document.defaultView.getComputedStyle(this,':hover').getPropertyValue('background-color'));"></p>
</body>
</html>

gleddy

11:43 pm on Sep 4, 2005 (gmt 0)

10+ Year Member



thanks for the help!

I am having trouble with this a bit. Is there a way to change this to testing the element:hover, on the basis of true or false?

so basically removing the onclick.

I want to hide/show certain elements depending on if another element has element:hover == true.

So basically giving that alert("this hover state is active"); without having to click.

can't crack it so far!
thanks!

Rambo Tribble

12:32 am on Sep 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can use whatever event you want to check the properties assigned to the :hover pseudo-element, but you will have to test for a specific property setting with getPropertyValue. getComputedStyle() will always return a CSSStyleDeclaration object, whether :hover has been applied or not.

gleddy

1:16 am on Sep 5, 2005 (gmt 0)

10+ Year Member



great. makes sense.. thanks for your help!

gleddy

7:34 am on Sep 6, 2005 (gmt 0)

10+ Year Member



hello, hello.

well I have been battling with this for days and am a bit stuck... but I am learning about cross-browser javascript a bit!

I am currently stuck making this function work in Mozilla as well... the checkHover() function works if I use document.onclick, but if I try to get more specific than that I start to get stuck.

basically I want it working exactly as it is in IE (which is working perfectly)

code below! cheers if you look! :-)

-----

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

<html>
<head>
<title>Untitled</title>

<script type="text/javascript">
<!--
// IE function
sfHover = function() {
var sfEls = document.getElementById("wrapper").getElementsByTagName("div");
if (sfHover) {
for (var i=0; i<sfEls.length; i++) {
sfEls[i].onmouseover = function() {
this.className += " sfhover";
document.getElementById("help").style.display = "block";
}
sfEls[i].onmouseout = function() {
this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
document.getElementById("help").style.display = "none"; // this needs to be generic
}
}
}
}

// mozilla function
checkHover = function() {
document.onclick = function() {
alert("yeahhhhhhh");
}
}

// load events
if ( window.attachEvent ) {
window.attachEvent("onload",sfHover);
}
else if ( window.addEventListener )
{
window.addEventListener("click",checkHover,true);
}

//-->
</script>

<style type="text/css">
#leadContainer{
width: 200px;
height: 600px;
position: relative;
background: red;
}
#help {
width: 200px;
height: 200px;
display: none;
position: absolute;
background: red;
top: 100px;
left: 100px;
}
#leadContainer.sfhover { display: block; background: yellow; filter: alpha(opacity=50); }
#leadContainer:hover { display: block; opacity: .5; background: yellow; }
</style>
</head>

<body>

<div id="wrapper">
<div id="leadContainer">This is the element.</div>
</div>
<span id="help">Help bubble</span>

</body>
</html>

Rambo Tribble

2:43 am on Sep 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Perhaps it would be simpler if you described what you want to accomplish. Are you trying to switch classes? Are you trying to accommodate IE's lack of support of :hover on elements other than links?

gleddy

11:34 pm on Sep 8, 2005 (gmt 0)

10+ Year Member



yup. sorry about that, was getting kind of convoluted...

basically I want to start up a help feature for an existing page I have. I want to use alternative stylesheets that will cause the following action.

1) cause block-level container divs to have a pseudo:hover state (which I chose opacity, #fff, 50%) when hovered.

2) when (for example) #main_story:hover is active, then I will have a corresponding unordered list (or span, etc) at the bottom of the page that will turn on/off depending on it's related block-level container div's actions.

make sense? (maybe not, here is an example):

----

<div id="main_story>
<p> content here </p>
<img src="picture.jpg" />
</div>

<ul>
<li id="main_story_tip">content</li>
<li id="second_story_tip">content</li>
<li id="left_nav_tip">content</li>
</ul>

----

so in the above example, when #main_story hover is active, then #main_story_tip would be styled to display: block, instead of hidden.

I did get this working in IE, but I think I went a bit off course as I was using the suckerfish based functions and might have gone a bit over-complicated. I need to tackle it with a fresh perspective I think.

so any ideas welcome. thanks!