Hover on text to change image with fade


I have a series of text links, which when rolled over, would cause a specific image link to fade in; that image would not fade/change to a different image until a different text link was rolled over.

I did find some basic script that does this, albeit without a fade affect when the image changes:

<script language="javascript">
function roll(img_name1, img_src1)
document[img_name1].src = img_src1;

<li><a href="1.htm" onMouseOver="roll('poster', '1.png')">111</a>
<li><a href="2.htm" onMouseOver="roll('poster', '2.png')">222</a>
<li><a href="3.htm" onMouseOver="roll('poster', '2.png')">333</a>


<p align="center"><img src="1.png" name="poster"></p> 

A perfect example is http://sharifabraham.com/projects/, which I discovered in a similar post: hover on text link to change image. I’ve tried to edit and use the code in both of the aforementioned links, but to no avail. Any insight on what to do is much appreciated.


I’ve dropped the inline JS code since it’s not the prefered way.

I’ve added a data-attribute to the lis which contain the image to load.

$(document).ready(function() {
  $('ul.image-switch li').mouseover(function() {
    var image_src = $(this).data('image');
    var img = $('.image-container img');
    if (img.attr('src') != image_src) { // only do the fade if other image is selected
      img.fadeOut('slow', function() { // fadeout the current image
        img.attr('src', img).fadeIn(); // load and fadein new image

<ul class="image-switch">
  <li><a href="1.htm" data-image="1.png">111</a>
  <li><a href="2.htm" data-image="2.png">222</a>
  <li><a href="3.htm" data-image="2.png">333</a>


<p align="center" class="image-container"><img src="1.png" name="poster"></p> 


Sorry made a type.

DEMO: http://jsfiddle.net/yVcV3/1/



Answered By – PeeHaa

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published