如何制作滚动条覆盖内容

Yun*_*lva 11 html css scrollbar

如何让滚动条覆盖 div 内容?

已经尝试使用track透明背景,但它不起作用

::-webkit-scrollbar-track {background: transparent}
Run Code Online (Sandbox Code Playgroud)

保持不变,占据部分内容

在此处输入图片说明

::-webkit-scrollbar-track {background: transparent}
Run Code Online (Sandbox Code Playgroud)
.faq-body {
  width: 250px;
  height: 400px;
  background: #fff;
  overflow: scroll;
  border: 1px solid #7b7d7f;
}

.faq-body::-webkit-scrollbar {
  width: 16px;
}
.faq-body::-webkit-scrollbar-thumb {
  background-color: #7b7d7f;
  border: 5px solid #fff;
  border-radius: 10rem;
}
.faq-body::-webkit-scrollbar-track {
  position: absolute;
  right: -3rem;
  top: -50rem;
  background: transparent;
}

.faq-question {
  padding: 20px;
  border-bottom: 1px solid #000;
  line-height: 1.3;
  color: #15191b;
  font-size: 0.8rem;
}
Run Code Online (Sandbox Code Playgroud)

有人能帮我吗?

Vit*_*nço 6

有两种方法可以获得类似的结果:

Javascript 库

使用http://grsmto.github.io/simplebar/

https://jsfiddle.net/w0a5Ls6h/

亲:

  • 浏览器兼容性
  • 满意的结果

缺点:

  • 第三个javascript

或者

只有 CSS

<style>
  .faq-body {
      width: 250px;
      height: 400px;
      background: #fff;
      overflow-y: scroll;
      border: 1px solid #7b7d7f;
    }

    .faq-body::-webkit-scrollbar {
      width: 7px;
    }
    .faq-body::-webkit-scrollbar-thumb {
      background-color: rgba(0,0,0,0.4);
      border-radius: 10rem;
      border: 1px solid #fff;
    }


    .faq-body::-webkit-scrollbar-track-piece:start {
      background: transparent;
    }

    .faq-body::-webkit-scrollbar-track-piece:end {
      background: transparent;
    }
    .faq-question {
      padding: 20px;
      border-bottom: 1px solid #000;
      line-height: 1.3;
      color: #15191b;
      font-size: 0.8rem;
    }
</style>
Run Code Online (Sandbox Code Playgroud)

缺点:

  • 浏览器兼容性
  • 类似的结果但并不令人满意


And*_*rew 5

overflow: overlay适用于某些浏览器(Chrome、Edge),但不适用于 Firefox。据说scrollbar-gutter应该有所帮助,但似乎还没有任何浏览器实现了这一点。

以下是浏览器支持的参考: https://caniuse.com/css-overflow-overlay

.faq-body {
  width: 250px;
  height: 400px;
  background: #fff;
  overflow-y: overlay;
  border: 1px solid #7b7d7f;
}

.faq-body::-webkit-scrollbar {
  width: 16px;
}
.faq-body::-webkit-scrollbar-thumb {
  background-color: #7b7d7f;
  border: 5px solid #fff;
  border-radius: 10rem;
}
.faq-body::-webkit-scrollbar-track {
  position: absolute;
  right: -3rem;
  top: -50rem;
  background: transparent;
}

.faq-question {
  padding: 20px;
  border-bottom: 1px solid #000;
  line-height: 1.3;
  color: #15191b;
  font-size: 0.8rem;
}
Run Code Online (Sandbox Code Playgroud)
<div class="faq-body">
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
  </div>
Run Code Online (Sandbox Code Playgroud)