最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to fade to a different image on mouse over? - Stack Overflow

programmeradmin8浏览0评论

I have two images. I would like to configure them so that when you mouseover the default image, it slowly fades to the second image. How could one go about this?

Thanks!

I have two images. I would like to configure them so that when you mouseover the default image, it slowly fades to the second image. How could one go about this?

Thanks!

Share Improve this question asked Jul 1, 2011 at 0:25 BrandonBrandon 812 gold badges2 silver badges3 bronze badges 2
  • Check these answers: stackoverflow./questions/1857827/… – dertkw Commented Jul 1, 2011 at 0:33
  • 1 Have you thought about the necessary logic if someone moves their hand away from your image? Does it go back to the default? Does it continue fading? Does it fade back? I ask because to do this without the fading is very simple, with fading is much more code. – Kerry Jones Commented Jul 1, 2011 at 0:34
Add a ment  | 

3 Answers 3

Reset to default 1

I've assumed that you want to fade back in when you mouseout, here's something to be getting started with.

// markup
<div id="imgs">
    <img src="..." id="i1">  <!-- this is the mouseover image -->
    <img src="..." id="i2">  <!-- this is the default image -->
</div>


// css
img {
    display:block;
    position:absolute;
    top: 0;
    left: 0;
 }


// jQuery
$(function() {
    $('#imgs')
    .mouseenter(function() {
        $('#i2').fadeOut('slow');
    })
    .mouseleave(function() {
        $('#i2').fadeIn('slow');
    });  
});

Something like this should work:

$('#first_image').mouseover( function() { 
    $(this).fadeOut('fast', function() {
        $('#new_image').fadeIn('slow');
    }
}

This simply fades out the old image on mouse over, and once the fadeout is plete, fades in the new one.

You could use the JQuery .animate effect. The following tutorial helped me learn how to use it and shows exactly what you're looking for. An image that fades in on mouseover and then out on mouseout. http://bavotasan./tutorials/creating-a-jquery-mouseover-fade-effect/

发布评论

评论列表(0)

  1. 暂无评论