1

I have two elements in this jsFiddle. Each expands on mouseover with a :hover transition.

I want the blue animation to match the red one. Right now the red one expands over top of the blue one on mouseover, but the blue one expands under the red one. I would like each to animate over top of the other. This could probably be done by changing the z-index on :hover but that didn't seem to work very well.

5
  • 1
    ?? -- for me, it does exactly what you want .. Commented Sep 23, 2011 at 21:52
  • What browser are you using? I know in IE that z-index only works on positioned elements if the parent has a higher z-index. Commented Sep 23, 2011 at 21:53
  • I am using chrome 13, but I see the glitch in firefox 6 as well. I want the animation to be smooth all of the way accross. Commented Sep 23, 2011 at 21:58
  • This is not a question, it's somebody asking for somebody else to do their homework for them. Sorry, I'm very forgiving about stack overflow rules but this "question" just doesn't belong here at all.
    – dudewad
    Commented Sep 23, 2015 at 4:47
  • Your jsfiddle has gone 404. Any chance you still have a copy of it somewhere?
    – DomQ
    Commented May 3, 2018 at 12:25

3 Answers 3

5

Since you are animating all properties, the z-index animates and steps from 0 to 1 at the end of the animation.

Just animate the properties that you need and it works:

#a, #b {
    width: 100px;
    height: 100px;
    position: absolute;
    top: 0;
    -webkit-transition: width .6s ease-in-out;
    -moz-transition: width .6s ease-in-out;
    -o-transition: width .6s ease-in-out;
    -ms-transition: width .6s ease-in-out;
    transition: width .6s ease-in-out;
}

http://jsfiddle.net/duopixel/hfM7E/

1
  • I like your solution, but what if I want to animate more than one property other than z-index such as height? Would I comma-separate the values? It doesn't seem to be working here: jsfiddle.net/hfM7E/1 Commented Sep 23, 2011 at 23:27
0

Try adding this to the CSS (and remove the z-index modifier in #a:hover, #b:hover {...}):

#a {
    background: #55a;
    left: 0;
    z-index: 1;
}

#b:hover {
    z-index: 2;   
}

Example: http://jsfiddle.net/h6ztv/3/

1
  • The jsfiddle link is broken.
    – microspino
    Commented Feb 11, 2019 at 8:15
0

Try setting initial z-index value to '0'

#a, #b {
    width: 100px;
    height: 100px;
    position: absolute;
    top: 0;
    -webkit-transition: all .6s ease-in-out;
    -moz-transition: all .6s ease-in-out;
    -o-transition: all .6s ease-in-out;
    -ms-transition: all .6s ease-in-out;
    transition: all .6s ease-in-out;
    z-index:0;
    }

#a:hover, #b:hover {
    width: 600px;
    z-index: 1;
}

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