将容器始终放置在顶部

Positioning a container always on top

本文关键字:顶部      更新时间:2023-09-26

我有一个插件,我提供了一个简单的div容器,插件通过javascript在容器内创建各种dom元素。这个插件创建的基本结构是这样的:

Wrapper Div (Position:Relative)
   - Left Container (Position: Absolute)
        - Left Top sub-container (Position: Absolute)
   - Right Container (Position: Relative)
        - Right Top sub-container (Position: Absolute)
        - Right Sub-Container frame with absolute items (Position: Relative)

这是我为插件创建的完全相同的结构创建的小提琴:http://jsfiddle.net/FsYt8/

我在这里面临的问题是,当内容溢出时,可以用鼠标滚轮滚动内容(如fiddle)。然而,当我向下滚动时,我希望上面的两个左上方和右上方的子容器(红/蓝条)颜色保持在顶部,而不是随着内容向下滚动。

插件在container-right-framediv内创建dom元素并定位元素的绝对位置(然后根据container-right的顶部/左侧定位)。我如何实现上述两个杆的固定定位?

使标题固定并相应定位:

http://jsfiddle.net/QYCra/

将列的初始高度向下移动,使它们从标题下开始:

.frame {width:924px;height:300px;position:relative;overflow-x:hidden;}
.axis-left {width:303px;height:34px;position:fixed; top:0; background-color:blue;z-index:2;}
.axis-right {width:615px;height:34px;position:fixed; top:0; left:310px; background-color:red;z-index:2;}
.container-left {height:340px;width:303px;top:30px; left:0px;position:absolute;background-color:green;z-index:1;overflow:hidden;}
.container-right {height:340px;width:615px;top:30px; left:304px;position:relative;overflow:hidden;background-color:yellow;z-index:1;}

您可以将红色和蓝色div固定并将其浮动到左侧。右DIV的左边距将按如下方式调整DIV的x轴位置:

.frame {width:924px;height:300px;position:relative;overflow-x:hidden;}
.axis-left {float:left;width:303px;height:34px;position:fixed;background-color:blue;z-index:2;}
.axis-right {float:left;margin-left:305px;width:303px;height:34px;position:fixed;background-color:red;z-index:2;}
.container-left {height:340px;width:303px;left:0px;position:absolute;background-color:green;z-index:1;overflow:hidden;}
.container-right {height:340px;left:304px;position:relative;overflow:hidden;}
.container-right-frame{
    position:relative;top:34px;background-color:yellow;z-index:1;width:615px;
}