Image Control Manipulation in JavaScript
If you do not have an Image to work with or would like to follow along in the code as well, please feel free to download the free source code located below.
Once we have created and set an image in our HTML, we must have a source for that image. What if we went ahead and were able to change the source of that Image control without the need to reload the page.
First let’s go and declare that we will be using JavaScript within the HTML page by adding the appropriate script tag.
We used over 10 web hosting companies before we found Server Intellect. They offer dedicated servers, and they now offer cloud hosting!
<head>
<script type="text/javascript" language="javascript">
</script>
<title></title>
</head>
Now we can go ahead and change the Image by changing the src by using the “OnMouseOver” attribute in which we will be making our JavaScript call. We will be taking the document object and calling the image in question, the “the_image”. We will then be calling the src property of the Image and setting it to another URL or source.
We used over 10 web hosting companies before we found Server Intellect. Our new server with cloud hosting,was set up in less than 24 hours. We were able to confirm our order over the phone. They responded to our inquiries within an hour. Server Intellect’s customer support and assistance are the best we’ve ever experienced.
<img name="the_image"
src="sunset.jpg"
onmouseover="document.the_image.src ='photography_image.jpg';"
Now we are left with a dilemma, the image remains with the source of the new Image even with the mouse away from the Image. What we need to do to remedy this is set another Property to make an additional call, the “OnMouseOut”.
Similar to the “OnMouseOver”, we will now be creating an event at the occurrence of the mouse being removed from the Image control. Just as we did before, we will be setting the source back to the original Image like so:
<img name="the_image"
src="sunset.jpg"
onmouseover="document.the_image.src ='photography_image.jpg';"
onmouseout="document.the_image.src='sunset.jpg';"/>
We stand behind Server Intellect and their support team. They offer dedicated servers, and they are now offering cloud hosting!
JSImageTut.zip
One Response
6.3.2011
is there a way to add a fade in fade out effect?