8

我在页面顶部有一个固定位置的 div。(例如 Facebook)和页面中相对定位的 div。当我向下滚动时,相对 div 正在通过固定 div。我希望它在固定的 div 下通过。有什么想法来处理这个吗?

在此处输入图像描述

#navcontainer
{
    position:fixed;
}

.city
{
    position:relative;
    float:left; 
}

.city .text 
{
    position:absolute;
    top:100px; 
    left:15px;
}

在这个 css 中,我有一个城市 div,绝对文本位于相对 div (.city) 中的图像上

4

4 回答 4

15

嗨,我认为你应该这样做:

css

#navcontainer
{
    position:fixed;
    background:red;
    left:0;
    right:0;
    top:0;
    height:100px;
    z-index:3;
}

.city
{
    position:relative; 
    background:green;
    left:0;
    right:0;
    padding:10px;
    top:100px;
    z-index:2;
}

.city .text 
{
    padding:10px;
    background:yellow;
}​

HTML

<div id="navcontainer">This is navigation</div>

<div class="city">
    <div class="text">here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br /></div>
</div>
​

现场演示http://jsfiddle.net/xLnKN/1/

于 2012-04-25T11:46:38.883 回答
5

我认为您正在寻找的是 CSS 属性z-index。有关文档,请参见此处:http ://reference.sitepoint.com/css/z-index

我拼凑了一个小例子,展示了如何将它与你的 CSS 一起使用:http: //jsfiddle.net/B63Km/

基本思想是 z-index 越高,元素离你越近。

于 2012-04-24T18:38:16.833 回答
1

对标题 div 使用较高的 z-index,即城市,为下面的 div 使用较低的 z-index(即相对的)

于 2013-05-16T09:41:11.000 回答
1

您如何避免为此使用 z-index?Z-index 通常被认为是一种反模式,并且很容易滚雪球式地要求其他元素也需要 z-index。

于 2019-11-24T03:39:42.870 回答