【问题标题】:allow position relative & top until the div is touching top of page允许位置相对和顶部,直到 div 触及页面顶部
【发布时间】:2016-12-14 08:58:32
【问题描述】:

我已使用vertical-align 将页面内的div 元素(.xasi-modal__login-modal) 对齐到中间,并使用position: relative 从顶部略微偏移-40px

但是,当.xasi-modal__login-modal 到达/触摸页面顶部时,它会在其上方消失。有没有办法让.xasi-modal__login-modal“触摸”页面顶部时,top: -40px 通过 CSSJS(最好是 CSS)禁用?

这是基本的 HTMLCSS

.modal-login__align{
  display: table-cell;
  height: 390px;
  vertical-align: middle;
}
.xasi-modal__login-modal{
  position: relative;
  top: -40px;
  width: 340px;
  height: 390px;
  margin: 0 auto;
}
<div class="modal-login__align">
  <div class="xasi-modal__login-modal"></div>
</div>    

小提琴:https://jsfiddle.net/vde3pwtL/

谢谢。

【问题讨论】:

  • 你真正想要达到的目标是什么?
  • @dreamhunter Is there a way to make it so that when the div "touches" the top of the page, the top: -40px is disabled through CSS or JS (preferably CSS)?
  • 你能给我们演示一下吗,或者小提琴
  • @JishnuVS 检查新编辑 :)
  • 是的,我已经检查了小提琴,有一个红框,里面有一个相对位置的空 div

标签: html css


【解决方案1】:

您可以使用css media queries 来检测max-height

我们可以通过取模态的height 并将负数top 加倍来计算max-height 值(因为我们是垂直居中的)

JSFIDDLE

CSS

@media (max-height:470px){
    .xasi-modal__login-modal{
        top:0;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-12
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多