【问题标题】:Scrollbar overwrites a div滚动条覆盖 div
【发布时间】:2017-04-07 14:53:37
【问题描述】:

感谢overflow-y: auto;,当滚动条出现在 div 上时,我遇到了问题。

当右侧的 div #bb 有一个滚动条(在其中添加更多行以查看它发生)时,滚动条位于 #help div 上方。 如何让#help div 向左移动,以免被滚动条覆盖?

html,
body {
  overflow: hidden;
  height: 100%;
}
* {
  margin: 0;
  padding: 0;
}
#aa {
  position: fixed;
  top: 0;
  left: 0;
  width: 50%;
  height: 100%;
  overflow-x: hidden;
  overflow-y: auto;
}
#bb {
  position: fixed;
  top: 0;
  left: 50%;
  width: 50%;
  height: 100%;
  background-color: blue;
  overflow-x: hidden;
  overflow-y: auto;
}
#help {
  position: fixed;
  top: 0;
  right: 0;
  background-color: green;
}
<div id="aa" contenteditable>a
  <br>few
  <br>lines
</div>
<div id="bb" contenteditable>please add more
  <br>lines here to make
  <br>the scrollbar appear!</div>
<div id="help">?</div>

【问题讨论】:

    标签: html css


    【解决方案1】:

    我改变了一些东西,我使用 flexbox 让它们彼此相邻,我已经使两半的位置相对。我已经将? 放入#bb 并将其设置为绝对位置。

    html,
    body {
      overflow: hidden;
      height: 100%;
    }
    * {
      margin: 0;
      padding: 0;
    }
    .flex-container {
      display: flex;
      width: 100vw;
      height: 100vh
    }
    #aa {
      position: relative;
      flex: 1;
      height: 100%;
      overflow-x: hidden;
      overflow-y: auto;
    }
    #bb {
      position: relative;
      flex: 1;
      height: 100%;
      background-color: blue;
      overflow-x: hidden;
      overflow-y: scroll;/* Just so the bar is there */
    }
    #help {
      position: absolute;
      top: 0;
      right: 0;
      background-color: green;
    }
    <div class="flex-container">
      <div id="aa" contenteditable>a
        <br>few
        <br>lines
      </div>
      <div id="bb" contenteditable>
        I've made it always
        <br>so the scrollbar is
        <br>there but you can
        <br>change it back to auto
        <div id="help">?</div>
      </div>
    </div>

    希望这有帮助。

    编辑

    我已经考虑过了。我认为这可能是一个更好的方法。

    html,
    body {
      min-height: 100vh;
    }
    * {
      margin: 0;
      padding: 0;
    }
    #aa {
      position: fixed;
      top: 0;
      left: 0;
      width: 50%;
      height: 100vh;
    }
    #bb {
      margin-left: 50%;
      min-height: 100vh;
      background-color: blue;
    }
    #help {
      position: fixed;
      top: 0;
      right: 0;
      background-color: green;
    }
    <div id="aa" contenteditable>a
      <br>few
      <br>lines
    </div>
    <div id="bb" contenteditable>please add more
      <br>lines here to make
      <br>lines here to make
      <br>lines here to make
      <br>lines here to make
      <br>lines here to make
      <br>lines here to make
      <br>lines here to make
      <br>lines here to make
      <br>lines here to make
      <br>lines here to make
      <br>lines here to make
      <br>lines here to make
      <br>lines here to make
      <br>lines here to make
      <br>lines here to make
      <br>the scrollbar appear!
    </div>
    <div id="help">?</div>

    这使用了body的滚动来滚动右栏(左栏是固定的)。这意味着right:0; 将考虑滚动条。

    【讨论】:

    • 有一个滚动条始终存在的好技巧(不需要时它被禁用),这样我们就可以将? 放置在正确的位置。但不幸的是,我不希望滚动条在不需要时出现......
    • @Basj 你读过我写的吗? “我一直这样做,所以滚动条在那里,但你可以将它改回自动”该栏只是在那里显示它工作,随意让它自动,它仍然可以工作:-)
    • 是的@AndrewBone 但是然后?当我向#bb 添加新行时完全消失(至少在 FF47 Windows 7 上)
    • @Basj 你的意思是当你向下滚动时它会消失吗?
    • 是的,目前确实如此。 (但滚动条始终存在的事实无论如何都不是我们想要的。如果我移动到 auto 而不是 scroll 它是一样的 ? 消失)
    【解决方案2】:

    简单的方法是将 bb div 宽度设置为 48%

    html,
    body {
      overflow: hidden;
      height: 100%;
    }
    * {
      margin: 0;
      padding: 0;
    }
    #aa {
      position: fixed;
      top: 0;
      left: 0;
      width: 50%;
      height: 100%;
      overflow-x: hidden;
      overflow-y: auto;
    }
    #bb2 {
      position: fixed;
      top: 0;
      left: 50%;
      width: 50%;
      height: 100%;
      background-color: blue;
    }
    #bb {
      position: fixed;
      top: 0;
      left: 50%;
      width: 48%;
      height: 100%;
      background-color: blue;
      overflow-x: hidden;
      overflow-y: auto;
    }
    #help {
      position: fixed;
      top: 0;
      right: 0;
      background-color: green;
    }
    <div id="aa" contenteditable>a
      <br>few
      <br>lines
    </div>
    <div id="bb2"></div>
    <div id="bb" contenteditable>please add more
      <br>lines here to make
      <br>the scrollbar appear!</div>
    <div id="help">?</div>

    【讨论】:

    • 谢谢,但它打破了滚动条触摸屏幕右边界的通常用户体验......
    • 我在这篇文章中找到了一个 javascript 函数 --> stackoverflow.com/questions/9333379/… 看看这个。
    • 或者我想了一个技巧,在 'bb' 下设置第二个空 div,但宽度为 50%,背景为蓝色,它应该适合这项工作。
    • @linearSpin 的问题是这不是一个事件,所以我无法知道什么时候会发生......
    • 同理@linearSpin:滚动条不靠近屏幕边界,这违反了用户体验标准...
    猜你喜欢
    • 2013-07-07
    • 2015-12-20
    • 2012-09-28
    • 2022-08-16
    • 2015-05-18
    • 1970-01-01
    • 1970-01-01
    • 2014-09-15
    相关资源
    最近更新 更多