Wallpapers Forums Login

How To Show Picture On Mouseover With Javascript Tutorial

today i was asked how you can make a picture appear when you put the mouse arrow on a link. for example, i have a link and i when someone puts their mouse arrow on top of it, it would show a picture. well, that's easy to do using HTML, CSS and Javascript. i will show you how you can do this.

first, we are going to create an html file called javascript-showpicture.html to create this file, i will be using a text editor. i am using windows xp to do this tutorial, so i will use notepad for my text editor.

the first thing we are going to need is the style, i will just simple create a box with a grey thin line border, and to make sure that the image shows right next to the link, i will use the absolute position so here is the CSS code i would use:

<style type="text/css">
#Style {
	position:absolute; 
	visibility:hidden;
	border:solid 1px #CCC;
	padding:5px;
	
}
</style>


ok, so i got my style, now i need the javascript code: we are going to use this code, this code will make it so that when you put the mouse over the link, it will show the image, but as soon as you move the mouse arrow outside the link, we want the picture to hide away. so we create a ShowPicture() Function using Javascript code:
<script language="Javascript">
<!--
function ShowPicture(id,Source) {
if (Source=="1"){
if (document.layers) document.layers[''+id+''].visibility = "show"
else if (document.all) document.all[''+id+''].style.visibility = "visible"
else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "visible"
}
else
if (Source=="0"){
if (document.layers) document.layers[''+id+''].visibility = "hide"
else if (document.all) document.all[''+id+''].style.visibility = "hidden"
else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "hidden"
}
}
//-->
</script>


ok, we have our CSS and Javascript, now we need the HTML to put it all together. we are going to be using a div to show the image and a href link to allow the user to put their mouse over the link so this is the HTML we will need to use:

<a href="#" onMouseOver="ShowPicture('Style',1)" onMouseOut="ShowPicture('Style',0)">Click Here To Show Image</a>
<div id="Style"><img src="http://www.wallpaperama.com/post-images/forums/200901/11-880-aaaa.jpg"></div>
thats it, now we put it all together, open a blank we are going to copy and past the following code into it:
<h1>Show Picture on Mouseover by Wallpaperama.com</h1>
<script language="Javascript">
<!--
function ShowPicture(id,Source) {
if (Source=="1"){
if (document.layers) document.layers[''+id+''].visibility = "show"
else if (document.all) document.all[''+id+''].style.visibility = "visible"
else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "visible"
}
else
if (Source=="0"){
if (document.layers) document.layers[''+id+''].visibility = "hide"
else if (document.all) document.all[''+id+''].style.visibility = "hidden"
else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "hidden"
}
}
//-->
</script>
<style type="text/css">
#Style {
	position:absolute; 
	visibility:hidden;
	border:solid 1px #CCC;
	padding:5px;
	
}
</style>

<a href="#" onMouseOver="ShowPicture('Style',1)" onMouseOut="ShowPicture('Style',0)">Click Here To Show Image</a>
<div id="Style"><img src="http://www.wallpaperama.com/post-images/forums/200901/11-880-aaaa.jpg"></div>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<div><a href="http://www.wallpaperama.com">&lt;&lt; Visit Wallpaperama.com For More Cool Javascript Tricks</a></div>
now save it as javascript-showpicture.html

once you have saved it, open javascript-showpicture.html with your browse and you can see it in action

ok, the best way to know what you are getting is to see what it looks like, you have two options, create the file youself and see it. i would prefer you do it that way, its more fun. but if you are lazy, i will create a demo for you. here it is: hope this helps
Put Mouse Here To Show Image










junior Tue May 01, 2012
Face the word that the image will appear when the mouse has not incorporated the above text is only two lines below the end of the sentence. to I solve this?
See how it looks ..
junior Thu Apr 19, 2012
I have a blog and I put this code in HTML / javascript appeared in the image, so it appears on my notepad (notepad in this code works well). I have to add something to the HTML of the blog page?
Thank you for your attention
Tyla Mon Apr 16, 2012
I have noticed that many have had the same problem as me and got no reply on how to fix it. The question is HOW DO I APPLY THIS TO MORE THEN ONE IMAGE ON MY PAGE. IT With Regards to this comment:

DOES NOT WORK? well here is the answer I found very similar code on another website just use it and it works fine. You can have as many mouseovers on one page as you like.. I hope this helps for all of you looking for a solution.

Plea could you give us the link...

Thanks
junior Mon Apr 16, 2012
eu tenho um blog e eu coloquei esse código em HTML/javascript na apareceu a imagem,so aparece no meu bloco de notas(no bloco de notas esse código dá certo).Eu tenho que adicionar alguma coisa ao HTML da página do blog?
obrigado pela atenção
holdenrussell Mon Mar 19, 2012
You said that you have a link to apply this to more than one image. Do you still have that link? (I don't see it)
mboadi Tue Mar 13, 2012
Thanx alot. It works fine for me but not compatible with pdf. I would like to see a preview of the pdf on mouseover too. Pleas help me fix this.

thank you
Erwin Fri Mar 02, 2012
I have the same problem, can you give the link with your solution please.
pixel Tue Feb 21, 2012
Yes, I have the same question... but I don't see the solution that was found by one80
ONE80 Mon Jan 30, 2012
I have noticed that many have had the same problem as me and got no reply on how to fix it. The question is HOW DO I APPLY THIS TO MORE THEN ONE IMAGE ON MY PAGE. IT DOES NOT WORK? well here is the answer I found very similar code on another website just use it and it works fine. You can have as many mouseovers on one page as you like.. I hope this helps for all of you looking for a solution. houstoni
peter Mon Jan 16, 2012
Awesome - so simple yet effective. By the way - how do I change the position(I would like to move the image slightly down and right by let's say 4 pixels)
nupz Wed Oct 05, 2011
I want to make a list of names. First picture show fine but when i move mouse the name on the bottom, the upper picture is blinking and not goes away. What i have to do?
tara Sun Sep 18, 2011
Try looking for position attributes in css. w3schools will be a good source.
John Sun Aug 14, 2011
What this is.....
It's just making a new html instead of showing a picture in an excisting page...I want a to show a picture in the same page when I move over a text..e.g. when the text is Chuck...I want to display his picture when the mouse is over this text...

Making a new page...is nothing....
Kannan Wed Aug 03, 2011
hi friends,,,,,,,,, i need how to show image from database by onmouseuover image script......
neeraj Sat Jul 02, 2011
thanks bro...............yup

Related Content

Information

Share

wallpaperama | Wallpapers | Forums | Terms Of Service

copyright © 2013 wallpaperama - All Rights Reserved - Last Updated Mon May 06, 2013 (-8 GMT)
Powered by: Webune Forums V5