<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>buyco.ca &#187; hover</title>
	<atom:link href="http://www.buyco.ca/tag/hover/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.buyco.ca</link>
	<description>because someone stole my name</description>
	<lastBuildDate>Fri, 23 Jul 2010 21:35:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Unobtrusive Image Rollover</title>
		<link>http://www.buyco.ca/2008/06/19/unobtrusive-image-rollover/</link>
		<comments>http://www.buyco.ca/2008/06/19/unobtrusive-image-rollover/#comments</comments>
		<pubDate>Thu, 19 Jun 2008 21:13:59 +0000</pubDate>
		<dc:creator>Buyco</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[addEventListener]]></category>
		<category><![CDATA[attachEvent]]></category>
		<category><![CDATA[hover]]></category>
		<category><![CDATA[mouseover]]></category>
		<category><![CDATA[unobtrusive]]></category>

		<guid isPermaLink="false">http://www.buyco.ca/?p=23</guid>
		<description><![CDATA[Learn how to add some spice to your site! Unobtrusive programming should be common practice, and what better way to start than here.]]></description>
			<content:encoded><![CDATA[<p>As web developers we spend so much time trying to make our sites cutting edge yet still functional with older browsers. It&#8217;s a curse! How great would it be to have a web browser standard that everyone could adhere to. Too bad life isn&#8217;t that simple. In today&#8217;s example we&#8217;re going to create a simple javascript image rollover. Yes, I know we&#8217;ve all done it before, but today we&#8217;ll do it right: unobtrusively.</p>
<p>There are many ways to do this, but in this example we&#8217;re just going to do a simple image replacement on mouseover. We&#8217;ll start off with the code for the image:</p>
<pre>&lt;img src="/images/how-to/javascript/button-inactive.gif" id="button" /&gt;</pre>
<p>For your SEO enthusiasts and W3C fiends, I know there are missing attributes. We&#8217;re going to go as minimalist as possible to simplify this process. One attribute to note of is the &#8220;id&#8221; which we&#8217;ll be using to select this particular image. This is very important so that we can select this image element based on it&#8217;s id (DOM FTW!).</p>
<p>Next we want to apply an event handler to that object so that when the user moves the cursor over it, the image will change into something pretty! Internet explorer uses a different type of event handler code than other browsers (because if it didn&#8217;t, this would be way too easy). So we will ensure we do a little browser detection:</p>
<pre>var isMoz;
window.addEventListener ? isMoz = true : isMoz = false;</pre>
<p>The window.addEventListener method is not used by Internet Explorer, so here we just single those browsers out. Next we want to create the code to attach a function to the mouseover (or onmouseover) event of the button. We haven&#8217;t created the image swapping functions yet, but keep in mind that we&#8217;ll call them imgOver and imgOut. That being said, lets create the event listeners and put them in a function called initMouseOver:</p>
<pre>function initMouseOver() {
 var objImage = document.getElementById('button');
 if (isMozilla) {
  objImage.addEventListener('mouseover', imgOver, false);
  objImage.addEventListener('mouseout', imgOut, false);
 }
 else {
  objImage.attachEvent('onmouseover', imgOver);
  objImage.attachEvent('onmouseout', imgOut);
 }
}</pre>
<p>Now you see why the image&#8217;s ID is important (remember: DOM FTW!). In the above snippet, when the function is called, event listeners are added to the image object. In addEventListener and attachEvent, the first value passed in is called the event type. For a mouse over event, these are valued at mouseover and onmouseover. The second value is the function to be associated with the event. In addEventListener the last value taken in is a boolean value which indicates whether or not to bind the event as it propogates to the image or as the event bubbles upwards from the image. This is usually set to false.</p>
<p>Next we need to create the functions to swap the images in and out as the object captures the events:</p>
<pre>function imgOver(e) {
 var eMoz = 'target';
 var eIE = 'srcElement';
 var objImgOver = new Image();
 objImgOver.src = '/images/how-to/javascript/button-active.gif';
 e[eMoz] ? e[eMoz].src = objImgOver.src : e[eIE].src = objImgOver.src;
}
function imgOut(e) {
 var eMoz = 'target';
 var eIE = 'srcElement';
 var objImgOut = new Image();
 objImgOut.src = '/images/how-to/javascript/button-inactive.gif';
 e[eMoz] ? e[eMoz].src = objImgOut.src : e[eIE].src = objImgOut.src;
}</pre>
<p>In both functions, we&#8217;re initializing the new image, then swapping it. This is not the most efficient way to do this since we&#8217;re recreating a new image object every time an event occurs. So let&#8217;s clean up all the code and put it into a separate javascript file. Here&#8217;s how it should look:</p>
<pre>var isMoz;
window.addEventListener ? isMoz = true : isMoz = false;
var eMoz = 'target';
var eIE = 'srcElement';
var objImgOver = new Image();
objImgOver.src = '/images/how-to/javascript/button-active.gif';
var objImgOut = new Image();
objImgOut.src = '/images/how-to/javascript/button-inactive.gif';

function initMouseOver() {
 var objImage = document.getElementById('button');
 if (isMozilla) {
  objImage.addEventListener('mouseover', imgOver, false);
  objImage.addEventListener('mouseout', imgOut, false);
 }
 else {
  objImage.attachEvent('onmouseover', imgOver);
  objImage.attachEvent('onmouseout', imgOut);
 }
}
function imgOver(e) {
 e[eMoz] ? e[eMoz].src = objImgOver.src : e[eIE].src = objImgOver.src;
}
function imgOut(e) {
 e[eMoz] ? e[eMoz].src = objImgOut.src : e[eIE].src = objImgOut.src;
}</pre>
<p>*Deep breath* still with me? That&#8217;s essentially it! All you need to do now is to include the javascript file in your HTML and ensure the initMouseOver() function is called when the page loads. Simply add onload=&#8221;" to your body tag and it should look a little something like this:</p>
<pre>&lt;body onload="javascript:initMouseOver();"&gt;</pre>
<p style="text-align: center;"><img id="button" class="aligncenter" src="/images/how-to/javascript/button-inactive.gif" alt="" /></p>
<p>That&#8217;s it! No wait&#8211;that&#8217;s only for one image. What if we need to set this up for multiple images? Well, glad I asked because there are several ways of accomplishing this. If you think you can take more of this, carry on to one of the next tutorials or download all the work files for this one:</p>
<blockquote><p><a title="Example: Unobtrusive Image Rollover" href="http://www.buyco.ca/downloads/how-to/javascript/example-unobtrusive-javascript-rollover.zip" target="_blank">Example: Unobtrusive Image Rollover</a></p></blockquote>
<p>Next step: multiple buttons, preloading, and optimization</p>
]]></content:encoded>
			<wfw:commentRss>http://www.buyco.ca/2008/06/19/unobtrusive-image-rollover/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
