December 2, 2008

How to show a different image on mouse hover


Recently I have posted an article on how to create animated gif images, which although makes animated images, but those images change their frames automatically, but here's a method, where you can control when to change the frame, that means, the picture is changed, only when mouse cursor is placed on the image. See an example below (Place the mouse cursor on the image):

This method is quite easy,
For this method there are two steps:

Firstly you have to add some code to your template, to do that
Go to "Layout" section, then click on "Edit HTML",
Now find this code
</body> (it will be at the bottom)

Then immediately BEFORE this tag, paste this code:
<script type="text/javascript">
//<![CDATA[

function pat_getByClass(c){
ems=new Array();e=document.getElementsByTagName('*');pattern=new RegExp('(^|\\\\s)'+c+'(\\\\s|$)');
for(i=0,j=0;i<e.length;i++)if(pattern.test(e[i].className)){ems[j]=e[i];j++;}
return ems;
};

function ps_flipit(){
var c = pat_getByClass('flipit');
for(i=0;i<c.length;i++){
c[i].firstChild.onmouseover=function(){
this.style.display='none';
this.parentNode.lastChild.style.display='';
};
c[i].lastChild.onmouseout=function(){
this.style.display='none';
this.parentNode.firstChild.style.display='';
};
}
}

ps_flipit();

//]]>
</script>
After doing this, save the template.

Second step:
Now onwards, while posting any article, whenever you want to show this effect, upload both the images, and the links should be embedded properly as instructed below:
<table border="1">
<tr> <td><center><div class="flipit">
<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="FIRST_IMAGE_TARGET_LINK_HERE"><img style="cursor:pointer; cursor:hand;" src="FIRST_IMAGE_SOURCE_LINK_HERE" border="0" alt=""id="PHOTO_ID" /></a><a style="display:none" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="SECOND_IMAGE_TARGET_LINK_HERE"><img style="cursor:pointer; cursor:hand;" src="SECOND_IMAGE_SOURCE_LINK_HERE" border="0" alt=""id="PHOTO_ID" />
</a></div>
</center></td> </tr>
</table>

Replace the lines in RED and BLUE color, with appropriate links.
And enjoy the effects...


15 comments:

Anonymous said...

im sorry, but is the 2nd step still on the "edit HTML" section (edit layout)? or on the posts section (while creating the post)?

Anonymous said...

i think the code isnt working :(

Divya Sai said...

The first step is the modification of BLOG TEMPLATE [CSS modification]

The second step is to be done, when writing the ACTUAL POST/ARTICLE !

Danoxs said...

This is great Divya ^^

The '50% transparent trick' is a better version of this, but for just one image, isn't it ?

Revista Mombaça said...

Hello!

Thank you for your blog. What is the difference between the image target link and the source link? I have uploaded my image to phobucket and I just have a direct link. Thank you from Spain.

Divya Sai said...

Hey there!

Try a easier method: HTML ROLLOVER BUTTONS
Although in the tutorial I have mentioned buttons/small images, but you can certainly use that code for larger pictures too..

Revista Mombaça said...

Hello again and thank you. So, must I put the direct link that photobucket gives me instead of IMAGE1? I can't get it to work. I have used the buttons, though. Could you give me a hand with this? Just not sure what to put where it says image1 and image 2, thank you very much.

Divya Sai said...

okay....

firstly, to host images, use tinypic.com (that is owned by photobucket too, but this one gives unlimited bandwidth, moreover you easily get the direct links for your images)

Upload 2 images, paste the direct links here in this comment thread, I'll give you the exact code to be used in your blog.

Revista Mombaça said...

Hello again,

I can't copy or paste inside this box. Is that normal?

Thank you.

Divya Sai said...

Yeah that's normal if you are using firefox 3.5.xx
read this post to solve that issue (just notice that screenshot) to separate this comment-form from this page, and then you'll be able to paste into it:
Unable To Paste Text While Commenting In Blogger Blogs ? [Problem Fixed]

Revista Mombaça said...

Ok,

at last! Here are my two direct links from

IMAGE1
http://i48.tinypic.com/30wl64m.jpg


IMAGE2

http://i46.tinypic.com/jai6px.jpg

I understand you will give me an example of how to do it for future images.

Thank you very much for your help, I am really learning with this blog.

Divya Sai said...

okay, This is the exact code you have to use:

<a href="http://bloggerstop.net"><img width="231" height="350" src="http://i48.tinypic.com/30wl64m.jpg" onmouseover="this.src='http://i46.tinypic.com/jai6px.jpg'" onmouseout="this.src='http://i48.tinypic.com/30wl64m.jpg'" alt="Click Me"/></a>

change the values in href and alt.

It is better if you use mention the width and height parameters every time you do this (I have already done it this time) - so it is better if you use images of almost same size (in this case width of first images is 241px and that of 2nd image is 231px, so I have taken 231px for both of them, to give an overall uniform look)

Revista Mombaça said...

Ok, great. Tnanks! Two last questions then: can I do this with the same code on side columns? And, more important: inside a post, how could I get the text to be alongside the photo (photo to the right, for example)? The text is either under or on top, but not on the side. Thank you very much.

Divya Sai said...

Yes you can do this in the sidebar too. Simply add a html/javascript widget and paste the code in to it.

And to align a pic to left/right, do this:
Normally a code looks like this:

<a><img src=""/></a>

So add this code to it:

<a><img style="float:right;" src=""/></a>

easy gov grants said...

Absolutely beautiful...

:)) ;)) ;;) :D ;) :p :(( :) :( :X =(( :-o :-/ :-* :| 8-} :)] ~x( :-t b-( :-L x( =))

comment on this post Share Your Views

Allowed HTML tags: <b>, <i> and <a>

Join and share your views with other bloggers at BloggerStop Community

Comment Like This! and Optimize your blog...

SPAMMER OF THE MONTH: www[.]MensItaly[.]com

Next Post Homepage Previous Post

Regular Readers


What They Say...

Divya Sai is the ingenious webmaster of Bloggerstop.Net. Divya is extremely patient, knowledgeable and responsive... Bloggerstop.net has an abundance of articles on creating widgets, templates, hacks and ways to make your blog search engine friendly.
- Stacey @Pets With Allergies
I stumbled onto a great site. Sai, over at Bloggerstop.net has helped me tremendously and with great patience...for any and all questions regarding computer issues. And again, THANK YOU Sai.
- Cheryl @Cmash Loves To Read

More Here...

Popular Posts

Get this widget

| © 2010 BloggerStop.net | Blogger Widgets | Template by Dicas Blogger | Enhanced By Divya Sai | Privacy Policy