0

在调整窗口大小时,绝对定位的 DIV 移动到HTML 的右边界之外- 因此出现水平滚动条。但我只需要在中央固定宽度列上滚动条。有什么解决办法吗?如何在 HTML-tag 右边框下悄悄获得正确的评论?

http://jsfiddle.net/9y5BW/1/

<!DOCTYPE HTML>
<html>
<head><title>Absolute position right scrollbar removing</title></head>

<body>
  <style>
    body {line-height: 1.5em;}
    p {margin: 0;}
    .page {position: relative; width: 400px; margin: 0 auto; background-color: #ccc;}
    .comment-container {position: relative; width: 100%; height: 0; top: -1px;}
    .comment {position: absolute; width: 200px; background-color: #eee; top: -1.5em; border-top: 1px solid #aaa;}
  </style>

    <div class="page">
    <p>
        Text and images here. Scrollbar should appear when window size is less than 400px.
    </p>
    <div class="comment-container">
      <div class="comment" style="right: -200px;">
        Some outside comment on the right. Horizontal scrolbar on html-tag appears on window squeezing.
        Is there any way to remove the scrollbar when this element goes outside?
      </div>
    </div>
    <p>
        More text, text and text goes here.
    </p>
    <div class="comment-container">
      <div class="comment" style="left: -200px;">
        Some outside comment on the left. No scrollbar on window resizing.
      </div>
    </div>
    </div>

</body>
</html>
4

2 回答 2

1

如果您overflow: hidden在正确的 div/包装器上使用,您可以决定哪些内容不会触发滚动条。

于 2012-07-26T08:28:17.803 回答
1

你把所有东西都放在一个隐藏溢出的容器中,然后right: -100px像这样定位右列:

<!DOCTYPE html>
<html>
<head>
<style type="text/css">
html,
body { 
    margin: 0;
    padding: 0;
    text-align: center;
}

body {
    overflow: auto;
}
#container {
    min-width: 960px;
    zoom: 1; /*For ie6*/
    position: relative; /*For ie6/7*/
    overflow: hidden;
    margin: 0 auto;
}

#main {
    background: #cea;
    width: 960px;
    margin: 0 auto;
    height: 700px;
    position: relative;
    top: 0;
}

#right,
#left {
    position: absolute;
    height: 100px;
    width: 100px;
    top: 0;
    z-index: 100;
}

#right { 
    background: #797;
    right: -100px;
}

#left {
    background: #590;
    left: -100px;
}      
</style>
</head>
<body>
    <div id="container">
        <div id="main">
            <div id="left">left</div>
            <div id="right">right</div>
        </div>
    </div>
</body>
</html>
于 2012-07-26T08:31:40.493 回答