【问题标题】:CSS close button for a popup window at fixed positionCSS关闭按钮,用于固定位置的弹出窗口
【发布时间】:2018-03-26 22:40:14
【问题描述】:

我正在尝试创建一个使用 JQuery 触发并使用 CSS 设置样式的弹出窗口。我在框的右上角添加了一个关闭按钮,但是当您在框内向下滚动时(溢出:滚动,由于框的最大高度),即使定义了绝对位置,X 关闭按钮也会滚动:

.team-name-popup{
position:fixed;
z-index: 9;
top:10%;
left:50%;
margin-left:-350px;
width:700px;
height:auto;
max-height:400px;
background:rgba(0,0,0,0.9);
overflow:scroll;
}
.team-name-popup-inner{
position:relative;
width:100%;
height:auto;
max-height: 100%;
padding: 50px;
overflow:scroll;
}
.team-popup-close{
position:absolute;
z-index:999;
top:5px;
right:10px;
color:#fff;
}

HTML

<div class="team-name-popup">
<a href="#" class="team-popup-close">&times;</a>
<div class="team-name-popup-inner">
<h5 class="team-name">Name</h5>
Text here
</div><!--team popup inner-->
</div><!--team popup-->

我尝试更改溢出,但滚动不起作用:

.team-name-popup{
....
overflow:hidden;
}
.team-name-popup-inner{
....
overflow:scroll;
}

默认

滚动

【问题讨论】:

  • 如果你想让一个元素保持相对于屏幕视口的位置,你需要fixed定位,MDN: > "fixed元素从正常的文档流中移除;没有为页面布局中的元素。相反,它相对于屏幕的视口定位,并且在滚动时不会移动。 developer.mozilla.org/en-US/docs/Web/CSS/position

标签: css overflow css-position


【解决方案1】:

请检查此解决方案。

.team-name-popup{
position:fixed;
z-index: 9;
top:10%;
left:50%;
margin-left:-350px;
width:700px;
height: 400px;
max-height:400px;
background:rgba(0,0,0,0.9);
overflow:scroll;
}
.team-name-popup-inner{
position: absolute;
width:100%;
height: 400px;
padding: 50px;
overflow:scroll;
}
.team-popup-close{
position:absolute;
z-index:999;
top:5px;
right:10px;
color:#fff;
}

【讨论】:

  • 非常感谢!所以看起来问题仅仅是因为 .team-popup-inner 没有定义适当的高度?
  • 其实是因为.team-name-popup-inner中有position: relative。然后我们不得不替换为height: 400pxposition:absolute
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-13
  • 2014-01-19
  • 1970-01-01
相关资源
最近更新 更多