-2

I've got the following code:

var clicked = false;
var klick = false;
$("img#hovertom").click(function() {
    if (clicked) clicked = true;
    else $(this).attr("src", "../img/tom_effects.png");
    if (clicked = true) $(this).attr("src", "../img/tom_effects.png");
    if (clicked = true) $("img#hoverdaniel").attr("src", "../img/daniel.png");
    if (clicked = true) klick = false;
});
$("img#hovertom").hover(function() {
    if (!clicked) $(this).attr("src", "../img/tom_hover.png");
}, function() {
    if (!clicked) $(this).attr("src", "../img/tom.png");
});
$("img#hoverdaniel").click(function() {
    if (klick) klick = true;
    else $(this).attr("src", "../img/daniel_effects.png");
    if (klick = true) $(this).attr("src", "../img/daniel_effects.png");
    if (klick = true) $("img#hovertom").attr("src", "../img/tom.png");
    if (klick = true) clicked = false;
});
$("img#hoverdaniel").hover(function() {
    if (!klick) $(this).attr("src", "../img/daniel_hover.png");
}, function() {
    if (!klick) $(this).attr("src", "../img/daniel.png");
});

There are 2 images with each 3 versions, they change on hover and click. And if you click the other image. It works fine, but the image changes immediately. Can anyone rewrite the code that the images fadein fadeout or fadeto, simply that the images are 'animated' when they change.

Thx

Tom Te
  • 11
  • 1
  • 4

2 Answers2

0

mmm, maybe something like:

$(this).fadeOut().attr("src", "the_path").fadeIn();

I've not tested it but give a try

Aguardientico
  • 7,641
  • 1
  • 33
  • 33
0

As they told you, you can simulate the transition by using fadeIn and fadeOut, although is not a real transition between images as it only hides one and shows other without any real transition between the images but with the background:

$(this).fadeOut('slow', function(){
     $(this).attr('src', 'YOUR_PATH').fadeIn();
});

Living example: http://jsfiddle.net/8hLMB/

Alvaro
  • 40,778
  • 30
  • 164
  • 336
  • Maybe you can write in for my code? I don't get how to do it in this code. I'm a jQuery noob ;) – Tom Te Jul 02 '13 at 17:24