Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-18-2010, 09:05 PM   PM User | #1
Left_blank
New Coder

 
Join Date: Jul 2010
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Left_blank is on a distinguished road
hover problem, seeing two divs at once

My basic set up is that I have some divs which I want to fade in and out when the user hovers over an image.

Code:
<div id="DefaultHint" >The default hint is in here, shown initially</div>
<div id="PPHint" style="display:none" >There is a different hint here, hidden initially</div>
Here is the code I have so far:

Quote:
$('#QmarkCOP').hover(
function(event){
$('#DefaultHint').fadeOut('fast', function(){
$('#PPHint').fadeIn('fast');
})
},
function(event){
$('#PPHint').fadeOut('fast', function(){
$('#DefaultHint').fadeIn('fast');
});
}
);
Now this works ok except for when I move in and out of the image (QmarkCOP) too fast. Then I see both hints at once.

Any ideas how I can prevent this from happening?

Thanks for any help.
Left_blank is offline   Reply With Quote
Old 08-18-2010, 09:24 PM   PM User | #2
iamjerson
New to the CF scene

 
Join Date: Jun 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
iamjerson is an unknown quantity at this point
try this instead

$('#QmarkCOP').hover(
function(){
$('#DefaultHint').fadeOut('fast');
$('#PPHint').fadeIn('fast');
},
function(){
$('#DefaultHint').fadeIn('fast');
$('#PPHint').fadeOut('fast');
}
);
iamjerson is offline   Reply With Quote
Old 08-18-2010, 09:55 PM   PM User | #3
Left_blank
New Coder

 
Join Date: Jul 2010
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Left_blank is on a distinguished road
Nope, that makes it a little different but now I see both hints at once for a millisecond on the transition as well as when I hover in and out too fast!
Left_blank is offline   Reply With Quote
Old 08-18-2010, 11:48 PM   PM User | #4
iamjerson
New to the CF scene

 
Join Date: Jun 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
iamjerson is an unknown quantity at this point
Quote:
Originally Posted by Left_blank View Post
Nope, that makes it a little different but now I see both hints at once for a millisecond on the transition as well as when I hover in and out too fast!

because your making it to fadeIn Fadeout fast make it.. fadeIn('slow'); and fadeOut('slow')
iamjerson is offline   Reply With Quote
Old 08-19-2010, 12:35 PM   PM User | #5
SB65
Senior Coder

 
Join Date: Feb 2009
Location: West Yorkshire
Posts: 2,817
Thanks: 9
Thanked 681 Times in 675 Posts
SB65 will become famous soon enoughSB65 will become famous soon enough
I suspect the problem with your original code is because when you move the mouse in and out quickly, the hoverIn fade has not completed before the hoverOut fade starts - resulting in both divs being visible.

Adding stop() as below seems to cure the problem.

Code:
$('#QmarkCOP').hover(
function(){
$('#PPHint').stop('true','true');
$('#DefaultHint').fadeOut('fast', function(){
$('#PPHint').fadeIn('fast');
})
},
function(){
$('#DefaultHint').stop('true','true');
$('#PPHint').fadeOut('fast', function(){
$('#DefaultHint').fadeIn('fast');
});
}
)
Interested in any alternative solutions.
SB65 is offline   Reply With Quote
Old 08-19-2010, 12:44 PM   PM User | #6
Left_blank
New Coder

 
Join Date: Jul 2010
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Left_blank is on a distinguished road
Edit: I got ninja replied on by SB65. This post is supposed to go just above his!


That doesn't fix it either I'm afraid. The problem is that there's nothing preventing the mouseout function from running at the same time as the mousein function when I move the mouse in and out of the image quickly. When both functions run at the same time, I see both hints at the same time.

So what I think I need to do is check if the mousein function is finished before I run the mouseout function somehow?

Last edited by Left_blank; 08-19-2010 at 01:22 PM..
Left_blank is offline   Reply With Quote
Old 08-19-2010, 01:04 PM   PM User | #7
many_tentacles
Regular Coder

 
Join Date: Dec 2005
Location: UK
Posts: 207
Thanks: 6
Thanked 2 Times in 2 Posts
many_tentacles is an unknown quantity at this point
not sure of exactly how the code would go, but i'd suggest using .mouseenter or .mouseover rather than .hover. Not sure which one though...

Take a look at this:
http://api.jquery.com/mouseover/
many_tentacles is offline   Reply With Quote
Old 08-19-2010, 01:30 PM   PM User | #8
Left_blank
New Coder

 
Join Date: Jul 2010
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Left_blank is on a distinguished road
@ SB65,

That does ease the problem somewhat but it doesn't eradicate it. When I flash the cursor back and forth over the image quickly I still see both divs at once sometimes. There are also instances of the functions being queued up in memory so if I flash over a lot of times and then move the mouse away there is still animations going on.

I'd guess I can add some sort of timer to the function so that nothing happens unless there is hover for a certain length of milliseconds. I'll try that and let you know how I get on.

Thanks.
Left_blank is offline   Reply With Quote
Old 08-19-2010, 02:27 PM   PM User | #9
SB65
Senior Coder

 
Join Date: Feb 2009
Location: West Yorkshire
Posts: 2,817
Thanks: 9
Thanked 681 Times in 675 Posts
SB65 will become famous soon enoughSB65 will become famous soon enough
This plugin looks like it might help you out.
SB65 is offline   Reply With Quote
Old 08-19-2010, 02:56 PM   PM User | #10
Left_blank
New Coder

 
Join Date: Jul 2010
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Left_blank is on a distinguished road
That looks like exactly what I'm looking for. I'll have a play around with it now in a minute.

Thus is as close as I got myself.

Code:
function doFade(fadeoutfield, fadeinfield){ //2
		$(fadeoutfield).fadeOut('fast', function(){ //1
			$(fadeinfield).fadeIn('fast');
		}) //1
	}

$('#QmarkCOP').mouseenter(function(event){
		TimerId = setTimeout ( "doFade('#DefaultHint', '#PPHint')", 500 )
	});
	
	$('#QmarkCOP').mouseleave(function(event){
		$('#DefaultHint').stop('true','true');
		if($('#PPHint').is(":visible")){
			doFade('#PPHint', '#DefaultHint')
		}
		else {
			clearTimeout(TimerId)
		}
	});
It's pretty hard to break this one unless you really try but it's not a perfect solution.
Left_blank is offline   Reply With Quote
Old 08-19-2010, 03:16 PM   PM User | #11
Left_blank
New Coder

 
Join Date: Jul 2010
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Left_blank is on a distinguished road
Yep that works perfectly SB. Excellent find. Thanks.
Left_blank is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:49 AM.


Advertisement
Log in to turn off these ads.