由于带有position:absolute的DIV位于页面之外而禁用水平滚动条
我有一个位于页面“外部”的绝对定位元素,但我希望浏览器(我使用的是 Firefox 3)不要显示水平滚动条。看起来显示位于左侧的div(例如具有“left:-20px”)是可以的,并且不显示滚动条。然而,右侧的相同内容(“right:-20px”)始终显示滚动条。是否可以隐藏滚动条,但保持标准滚动成为可能?我的意思是我只想由于这个绝对定位的元素而禁用滚动,但由于其他元素而保持滚动(我知道我可以完全禁用滚动条,这不是我想要的)。
<!DOCTYPE html>
<html>
<body>
<div id="el1" style="position: absolute; top: 0; background-color: yellow; left: -20px;">
element
</div>
<div id="el2" style="position: absolute; top: 0; background-color: yellow; right: -20px;">
element
</div>
<h1>Hello</h1>
<p>world</p>
</body>
</html>
I have an absolutely positioned element that is "outside" of the page, but I want browsers (I am using Firefox 3) not to display horizontal scrollbars. It seems that displaying a div that is positioned to the left (e.g. having "left: -20px") is okay, and no scrollbar is shown. However the same thing on the right ("right: -20px") always shows the scrollbar. Is it possible to hide the scrollbar, but to keep standard scrolling possible? I mean I only want to disable scrolling due to this absolute-positioned element, but to keep scrolling due to other elements (I know I can disable scrollbars completely, that's not what I want).
<!DOCTYPE html>
<html>
<body>
<div id="el1" style="position: absolute; top: 0; background-color: yellow; left: -20px;">
element
</div>
<div id="el2" style="position: absolute; top: 0; background-color: yellow; right: -20px;">
element
</div>
<h1>Hello</h1>
<p>world</p>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
是的,可以在 html 标签上输入
style="overflow-x:hidden"
。这样就可以了...Yes, it is possible, on your html tag, type
style="overflow-x: hidden"
. That'll do the trick...事实上,这可以使用直接的 CSS 来完成,而对页面宽度等没有任何限制。可以通过以下方式完成:
第一个 div 中的内容将与第二个 div 的内容保持正确对齐,但任何超出窗口边界的内容都将被截断。
下面是一个工作示例,无需使用任何 JavaScript,即可将图像保持在相对于其余内容的固定位置:
This can in fact be done using straight CSS without having any restrictions on page width etc. It can be done by:
The content in your first div will stay properly aligned with the content of your second div, but any of its contents that go beyond the perimeter of the window will be truncated.
Here's a working example that keeps the image in a fixed position relative to the rest of the content, without using any JavaScript:
您所要做的就是在 div 外部添加一个包装器。
这是CSS:我将我的类称为“main_body_container”
希望它有帮助:)
更新
为其创建了一支笔,请参阅这里
What you have to do is to add a wrapper outside of your divs.
Here is the CSS: I call my class 'main_body_container'
Hope it helps :)
Update
Created a Pen for it, see it here
不太确定这是否有帮助,但您可以尝试将上述 div 的样式更改为:
通过将位置设置为固定,它将“锁定”指定的 div,而其余内容仍然可滚动。当然,这是假设在实例中其余内容通过 z-index 堆叠在定义的 div 之上。
希望这有帮助。
Not too sure if this would help but you can try changing the styling for the mentioned divs to this instead:
By setting the position as fixed instead, it "locks" the specified divs in place while the rest of the content is still scrollable. Of course this is assuming that it is in the instance that the rest of the content is stacked by the z-index to be on top of the defined divs.
Hope this helps.
我通过将元素的
position
属性更改为fixed
解决了这个问题:该元素不再导致水平滚动,但其他元素仍然导致水平滚动。
I was able to solve this problem by changing the element's
position
attribute tofixed
:No more horizontal scrolling caused by this element but still horizontal scrolling caused by other elements.
放置以下样式
我在 IE 8、FF 和 Chrome 上进行了测试。
Put the following style
I tested it on IE 8, FF and Chrome.
将其添加到您的 CSS 中:
IE 无法识别 Overflow-x 和 -y 样式。因此,如果您只关心 Firefox 3,那么这会很好用。否则,你可以使用一些 JavaScript:
Add this to your CSS:
The overflow-x and -y styles are not recognized by IE. So if you're only concerned with Firefox 3, this will work fine. Otherwise, you can use some javascript: