透明滚动条与CSS

use*_*950 21 html css scrollbar

现在使用此代码(以及此代码的许多变体),但滚动轨迹变为深灰色,类似#222222或附近.找到很多例子,但是所有例子都给出了相同的结果.Opera,Chrome和Firefox显示此错误.怎么修?

#style-3::-webkit-scrollbar-track {
    -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
    background-color: transparent;
}

#style-3::-webkit-scrollbar {
    width: 6px;
    background-color: transparent;
}

#style-3::-webkit-scrollbar-thumb {
    background-color: #000000;
}
Run Code Online (Sandbox Code Playgroud)

kar*_*ice 32

如果你使用这个:

body {
    overflow: overlay;
}
Run Code Online (Sandbox Code Playgroud)

然后滚动条也会在整个页面上采用透明背景。这也会将滚动条放在页面内,而不是删除一些宽度以放入滚动条。

这是一个演示代码。我无法将它放入任何 codepen 或 jsfiddle 中,显然我花了一段时间才弄明白,但它们没有显示透明度,我不知道为什么。

但是把它放在一个 HTML 文件中应该没问题。

能够把它放在小提琴上:https : //jsfiddle.net/3awLgj5v/

<!DOCTYPE html>
<html>
<style>
html, body {
  margin: 0;
  padding: 0;
}

body {
  overflow: overlay;
}

.div1 {
  background: grey;
  margin-top: 200px;
  margin-bottom: 20px;
  height: 20px;
}

::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}

::-webkit-scrollbar-thumb {
  background: rgba(90, 90, 90);
}

::-webkit-scrollbar-track {
  background: rgba(0, 0, 0, 0.2);
}
</style>
  
<body>

<div class="div1"></div>

<div class="div1"></div>

<div class="div1"></div>

<div class="div1"></div>

<div class="div1"></div>
  

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我想测试它的最佳方法是创建一个本地 html 文件。

您还可以将其应用于其他元素,例如任何滚动框。在使用检查器模式时,您可能必须将溢出置于隐藏状态,然后再返回到其他任何内容。它可能需要刷新。之后应该可以在滚动条上工作而无需再次刷新它。请注意,这是针对检查员模式的。

  • “overlay”值现已弃用。https://developer.mozilla.org/en-US/docs/Web/CSS/overflow#Values (6认同)
  • 如何在 div 元素上实现这种效果? (2认同)
  • 好的,但是还有什么选择呢?每个人都立即指出“overlay”已被弃用,但没有人知道有什么替代方案。如果没有,为什么会被贬低? (2认同)

Voi*_*rit 12

使用纯css时,不可能使其透明.你必须使用这样的透明背景图像:

::-webkit-scrollbar-track-piece:start {
    background: transparent url('images/backgrounds/scrollbar.png') repeat-y !important;
}

::-webkit-scrollbar-track-piece:end {
    background: transparent url('images/backgrounds/scrollbar.png') repeat-y !important;
}
Run Code Online (Sandbox Code Playgroud)

  • 这是有关轨道背景颜色的第一个准确陈述.这么多人假设它没有实际测试它的例子.谢谢! (2认同)

4dg*_*rav 7

    .scrollable-content {
      overflow-x:hidden;
      overflow-y:scroll; // manage scrollbar content overflow settings
    }
    .scrollable-content::-webkit-scrollbar {
      width:30px; // manage scrollbar width here
    }
    .scrollable-content::-webkit-scrollbar * {
      background:transparent; // manage scrollbar background color here
    }
    .scrollable-content::-webkit-scrollbar-thumb {
      background:rgba(255,0,0,0.1) !important; // manage scrollbar thumb background color here
    }
Run Code Online (Sandbox Code Playgroud)

  • 现在 - 只是默认滚动条。 (2认同)
  • 我一遍又一遍地看到了这一点。背景颜色不适用于滚动条轨道或轨道块(我很确定其他元素也是如此)。滚动条轨道似乎从 body 标签中获取它的颜色。 (2认同)

小智 6

将此代码嵌入到您的 css 中。

::-webkit-scrollbar {
    width: 0px;
}
Run Code Online (Sandbox Code Playgroud)

/* 追踪 */

::-webkit-scrollbar-track {
    -webkit-box-shadow: none;
}
Run Code Online (Sandbox Code Playgroud)

/* 处理 */

::-webkit-scrollbar-thumb {
    background: white;
    -webkit-box-shadow: none;
}

::-webkit-scrollbar-thumb:window-inactive {
    background: none;
}
Run Code Online (Sandbox Code Playgroud)


Emi*_*bos 6

标准方法(目前仅适用于 Firefox)是:

:root {
  scrollbar-color: transparent transparent;
}
Run Code Online (Sandbox Code Playgroud)