<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-64508075744045865</id><updated>2012-02-15T23:13:54.342-08:00</updated><category term='dev tips'/><category term='css'/><category term='javascript'/><title type='text'>tommywhoops</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://tommywhoops.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/64508075744045865/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://tommywhoops.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>tommywhoops</name><uri>http://www.blogger.com/profile/14411643018728616776</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='27' src='http://3.bp.blogspot.com/_ibdmAAgGxMw/Sl1mLgLZrYI/AAAAAAAAAAY/xofEwU7iyDI/S220/tom.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-64508075744045865.post-9030508358028827003</id><published>2009-07-17T09:11:00.000-07:00</published><updated>2009-07-17T10:19:03.065-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='javascript'/><category scheme='http://www.blogger.com/atom/ns#' term='dev tips'/><category scheme='http://www.blogger.com/atom/ns#' term='css'/><title type='text'>The Easiest Darn Image Swap You Could Ask For</title><content type='html'>Eek, first blog post to the development community of the web! I figured I'd show something to beginner/intermediate level developers that I discovered and found quite useful, and am starting to use in pretty much every website project I develop.&lt;br /&gt;&lt;br /&gt;Image swapping in menu's is one of the most commonly used practices in any website. It's pretty basic and is probably something we've all done 100 times. However I've noticed that when searching Google for "image swap", the results that come up are pretty old methods, or are just crazy javascript generating apps that will give you 20 lines of code to do a simple image swap effect.&lt;br /&gt;&lt;br /&gt;Ever try it in Dreamweaver? Here's a small snippet of the nonsense generated for you:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://www.tommywhoops.com/blog/images/imageswap/ul_DWcode.gif"&gt;&lt;img src="http://www.tommywhoops.com/blog/images/imageswap/ul_DWcode.gif" alt="" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;That's just a part of it, and there's already 3 functions full of variables and for loops. There are easier ways, people! Here's how we do it:&lt;br /&gt;&lt;br /&gt;We'll look at the example from my website. Here's my two "Resume" images from my navigation.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_ibdmAAgGxMw/SmCl9fwUvGI/AAAAAAAAABA/SqCYxGMeqs4/s1600-h/ul_twoImages.gif"&gt;&lt;img src="http://4.bp.blogspot.com/_ibdmAAgGxMw/SmCl9fwUvGI/AAAAAAAAABA/SqCYxGMeqs4/s320/ul_twoImages.gif" alt="" id="BLOGGER_PHOTO_ID_5359466032485678178" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;So instead of actually swapping the images with an onMouseOver event, what we do instead is combine the two images together on top of each other so we can use them as a background image:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_ibdmAAgGxMw/SmCmTvzijPI/AAAAAAAAABI/aZa1cAV3ulw/s1600-h/ul_newBGImage.gif"&gt;&lt;img src="http://3.bp.blogspot.com/_ibdmAAgGxMw/SmCmTvzijPI/AAAAAAAAABI/aZa1cAV3ulw/s320/ul_newBGImage.gif" alt="" id="BLOGGER_PHOTO_ID_5359466414751255794" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;So now in my source code, we create a div just big enough to display the top half of the image, and give that div a class where we use the image as a background image using the background-image attribute in CSS.&lt;br /&gt;&lt;br /&gt;Here's the html:&lt;br /&gt;&lt;br /&gt;&lt;img src="http://tommywhoops.com/blog/images/imageswap/ul_htmlCodeStart.gif" style="" 0px="" auto="" /&gt;&lt;br /&gt;&lt;br /&gt;And here's the CSS to go with it:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_ibdmAAgGxMw/SmCo_hbU3RI/AAAAAAAAABo/ZVRDnVTLTc4/s1600-h/ul_cssStart.gif"&gt;&lt;img src="http://1.bp.blogspot.com/_ibdmAAgGxMw/SmCo_hbU3RI/AAAAAAAAABo/ZVRDnVTLTc4/s400/ul_cssStart.gif" alt="" id="BLOGGER_PHOTO_ID_5359469365829098770" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;So now what we are going to do is create the effect that when a user rolls over this div, the background image is going to jump up into a new background position so we only see the bottom half of our background image instead of the top half. For this we just need two very small functions and one more CSS style for the .resumeLink hover style.&lt;br /&gt;&lt;br /&gt;Here's the two functions:&lt;br /&gt;&lt;br /&gt;&lt;img src="http://tommywhoops.com/blog/images/imageswap/ul_functions.gif" style="" 0px="" auto="" /&gt;&lt;br /&gt;&lt;br /&gt;And now add the two functions to your initial html code:&lt;br /&gt;(formatted slightly different so it fits across this page)&lt;br /&gt;&lt;br /&gt;&lt;img src="http://tommywhoops.com/blog/images/imageswap/ul_htmlCode.gif" style="" 0px="" auto="" /&gt;&lt;br /&gt;&lt;br /&gt;What the functions are doing is forcing a class change on the div. In the image above of the functions you can see hoverOver appends "hover" to your class name. And then hoverOut removes the "hover" so it goes back to the original class name. So there's still one more thing to do which is to actually create the hover class in our CSS.&lt;br /&gt;&lt;br /&gt;The hover class looks like this:&lt;br /&gt;&lt;br /&gt;&lt;img src="http://tommywhoops.com/blog/images/imageswap/ul_css.gif" style="" 0px="" auto="" /&gt;&lt;br /&gt;&lt;br /&gt;The hover class for .resumeLink is just one line of CSS that bumps our background image up 34 pixels (half the height of background image, and exactly the height of the part of the image I want to show for the swap effect).&lt;br /&gt;&lt;br /&gt;A couple more things I want to mention, as you can see in the html source code, I've put the actual anchor tag on the #resume div itself. The #resume div is just to declare the size of the menu item. It's the two classes that are swapping and changing via the functions in our javascript file.&lt;br /&gt;&lt;br /&gt;Also, if you get funky behavior such as the hover state flowing all the way across your site like in the image below, it's because the container div (#resume) needs to be floated left. Without the float attribute you'll see the image swapping if you're anywhere to the left or right of it instead of only when rolling directly over the image.&lt;br /&gt;&lt;br /&gt;&lt;img src="http://tommywhoops.com/blog/images/imageswap/ul_floatIssue.gif" style="" 0px="" auto="" /&gt;&lt;br /&gt;&lt;br /&gt;So there's you're super easy image swap. I recently worked on a site where you can see another example of this working (&lt;a href="http://www.pstnet.com/software.cfm?ID=54" target="_blank"&gt;pstnet.com software page&lt;/a&gt;) where a background image of 4 rollover effects is used. Here's an example of the background image used:&lt;br /&gt;&lt;br /&gt;&lt;img src="http://tommywhoops.com/blog/images/imageswap/ul_roadmap.png" style="" 0px="" auto="" /&gt;&lt;br /&gt;&lt;br /&gt;So you can see by clicking on the link above and rolling over the horizontal arrow roadmap that the image above is changing it's background position between 4 different states, not just 2.&lt;br /&gt;&lt;br /&gt;Questions or comments, please feel free to post or e-mail me directly!&lt;br /&gt;&lt;br /&gt;-tw&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/64508075744045865-9030508358028827003?l=tommywhoops.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tommywhoops.blogspot.com/feeds/9030508358028827003/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://tommywhoops.blogspot.com/2009/07/easiest-darn-image-swap-you-could-ask.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/64508075744045865/posts/default/9030508358028827003'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/64508075744045865/posts/default/9030508358028827003'/><link rel='alternate' type='text/html' href='http://tommywhoops.blogspot.com/2009/07/easiest-darn-image-swap-you-could-ask.html' title='The Easiest Darn Image Swap You Could Ask For'/><author><name>tommywhoops</name><uri>http://www.blogger.com/profile/14411643018728616776</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='27' src='http://3.bp.blogspot.com/_ibdmAAgGxMw/Sl1mLgLZrYI/AAAAAAAAAAY/xofEwU7iyDI/S220/tom.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_ibdmAAgGxMw/SmCl9fwUvGI/AAAAAAAAABA/SqCYxGMeqs4/s72-c/ul_twoImages.gif' height='72' width='72'/><thr:total>0</thr:total></entry></feed>
