使一个position:absolute div在另一个可滚动div中正常滚动
我有一个固定高度的可滚动
使用 position:fixed
定位在所有页面元素上。在 div 中,我的元素高于固定高度,因此出现滚动条。我还有一个工具提示,即使它滚动,我也想保留该段落。这就是我想要在这里发生的事情,但不幸的是我的解决方案都无法正常工作:
我将
position:absolute
添加到工具提示,将position:relative
添加到#overlay
(工具提示的父级):http://jsfiddle.net/4qTke/工具提示按预期滚动,但在
#overlay
之外不可见。我只将
position:absolute
添加到工具提示中:http://jsfiddle.net/ yp6Wf/工具提示在父级
#overlay
外部可见,但在 div 滚动时不会移动。
我希望工具提示始终可见并在滚动时移动。
I have a fixed height scrollable <div id="overlay">
positioned over all the page elements using position:fixed
. In the div I have elements higher than the fixed height, so the scrollbar appears. I also have a tooltip that I want to stay with a paragraph even if it is scrolled.
That's what I want to happen here, but unfortunately neither of my solutions work properly:
I add
position:absolute
to the tooltip andposition:relative
to#overlay
(the tooltip's parent): http://jsfiddle.net/4qTke/The tooltip scrolls as expected but it is not visible outside of
#overlay
.I only add
position:absolute
to the tooltip: http://jsfiddle.net/Yp6Wf/The tooltip is visible outside of the parent
#overlay
but doesn't move when the div is scrolled.
I want the tooltip to always be visible AND for it to move when scrolled.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
仅使用 CSS 和 HTML 无法实现您想要的效果。
您遇到的主要问题是您在
#tooltip
相对于的容器上设置了overflow:scroll
。因为当您将#tooltip
放置在div
的“外部”时,此溢出属性会阻止任何内容出现在其边缘之外,因此它将被隐藏,并且仅在滚动到时才可见。它在第二种情况下可见的原因是,如果不设置
position:relative
,您的#tooltip
是相对于页面而不是容器的。这意味着它不受容器的overflow:scroll
属性的影响。What you want is not possible using just CSS and HTML.
The main problem you have is that you have set
overflow: scroll
on the container your#tooltip
is relative to. Because this overflow property is stopping any content from appearing outside of its edges when you position#tooltip
"outside" of thediv
it will be hidden and only visible when scrolled to.The reason it was visible in your second scenario is because without setting
position:relative
your#tooltip
was relative to the page and not the container. Which meant it was not affected by theoverflow:scroll
property of the container.HTML:
CSS:
HTML:
CSS:
也许这对你来说是一个选择?请参阅演示小提琴。
Maybe this is an alternative for you? See demo fiddle.