代码之家  ›  专栏  ›  技术社区  ›  extraxt

只滚动模态

css
  •  1
  • extraxt  · 技术社区  · 7 年前

    我做了一个设定高度和宽度的模型。

    当我单击按钮打开模式时,一个新类( .modal-open )添加到 BODY 标签。现在 身体 无法滚动。这就是我想要的,没问题。

    .modal-open {
      overflow: hidden;
    }
    

    enter image description here

    enter image description here

    我现在想做的是我想不通的事。

    我想阻止尸体滚动而不是设置 height 属性,以便我可以在不使用 Y 纸卷。这样地:

    enter image description here

    我能做些什么来滚动模态的全部内容而不被卡住?

    这是我的CSS模态类:

    .my-modal {
      background-color: $color-ink;
      position: fixed;
      width: 100vw;
      height: 100vh;
      top: 0;
      left: 0;
    
      &__content {
        position: fixed;
        // overflow-y: scroll;
        top: 20%;
        left: 50vw;
        transform: translateX(-50%);
        border-radius: 5px;
        max-width: 60rem;
        // height: 50vh;
      }
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Bryce Howitson    7 年前

    如果没有看到剩下的代码,很难确定。但假设Content元素在 my-modal 你应该用 position: absolute 而不是固定的内部。

    Position fixed会产生奇怪的效果,因为它与主体的绑定方式与Position absolute不同。absolute引用堆栈中下一个显式定位的父元素,因此您的位置应该保持不变。

    你这样做之后, height overflow-y 在内容上应该产生你期望的效果。