javascript - How to addClass and removeClass with fade via jQuery? - Stack Overflow

I'm trying to fade one image into another.I tried the CSS route by making the top image transpar

I'm trying to fade one image into another. I tried the CSS route by making the top image transparent on hover, but that's not going to work in my case. In my case, on hover, I'm trying to fade my logo into my logo with a glow. The logos aren't a perfect shape, so having the two pictures on top of one another is redundant, since the user can see the "over" picture, aka logo with a glow, without hovering over it.

I found jQuery's addClass and removeClass and thought that'd work. So far, it has, except that the fading isn't occurring. Instead, it's just immediately switching to the on-hover image.
Here's a Fiddle of my code, except that my logo has been replaced by an apple and the over-logo has been replaced by an orange, since my logo pictures are locally stored.

The JavaScript I have in the Fiddle is this:

$(document).ready(function() {
    $('img#logo').hover(
   function(){ $(this).stop(true).addClass('over', 500) },
   function(){ $(this).stop(true).removeClass('over', 500) });
});

And here is my HTML:

<img id="logo" class="top" width="256" height="256" />

And my CSS:

img.top {
    background-image: url('.jpeg');
}

img.over {
    background-image: url('.png');
}

Thoughts? Thanks!

I'm trying to fade one image into another. I tried the CSS route by making the top image transparent on hover, but that's not going to work in my case. In my case, on hover, I'm trying to fade my logo into my logo with a glow. The logos aren't a perfect shape, so having the two pictures on top of one another is redundant, since the user can see the "over" picture, aka logo with a glow, without hovering over it.

I found jQuery's addClass and removeClass and thought that'd work. So far, it has, except that the fading isn't occurring. Instead, it's just immediately switching to the on-hover image.
Here's a Fiddle of my code, except that my logo has been replaced by an apple and the over-logo has been replaced by an orange, since my logo pictures are locally stored.

The JavaScript I have in the Fiddle is this:

$(document).ready(function() {
    $('img#logo').hover(
   function(){ $(this).stop(true).addClass('over', 500) },
   function(){ $(this).stop(true).removeClass('over', 500) });
});

And here is my HTML:

<img id="logo" class="top" width="256" height="256" />

And my CSS:

img.top {
    background-image: url('https://pbs.twimg./profile_images/3588564988/ca2ec46372bf01eff59044dd0046132c.jpeg');
}

img.over {
    background-image: url('http://icons.iconarchive./icons/gcds/chinese-new-year/256/orange-icon.png');
}

Thoughts? Thanks!

Share Improve this question edited Feb 6, 2014 at 3:44 Sumner Evans 9,1755 gold badges31 silver badges48 bronze badges asked Feb 6, 2014 at 2:54 BenBen 3172 gold badges5 silver badges10 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

Try using a transition

#logo{
    transition: background-image 2s;
    -moz-transition: background-image 2s;
    -webkit-transition: background-image 2s;
}

Demo: Fiddle


Since background-image transition is supported only in Chrome for now, try fadeOut()

<div id="logo" class="top" />

then

#logo {
    width:256px;
    height:256px;
    background-image: url('https://pbs.twimg./profile_images/3588564988/ca2ec46372bf01eff59044dd0046132c.jpeg');
}
#logo.over {
    background-image: url('http://icons.iconarchive./icons/gcds/chinese-new-year/256/orange-icon.png');
}

and

$(document).ready(function () {
    $('#logo').hover(function () {
        $(this).stop(true, false).fadeTo(500, .1, function () {
            $(this).addClass('over').fadeTo(500, 1)
        })
    }, function () {
        $(this).stop(true, false).fadeTo(500, .1, function () {
            $(this).removeClass('over').fadeTo(500, 1)
        })
    });
});

Demo: Fiddle

As css transition doesn't seems to work for background images in firefox. You can follow a different approach. Pure html css approach.

HTML

<div id="logo">
  <img id="logo-img1" src="http://imgsrc">
  <img id="logo-img2" src="http://imgsrc">
</div>

CSS

#logo{
    position:relative; //not required if you already have given any position css
}
#logo img{
    position:absolute;
    top:0;
    left:0;
    width:100%;
    height:100%;
    transition: opacity .25s ease-in-out;
    -moz-transition: opacity  .25s ease-in-out;
    -webkit-transition: opacity  .25s ease-in-out;
}

#logo-img1{
        opacity:1;  
    }

#logo-img2{
        opacity:0;
    }

#logo:hover #logo-img1{
      opacity:0;    
    }

#logo:hover #logo-img2{
      opacity:1;    
    }

You need to use a div instead of an img tag and then you need to use a CSS transition:

HTML:

<div class="logo"></div>

CSS:

.logo {
    background-image: url('https://pbs.twimg./profile_images/3588564988/ca2ec46372bf01eff59044dd0046132c.jpeg');
    transition: background-image .25s ease-in-out;
    -moz-transition: background-image .25s ease-in-out;
    -webkit-transition: background-image .25s ease-in-out;
    height: 256px;
    width: 256px;
}

.logo:hover {
    background-image: url('http://icons.iconarchive./icons/gcds/chinese-new-year/256/orange-icon.png');
}

Note: For some reason the transition doesn't work in Firefox.

http://jsfiddle/8tx36/4/

Please, take a look on the example with css3

transition: background 1s linear;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745008431a4605923.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信