4

剧情说明:有一只鸟在飞之前应该收拾它的腿。鸟的身体、腿和眼睛是background-image分开div的s。我使用 jQuery 的动画方法来改变腿的位置并将它们放在鸟的身体内。

问题:腿不是在鸟的身体下面,而是在身体上方。

问题:我做错了什么导致鸟的腿不能在它下面?

图片:

  1. 动画前
  2. 动画后

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');
4

3 回答 3

15

您缺少.. z-indexposition上的属性#gonji .body仅适用于定位元素

尝试添加position: relative;

于 2013-03-23T15:56:18.477 回答
2

您必须更改z-index.

z-index:-1;.feet

您也可以保持相同的位置,只需在.hide()和之间切换.show()

于 2013-03-23T15:53:42.177 回答
0

CSS

#gonji { width: 80px; height: 55px; position: relative; }
#gonji .body { width: 80px; height: 55px; background:red; z-index: 998; }
#gonji .eye { width: 5px; height: 4px; background:blue; position: absolute; top: 13px; left: 30px; z-  index: 999; }
#gonji .feet { width: 9px; height: 17px; background:black; position: absolute; top: 55px; left: 30px; }

JS

    $(document).ready(function(){
setInterval(callMe,1000);
 });

function callMe(){
var $gonjiFeet = $("#gonji").find(".feet");
var gonjiOrigFeetPos = $gonjiFeet.position();
$gonjiFeet.css('z-index','-1').animate({ 'top': gonjiOrigFeetPos.top - 20 }, 'fast');
$gonjiFeet.animate({ 'top': '55' }, 'fast');
}
于 2013-03-23T16:12:36.920 回答