开发者

FadeIn FadeOut with Jquery with a twist

开发者 https://www.devze.com 2022-12-10 09:28 出处:网络
I am trying to create a hover over action which brings in a coloured image and also once the hover is removed it fades back to its original image.

I am trying to create a hover over action which brings in a coloured image and also once the hover is removed it fades back to its original image.

I got it to the point of fading in the first image with the help Funka and Brad on this forum however i need to get it so it fades out once you hover off.

Currently it fades out the image to nothing and then fades the new one in. This will then stay in place regardless of whether i hover o开发者_运维技巧ff or no.

Id like it so it seems like the colour image is fading through the black and white one as apposed to fading to 0 before fading in...as well as reverting once the hover is removed.

Any help would be appreciated.

//Loop through the images and print them to the page
   for (var i=0; i < totalBoxes; i++){
    $.ajax({
     url: "random.php?no=",
     cache: false,
     success: function(html) {
      // following line I originally suggested, but let's make it better...
      //$('#bg').append(html).fadeIn('slow');
      // also note the fine difference between append and appendTo.
      var $d = $(html).hide().appendTo('#bg').fadeIn('slow');
      $('img', $d).hover(function() {
       var largePath = $(this).attr("rel");
       $(this).fadeOut("slow", function() {
        $(this).attr({ src: largePath }).fadeIn("slow");
       });
      });
     }
    });
   }


Your hover only has a mouseover function - to do something on mouseout...

$('img', $d).hover(function() {
    //This is the mouseover function
    var largePath = $(this).attr("rel");
    $(this).fadeOut("slow", function() {
        $(this).attr({ src: largePath }).fadeIn("slow");
    }
    );
},
function() {
    //This is the mouseout function!  Do something here!
});


I don't really know jQuery but the following code if what I have been using and sounds like what you might be after. I use it with sprite images to stop the annoying flicker that appears in some browsers.

$(function() {
    $(".fadebtn")
    .find("span")
    .hide()
    .end()
    .hover(function() {
            $(this).stop(true, true).find("span").fadeIn(600);
    }, function() {
            $(this).stop(true, true).find("span").fadeOut(200);
    });
});
0

精彩评论

暂无评论...
验证码 换一张
取 消