8

Is there a way that I can do the following?

I have a transparent png sprite that shows a standard picture on the left, and a picture for the :hover state on the right.

Is there a way that I can have the image fade from the left image into the right image on :hover using only css3 transitions? I've tried the following, but it doesn't work:

li{-webkit-transition:all 0.5s linear; -moz-transition:all 0.5s linear; -o-transition:all 0.5s linear; transition:all 0.5s linear;}
li{background:url(/img/sprites.png) 0 -50px no-repeat;}
li:hover{background:url(/img/sprites.png) 0 -150px no-repeat;}

Now, the above does animate the background, it pans the image across. What I'd like instead of a pan is a fade or dissolve effect.

UPDATE: I ended up having to create two elements and just animate the opacities separately. It's a tad messy because I have to specify the exact margins of each element, but I guess it'll work. Thanks for everyones help :)

8 Answers 8

4

The latest news on this topic:

Chrome 19 and newer supports background-image transitions:

Demo: http://dabblet.com/gist/1991345

Additional info: http://oli.jp/2010/css-animatable-properties/

2
  • 1
    +1 best answer so far. But it's only working in chrome for now Commented Jun 19, 2013 at 11:28
  • True, But what about other cross browsers support ?? Commented Nov 27, 2015 at 7:26
3

You haven't specified any code to do the actual transition.

http://css3.bradshawenterprises.com/cfimg1/

Try this out in your hover style:

-webkit-transition: opacity 1s ease-in-out;
-moz-transition: opacity 1s ease-in-out;
-o-transition: opacity 1s ease-in-out;
-ms-transition: opacity 1s ease-in-out; 
transition: opacity 1s ease-in-out;
2
  • No, I have, sorry, I didn't include that. Opacity would hide the entire list element though, I need it to fade from one background position to another instead of moving the background image.
    – Jack
    Commented Aug 4, 2011 at 21:57
  • @Joe that's very close to what I want. I'm going to have to look through the code a bit.
    – Jack
    Commented Aug 4, 2011 at 22:21
1

Take a look at this: http://jsfiddle.net/j5brM/1/

I think this suits all your needs and its a little bit less complicated.

3
  • Thanks for going to the effort, but this still just pans the background image. What I need is a fade effect.
    – Jack
    Commented Aug 4, 2011 at 22:17
  • I'm sorry! I forgot what type of animation you needed. However I tried doing this: http://jsfiddle.net/ZNsnV/. It's the best I can get with only css and background images!
    – Rauzippy
    Commented Aug 4, 2011 at 22:54
  • I ended up having to create two elements and just animate the opacities separately. It's a tad messy because I have to specify the exact margins of each element, but I guess it'll work. Thanks for your help :)
    – Jack
    Commented Aug 5, 2011 at 0:26
1

I don’t think you can change the opacity of just background images in CSS, so unless you have two separate elements for the background image (one for each position of the sprite) and change the opacity of both of them on hover, I think you’re stuck.

3
  • I was afraid of that. What a shame that they didn't include this in css3.
    – Jack
    Commented Aug 4, 2011 at 22:23
  • Oh: it has been suggested that you could use :before to simulate background opacity — see near the bottom of nicolasgallagher.com/css-background-image-hacks Commented Aug 4, 2011 at 23:03
  • I ended up having to create two elements and just animate the opacities separately. It's a tad messy because I have to specify the exact margins of each element, but I guess it'll work. Thanks for your help :)
    – Jack
    Commented Aug 5, 2011 at 0:26
0
li{background:url(/img/sprites.png) 0 -50px no-repeat; background:rgba(80, 125, 200, 0.55);}
li:hover{background:url(/img/sprites.png) 0 -150px no-repeat; background:rgba(100, 125, 175, 0);}

should be

li{background:url(/img/sprites.png) 0 -50px no-repeat; background-color:rgba(80, 125, 200, 0.55);}
li:hover{background:url(/img/sprites.png) 0 -150px no-repeat; background-color:rgba(100, 125, 175, 0);}

not sure if that fixes it or not though.

2
  • Unfortunately that didn't solve it, but thanks for pointing that out.
    – Jack
    Commented Aug 4, 2011 at 21:51
  • @Jack it's kind of hard to visualize the situation without knowing what the sprite is... do you have a development version of the site up somewhere? Commented Aug 4, 2011 at 21:53
0

I know this may be a tad late. But I was struggling with the same issue for a long time. Also with transparent sprites many solutions don't seem to work.

What I did is this

HTML

<div class="sprite-one">
  <span class="foo"></span><span class="zen"></span>
</div>

CSS

.sprite-one {
height: 50px
width: 50px
}
.sprite-one span {
width: 50px;
height: 50px;
display: block;
position: absolute;
}
.foo, .zen { 
background-image: url(sprites.png) no-repeat;
-webkit-transition: opacity .6s ease-in-out;
-moz-transition: opacity .6s ease-in-out;
-o-transition: opacity .6s ease-in-out;
transition: opacity .6s ease-in-out;
}
.foo {
background-position: 0 0;
opacity: 1;
}
.zen {
background-position: -50px 0;
opacity: 0;
}
.sprite-one:hover .foo {
opacity: 0;
}
.sprite-one:hover .zen {
opacity: 1;
}

This is a pure css way & has a bit of a lot of coding.. but seems be the only way I achieved the desired effect! Hope people that also stumble onto this can find some help from this!

0
<li class="image transition"></li>

css:
.image{
background-image: url("some/file/path.png");
background-repeat: no-repeat;
width: XXpx;
height: XXpx;
background-position: center center;
background-size: cover;
}

/* DRY */
.transition{
transition: background 0.6s;
-webkit-transition: background 0.6s;
}

.image:hover{
background-image: url("some/file/path_hoverImage.png");
}
-1

CSS:-

li { background: url(http://oakdale.squaresystem.co.uk/images/solutions.png) no-repeat left center; background-size: 89px; padding: 54px 0 54px 130px; webkit-transition:all 0.5s linear; -moz-transition:all 0.5s linear; -o-transition:all 0.5s linear; transition:all 0.5s linear; }

li:hover { background-size: 50px }

Not the answer you're looking for? Browse other questions tagged or ask your own question.