Ry-
Ry-

Reputation: 225124

Keeping style applied using :hover until transition complete?

I have a bunch of tiles on a page that expand as the user mouses over them. The expanded one should have the highest z-index, and this works, but I need the z-index to remain until the size transition is complete. Is there a way to do this using CSS only, no JavaScript? Since I'm using transitions, I'm not too worried about compatibility here, I applied progressive enhancement correctly.

Here's a jsFiddle that demonstrates this. Mouse over A; it transitions out. Mouse off of it, however, and it falls behind B. I need it to stay in front of B until the transition completes. Is there an elegant way to do this?

Upvotes: 6

Views: 1112

Answers (4)

noob
noob

Reputation: 9212

You need to set z-index to transition too: http://jsfiddle.net/uHJwT/2/

Upvotes: 2

frozenkoi
frozenkoi

Reputation: 3248

Try using transitions like in http://jsfiddle.net/frozenkoi/YK52N/ (note the comments in the CSS section, for both the .item and .item:hover)

The trick is to use transitions for the z-index property too. You can set, for example, a value of 10 for the normal items and 11 for the hovered ones. You also have to use transition-delay so that the animation for moving the mouse out doesn't reset the z-index inmediately. Next, add a different value to transition-delay to the rule for :hover with a value of zero so that the z-index does update inmediately when the mouse goes into the item.

In short, .item has the transition for mouse out of the item and .item:hover the rules for when the mouse moves in.

Upvotes: 1

bookcasey
bookcasey

Reputation: 40511

You need to define the z-index, as well as animate it.

This works in Firefox (8.0.1) and Webkit.

Upvotes: 2

VinayC
VinayC

Reputation: 49225

Here's the one solution: http://jsfiddle.net/uHJwT/4/

Essentially, it uses another wrapper div that has sufficient width & height to cover animated surface - on hover, it elevates its z-index so that the animated div remains on top. Of course, this is not full-proof solution - it is based on the fact that typical hover off would be down movement and it works for that - but hover off in diagonal direction would not work. But seems to be a reasonable CSS only solution - I would rather used js to get a perfect one.

Upvotes: 0

Related Questions