Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Absolute position and wrong z-index

Plot Explanation: There's a bird that should collect it's legs before flying. Bird's body, legs and eye are background-images of separate divs. I used jQuery's animate method to change legs position and put them inside the bird's body.

Problem: Instead of legs going under the bird's body, they stay above it.

Question: What am I doing wrong that makes bird's legs not going under it?

Images:

  1. Before animation
  2. After Animation

HTML:

<div id="gonji">
    <div class="legs"></div>
    <div class="body">
        <div class="eye"></div>
    </div>
</div>

CSS:

#gonji { width: 80px; height: 55px; position: relative; }
#gonji .body { width: 80px; height: 55px; background: url('../gonji/gonji.png') no-repeat scroll center center transparent; z-index: 998; }
#gonji .eye { width: 5px; height: 4px; background: url('../gonji/gonji_eye.png') no-repeat scroll center center transparent; position: absolute; top: 13px; left: 30px; z-index: 999; }
#gonji .legs{ width: 9px; height: 17px; background: url('../gonji/gonji_legs.png') no-repeat scroll center center transparent; position: absolute; top: 35px; left: 30px; z-index: 1 }

JS:

var $gonjiLegs = $("#gonji").find(".legs");
var gonjiOrigLegsPos = $gonjiLegs.position();
$gonjiLegs.animate({ 'top': gonjiOrigLegsPos.top - 17 }, 'fast');
like image 507
Farid Rn Avatar asked Dec 02 '22 23:12

Farid Rn


1 Answers

You're missing a position property on #gonji .body .. z-index only works on positioned elements

Try adding position: relative;

like image 98
user2129623 Avatar answered Dec 24 '22 01:12

user2129623