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

Chrome:应用两个滚动条时的自定义滚动条问题

  •  1
  • connexo  · 技术社区  · 5 年前

    body { background-color: #fff }
    
    section {
      background-color: #f8f8f8;
      width: 400px;
      height: 150px;
      padding: 10px;
      overflow: overlay;
    }
    
    .force-overflow {
      background-image: linear-gradient(45deg, orange, yellow, orange);
      height: 600px;
    }
    
    ::-webkit-scrollbar {
      background-color: #F5F5F5;
      height: 10px;
      width: 10px;
    }
    
    ::-webkit-scrollbar-track {
      background-color: #F5F5F5;
      box-shadow: inset 0 0 3px rgba(0,0,0,0.2);
    }
    
    ::-webkit-scrollbar-thumb {
      background: #999;
    }
    
    ::-webkit-scrollbar-thumb:hover {
      background: #444;
    }
    
    ::-webkit-scrollbar-corner {
      background: transparent;
    }
    <section>
      <div class="force-overflow"></div>
    </section>

    这和预期的一样。

    如果我们加上 水平的 溢出也:

    body { background-color: #fff }
    
    section {
      background-color: #f8f8f8;
      width: 400px;
      height: 150px;
      padding: 10px;
      overflow: overlay;
    }
    
    .force-overflow {
      background-image: linear-gradient(45deg, orange, yellow, orange);
      height: 600px;
      width: 1200px;
    }
    
    ::-webkit-scrollbar {
      background-color: #F5F5F5;
      height: 10px;
      width: 10px;
    }
    
    ::-webkit-scrollbar-track {
      background-color: #F5F5F5;
      box-shadow: inset 0 0 3px rgba(0,0,0,0.2);
    }
    
    ::-webkit-scrollbar-thumb {
      background: #999;
    }
    
    ::-webkit-scrollbar-thumb:hover {
      background: #444;
    }
    
    ::-webkit-scrollbar-corner {
      background: transparent;
    }
    <<div class=“force overflow”></div>

    然后滚动到最右边/最下面,我们得到:

    enter image description here enter image description here

    我知道我可以用 overflow: auto; overflow: overlay; ,但我真的很想坚持 overlay 因为它可以防止你的布局在滚动条出现/消失时“跳跃”。

    有人知道如何解决这个问题吗?

    1 回复  |  直到 5 年前
        1
  •  1
  •   Temani Afif    5 年前

    一个老套的解决方案是添加一些框阴影来覆盖此部分:

    ::-webkit-scrollbar-thumb:horizontal {
      box-shadow: 3px 0 0 0;
    }
    ::-webkit-scrollbar-thumb:vertical {
      box-shadow: 0 3px 0 0;
    }
    

    body { background-color: #fff }
    
    section {
      background-color: #f8f8f8;
      width: 400px;
      height: 200px;
      padding: 10px;
      overflow: overlay;
    }
    
    .force-overflow {
      background-image: linear-gradient(45deg, orange, yellow, orange);
      height: 800px;
      width: 1200px;
    }
    
    ::-webkit-scrollbar {
      background-color: #F5F5F5;
      height: 10px;
      width: 10px;
    }
    
    ::-webkit-scrollbar-track {
      background-color: #F5F5F5;
      box-shadow: inset 0 0 3px rgba(0,0,0,0.2);
    }
    
    ::-webkit-scrollbar-thumb {
      background: #999;
      color:#999;
      transition-duration: 2s;
    }
    ::-webkit-scrollbar-thumb:horizontal {
      box-shadow: 3px 0 0 0;
    }
    ::-webkit-scrollbar-thumb:vertical {
      box-shadow: 0 3px 0 0;
    }
    
    
    ::-webkit-scrollbar-thumb:hover {
      background: #444;
      color:#444;
      transition-duration: 2s;
    }
    ::-webkit-scrollbar-corner {
      background: transparent;
    }
    <section>
      <div class="force-overflow"></div>
    </section>
    推荐文章