javascript - 使用 Position Fixed 子项滚动 Div
问题描述
我有一个包含一些内容的 div。此 div 将其溢出-y 设置为滚动。
在包装器内部,还有 2 个 div。第一个的高度大于包装器,第二个包含一些内容并且是绝对定位的。
<div id="wrapper" style="overflow-y:scroll;height:500px">
<div id="setHeight" style="height:1000px"></div>
<div id="content" style="position:fixed">
Content Goes Here
</div>
</div>
问题是当鼠标位于 上时#content
div
,#wrapper
不会滚动。但是,当鼠标位于未填充的其他部分时,#wrapper
div
它#content
div
会滚动。
我试图以各种方式解决这个问题:
第一:将z-index
of设置#content
为-1
。这有效(如在#wrapper
卷轴中),但无法再与内容交互。
第二:应用上述修复,但使用 Javascript 来监听鼠标点击。当用户单击时,立即将#content
'更改z-index
为1
允许用户交互。
问题在于即使它有效(即z-index
更改),浏览器仍然不会与 交互,#content
除非您松开鼠标并再次单击。当您放开鼠标时,我希望它#wrapper
可以再次滚动。
第三:我尝试将 设置#content
z-index
为-1
,并将#wrapper
's设置pointer-events
为none
。然而,这使得滚动和交互都停止了。
有没有办法做到这一点?
解决方案
#wrapper {
background: red;
overflow-y: scroll;
height: 100px; border: solid 1px;}
#header {
background: lightblue;
height: 200px; overflow-y: none;}
#content {
background: green;}
#popup {
background: yellow;
position: fixed;
top: 0;
opacity: 0.5;
pointer-events: none;
}
<div id="wrapper">
<div id="header">Headerum textum</div>
<div id="content">
Contentum Textum
<div id="popup">
Popup textum, lorem ipsum dolor sit amet, consectetur adipiscing elit. ... Aenean ut orci vel massa suscipit pulvinar. Nulla sollicitudin. Fusce varius, ligula non tempus aliquam, nunc turpis ullamcorper nibh, in tempus sapien eros vitae ligula.
</div>
</div>
</div>
请看一下……在这个CodePen上
推荐阅读
- rust - 从内部 if 语句返回时类型不匹配
- c++ - 使用重复的 SWIG 模块名称
- api - Xamarin,如果状态码不正常,则重定向到页面
- java - 如何更改 Standalone FLINK 集群中单个应用的日志记录级别
- python - 如何扩展 django 管理员历史日志并添加自定义字段?
- json - Angular 2 - 将 Excel 工作表解析为 Json
- python - python-从数据框中获取列数据类型
- ruby-on-rails - rails - 故意禁用对“创建”操作的 CSRF 检查的最大安全风险是什么?
- node.js - 使用 NodeJs 的 Azure WebSocket
- javascript - 将参数传递给 GET-Request