有没有更简单的方法来阻止元素滚动到页面顶部?

时间:2011-07-06 16:36:26

标签: css scrollbar

http://jsfiddle.net/XjqmS/

我将内容标记更正为position:relative后,该元素将不会停留在其位置。相反,它在图像上方滚动。我希望页面滚动,而不是元素。这可能吗?

1 个答案:

答案 0 :(得分:1)

我认为重做布局会消除使用position:fixed:D

的问题

请参阅此处:http://jsfiddle.net/SAfLK/

<强> HTML

<div id="wrapper">
    <div id="header">FIXED HEADER</div>
    <div id="navigation">FIXED NAVIGATION</div>
    <div id="content">
        CONTENT GOES HERE
        <img src="http://www.dummyimage.com/200/" />
        <br />
        <img src="http://www.dummyimage.com/200/" />
        <br />
        <img src="http://www.dummyimage.com/200/" />
        <br />
        <img src="http://www.dummyimage.com/200/" />
        <br />
    </div>
</div>

<强> CSS

/*adjust dimensions as necessary*/

html, body {
    height:100%;
    margin:0;
    padding:0;
}

#wrapper {
    position:relative;
    height:100%;
    width:400px;
    background-color:#efe;
    margin:0 auto; 
}

#header {
    height:50px;
    background-color:#ded;
}

#navigation, #content {
    position:absolute;
    top:50px; /*set to header's height*/
    bottom:0;
    left:0;
    right:0;
}

/* #content and #navigation's width should add up to the #wrapper's width*/

#navigation {
    width:150px;
    background-color:#cdc;
}

#content {
    overflow:auto;
    width:250px;
    left:150px; /*set to whatever navigations's width is*/
    background-color:#fef;
}