【问题标题】:How to add left and right padding to ::webkit-scrollbar-thumb?如何向 ::webkit-scrollbar-thumb 添加左右填充?
【发布时间】:2021-06-15 00:03:08
【问题描述】:

有没有办法复制 YouTube 的自定义滚动条?

想要的效果:

似乎 ::webkit-scrollbar-thumb 具有左右填充来实现该效果。

我尝试了以下方法:

* {
  margin: 0;
  padding: 0;
  ::-webkit-scrollbar {
    background: #181818;
    width: 12px;
  }
  ::-webkit-scrollbar-thumb {
    padding: 0 4px; // This was supposed to do the trick
    background: #909090;
    border-radius: 100px;
    &:hover {
      background: #606060;
    }
  }
}

但它不起作用......

结果:

任何想法如何达到预期的效果? 提前致谢。

【问题讨论】:

    标签: html css sass scrollbar


    【解决方案1】:

    您可以尝试使用background-clip: padding-box 伪造填充并应用透明的左右边框。

    ::-webkit-scrollbar {
      background: #181818;
      width: 20px;
    }
    
     ::-webkit-scrollbar-thumb {
      padding: 0 4px;
      border-right:4px solid transparent;
      border-left:4px solid transparent;
      background: #909090;
      background-clip: padding-box;
      border-radius: 100px;
      &:hover {
        background: #606060;
      }
    }
    
    html,
    body {
      height: 300%;
    }

    【讨论】:

    • 由于某种原因,悬停效果被窃听。当我将鼠标悬停在滚动条上时,它会失去“填充”效果。
    • 没关系。刚刚更改了“背景颜色”的“背景”属性,现在问题已解决。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-10
    • 2020-10-01
    • 2021-12-07
    • 2012-09-15
    • 2012-07-04
    • 2014-04-18
    相关资源
    最近更新 更多