Be a Supporter!

A javascript question

  • 275 Views
  • 4 Replies
New Topic Respond to this Topic
skylinegodzilla
skylinegodzilla
  • Member since: Sep. 9, 2008
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
A javascript question 2010-08-10 20:47:16 Reply

Hey dose any one know how I can program javascript to make images fade in and out when I hover over Hyperlinks?

So like there is a menu along the top of the page and each time I hover over a hyperlink in the menu the image that is somewhere else on the page will change?

I would really like some help on this.

(Do not suggest me to do it in flash I know it would be easier but I am not aloud to use flash in this project)


BBS Signature
Jon-86
Jon-86
  • Member since: Jan. 30, 2007
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
Response to A javascript question 2010-08-10 20:54:04 Reply

It would be pointless doing it in flash as flash is seporate from the DOM and you would need to use some sort of JS interface to communicate the mouseover event into flash in order for flash to do something.

You can use fadeIn to achieve this effect quite easily.


PHP Main :: C++ Main :: Java Main :: Vorsprung durch Technik
irc.freenode.net #ngprogramming

BBS Signature
fourthfrench
fourthfrench
  • Member since: Aug. 13, 2009
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to A javascript question 2010-08-12 17:15:51 Reply

At 8/10/10 08:54 PM, Jon-86 wrote: You can use fadeIn to achieve this effect quite easily.

Do this first. If you don't know how to use jquery (or don't want to), you can do something like what I did below (I hacked it together quickly, doesn't work in IE).

window.fading = null;
			function fadeInit(id,fadein)
			{
				clearTimeout(window.fading);
				elem = document.getElementById(id);
				v=parseFloat(elem.style.opacity);
				if(fadein&&v<1 || !fadein&&v>0)
					fade(elem,v,fadein?.1:-.1);
			}
			function fade(elem,now,speed)
			{
				elem.style.opacity = (now+speed);
				if(speed>0&&now<1 || speed<0&&now>0)
					window.fading = 
						setTimeout("fade(elem,"+(now+speed)+","+speed+")",10);
				else
					clearTimeout(window.fading);
			}
...
		<img style="opacity:1" src="img.jpg" id="myimg">
		<a onmouseover="fadeInit('myimg',true)" href="javascript:void">Fade in</a>
		<br>
		<a onmouseover="fadeInit('myimg',false)" href="javascript:void">Fade out</a>
Mich
Mich
  • Member since: Jan. 12, 2008
  • Offline.
Forum Stats
Member
Level 43
Musician
Response to A javascript question 2010-08-12 20:33:19 Reply

At 8/12/10 05:15 PM, fourthfrench wrote:
At 8/10/10 08:54 PM, Jon-86 wrote: You can use fadeIn to achieve this effect quite easily.
Do this first. If you don't know how to use jquery (or don't want to), you can do something like what I

I personally think it's a much better idea to just use jQuery, as it would also work in IE, and eventually, using jQuery all the time will open up a much more FUN javascript programming experience.

I've personally kept from using jQuery for a long while, as I was being a purist, doing everything myself, basically. When I decided to try it out, I felt dumb for not doing so right away.

fourthfrench
fourthfrench
  • Member since: Aug. 13, 2009
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to A javascript question 2010-08-12 22:23:58 Reply

At 8/12/10 08:33 PM, Mich wrote: I personally think it's a much better idea to just use jQuery

I completely support using a framework instead. I just wanted to give the guy another option, since switching to a framework is a little daunting.