绝对位置,负右移,删除滚动条



在窗口调整大小时,绝对定位的div移动到HTML的右边界之外,因此出现水平滚动条。但我只需要中央固定宽度列上的滚动条。有什么建议吗?如何在HTML标签的右边框下安静地获得正确的评论?

请参阅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>

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

您将所有内容都放在一个有隐藏溢出的容器中,然后像这样定位右列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>

最新更新