使用转换:: - webkit-scrollbar?

Bus*_*sti 23 css webkit scrollbar css-transitions

是否可以在webkit滚动条上使用转换?我试过了:

div#main::-webkit-scrollbar-thumb {
    background: rgba(255,204,102,0.25);
    -webkit-transition: background 1s;
    transition: background 1s;
}

div#main:hover::-webkit-scrollbar-thumb {
    background: rgba(255,204,102,1);
}
Run Code Online (Sandbox Code Playgroud)

但它不起作用.或者是否可以创建类似的效果(没有JavaScript)?

这是一个显示rgba过渡问题的jsfiddle

bri*_*out 13

background-color: inherit;除此之外,使用Mari M的技术也很容易实现-webkit-background-clip: text;.

现场演示; https://jsfiddle.net/s10f04du/

@media screen and (-webkit-min-device-pixel-ratio:0) { 
  .container {
    overflow-y: scroll;
    overflow-x: hidden;
    background-color: rgba(0,0,0,0);
    -webkit-background-clip: text;
    transition: background-color .8s;
  }
  .container:hover {
    background-color: rgba(0,0,0,0.18);  
  }
  .container::-webkit-scrollbar-thumb {
    background-color: inherit;
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 这种方法在滚动期间留下某种文本跟踪.请检查 - https://jsfiddle.net/s10f04du/62/ (4认同)

Dav*_*der 11

通过对后台使用的CSS 变量进行动画处理可以实现干净的解决方案。请注意,为了能够对 CSS 变量进行动画处理,您需要使用@property.

html,
body {
  height: 100%;
  margin: 0;
  overflow: hidden;
}

@property --var1 {
  syntax: "<color>";
  inherits: true;
  initial-value: white;
}

.container {
  height: 100%;
  width: 100%;
  overflow-y: scroll;
  transition: --var1 .5s;
}

.container:hover {
  --var1: #aaa;
}

.container::-webkit-scrollbar {
  background: white;
  width: 8px;
}

.container::-webkit-scrollbar-thumb {
  background: var(--var1);
  border-radius: 4px;
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">
  test
  <div style="height: 1000px; width:100%;"></div>
</div>
Run Code Online (Sandbox Code Playgroud)


Muh*_*him 9

简短回答:不,不可能transition::-webkit-scrollbar

长答案:有办法完全在 CSS 中实现类似的效果。

解释:

  1. 我们需要创建一个可以滚动的外部容器。然后我们要创建一个内部容器。
  2. 外部容器将有一个background-color属性。此属性将匹配我们要为滚动条过渡的颜色。
  3. 内部容器将与外部容器的高度相匹配,并且将具有background-color遮蔽外部容器的 。
  4. 滚动条background-color将继承外部容器。
  5. transition属性将应用于background-color外部容器的 。

这里的一个主要缺点是你必须做一些掩蔽。如果您的背景不是纯色,这可能会有点麻烦,因为内部容器很可能需要匹配。如果这不担心,这对你有用。这是将所有内容组合在一起的代码,用于具有水平滚动组件的页面。

HTML

<div id="container-outer">
    <div id="container-inner">
        <!-- Content goes here -->
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS

    /* Scrollbar */
    ::-webkit-scrollbar {
        border: 0;
        height: 10px;
    }
    ::-webkit-scrollbar-track {
        background: rgba(0,0,0,0);
    }
    ::-webkit-scrollbar-thumb {
        background-color: inherit; /* Inherits from outer container */
        border-radius: 20px;
    }

    /* Container */
    #container-outer {
        overflow-y: hidden;
        overflow-x: scroll; /* Horizontal-only scrolling */
        height: 400px;
        background-color: white; /* Initial color of the scrollbar */
        transition: background-color 200ms;
    }
    #container-outer:hover {
        background-color: red; /* Hover state color of the scrollbar */
    }
    #container-inner {
        background-color: white; /* Masks outer container */
        font-size: 0;
        height: inherit; /* Inherits from outer container */
        width: 10000px; /* Set to see the scrolling effect */
    }
Run Code Online (Sandbox Code Playgroud)

笔记:

  • 应该是显而易见的,但这是一个 Webkit 解决方案。该问题专门针对 Webkit 而不是其他任何问题,因此此答案仅针对 Webkit。
  • 您的外部容器可能需要一个max-width与您的内部容器相匹配的width,否则您可能会在超大显示器上看到一些奇怪的东西。这是浏览器宽度大于水平滚动内容宽度时的边缘情况。这是假设您使用水平滚动,就像示例代码那样。
  • 在大多数情况下,悬停样式在移动设备上无法按预期工作。考虑到 Webkit 移动浏览器的巨大市场渗透率,这是一个问题。在使用此解决方案之前考虑到这一点。


wat*_*lea 8

这是基于这里回复的另一个想法.您可以使用颜色代替背景颜色,然后使用框阴影来为拇指着色(您甚至可以像我一样破解文本阴影,显然不是生产就绪的方法,但您可以在子容器中覆盖颜色):

https://codepen.io/waterplea/pen/dVMopv

*::-webkit-scrollbar-thumb {        
  box-shadow: inset 0 0 0 10px;
}

div {
  overflow: auto;
  color: rgba(0, 0, 0, 0);
  transition: color .3s ease;
}

div:hover {
  color: rgba(0, 0, 0, 0.3);
}
Run Code Online (Sandbox Code Playgroud)

  • 对我来说,这个解决方案改变了可滚动容器内文本的颜色。 (3认同)

Tre*_*ery 7

改编自@brillout 的回答,如果我们转换border-color而不是background-color,我们可以避免使用background-clip: text,如果您有任何嵌套文本,则会留下一些片段。

完整解释:

  1. 将您的内容放在某个包装器 div 中,然后border-color在您的包装器上添加过渡以更改悬停时的颜色。
  2. 向滚动条添加一个内嵌边框,并将宽度设置得足够大以填充整个元素。
  3. border-color: inherit在滚动条上设置。

现在,当我们将鼠标悬停在包装器上时,边框颜色将发生变化。包装器没有边框,所以我们看不到任何事情发生。但是,滚动条继承了该颜色,因此滚动条颜色会发生变化。

这是最重要的代码。这个小提琴和下面的代码片段中有一个完整的例子。

#scroller {
  /* fill parent */
  display: block;
  width: 100%;
  height: 100%;

  /* set to some transparent color */
  border-color: rgba(0, 0, 0, 0.0);
  /* here we make the color transition */
  transition: border-color 0.75s linear;
  /* make this element do the scrolling */
  overflow: auto;
}

#scroller:hover {
  /* the color we want the scrollbar on hover */
  border-color: rgba(0, 0, 0, 0.1);
}

#scroller::-webkit-scrollbar,
#scroller::-webkit-scrollbar-thumb,
#scroller::-webkit-scrollbar-corner {
  /* add border to act as background-color */
  border-right-style: inset;
  /* sum viewport dimensions to guarantee border will fill scrollbar */
  border-right-width: calc(100vw + 100vh);
  /* inherit border-color to inherit transitions */
  border-color: inherit;
}
Run Code Online (Sandbox Code Playgroud)
<div id="scroller">...</div>
Run Code Online (Sandbox Code Playgroud)

body {
  background: whitesmoke;
}

#wrapper {
  width: 150px;
  height: 150px;
  margin: 2em auto;
  box-shadow: 0 0 15px 5px #ccc;
}

#scroller {
  /* fill parent */
  display: block;
  width: 100%;
  height: 100%;
}

#content {
  display: block;
  width: 300px;
  height: auto;
  padding: 5px;
}

#scroller {
  /* The background-color of the scrollbar cannot be transitioned.
     To work around this, we set and transition the property
     of the wrapper and just set the scrollbar to inherit its
     value. Now, when the the wrapper transitions that property,
     so does the scrollbar. However, if we set a background-color,
     this color shows up in the wrapper as well as the scrollbar.
     Solution: we transition the border-color and add a border-right
     to the scrollbar that is as large as the viewport. */
  border-color: rgba(0, 0, 0, 0.0);
  transition: border-color 0.75s linear;
  /* make this element do the scrolling */
  overflow: auto;
}

#scroller:hover {
  border-color: rgba(0, 0, 0, 0.1);
  transition: border-color 0.125s linear;
}

#scroller::-webkit-scrollbar,
#scroller::-webkit-scrollbar-thumb,
#scroller::-webkit-scrollbar-corner {
  /* add border to act as background-color */
  border-right-style: inset;
  /* sum viewport dimensions to guarantee border will fill scrollbar */
  border-right-width: calc(100vw + 100vh);
  /* inherit border-color to inherit transitions */
  border-color: inherit;
}

#scroller::-webkit-scrollbar {
  width: 0.5rem;
  height: 0.5rem;
}

#scroller::-webkit-scrollbar-thumb {
  border-color: rgba(0, 0, 0, 0.1);
  /* uncomment this to hide the thumb when not hovered */
  /* border-color: inherit; */
}

#scroller::-webkit-scrollbar-thumb:hover {
  border-color: rgba(0, 0, 0, 0.15);
}

#scroller::-webkit-scrollbar-thumb:active {
  border-color: rgba(0, 0, 0, 0.2);
}
Run Code Online (Sandbox Code Playgroud)
<div id="wrapper">
  <div id="scroller">
    <div id="content">
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum pretium mi felis, pharetra ornare lorem pellentesque pulvinar. Donec varius condimentum nunc at mollis. Pellentesque posuere et quam eu tristique. Integer interdum enim non interdum mattis. Suspendisse gravida nibh enim, non efficitur lacus suscipit quis. Etiam pharetra libero auctor ultricies ornare. Duis dapibus semper semper. Nam sit amet lobortis arcu.

  Maecenas fermentum risus quis justo convallis, non ornare erat fringilla. Cras eleifend leo sapien, ac iaculis orci ultricies sed. Praesent ultrices accumsan risus, pharetra pharetra lorem dignissim id. Aenean laoreet fringilla eros, vel luctus eros luctus sed. Nullam fermentum massa sit amet arcu dictum, nec bibendum lectus porta. Duis pellentesque dui sed nisi ultricies, vitae feugiat dui accumsan. Nam sollicitudin, ex et viverra ultricies, justo metus porttitor quam, quis vestibulum nibh nisl eget leo.

  Integer luctus arcu et sapien accumsan fringilla. Integer mollis tellus vel imperdiet elementum. Ut consequat ac nibh ac sagittis. Duis neque purus, pellentesque nec erat id, pharetra ornare sapien. Etiam volutpat tincidunt nunc ac facilisis. Aenean sed purus pellentesque, vehicula mauris porta, fringilla nibh. Ut placerat, risus et congue rutrum, lorem arcu aliquet urna, sollicitudin venenatis lorem eros et diam. Aliquam sodales ex risus, ac vulputate ipsum porttitor vel. Pellentesque mattis nibh orci. Morbi turpis nulla, tincidunt vitae tincidunt in, sodales et arcu. Nam tincidunt orci id sapien venenatis porttitor ut eu ipsum. Curabitur turpis sapien, accumsan eget risus et, congue suscipit ligula.

  Maecenas felis quam, ultrices ac ornare nec, blandit at leo. Integer dapibus bibendum lectus. Donec pretium vehicula velit. Etiam eu cursus ligula. Nam rhoncus diam lacus, id tincidunt dui consequat id. Ut eget auctor massa, quis laoreet risus. Nunc blandit sapien non massa bibendum, ac auctor quam pellentesque. Quisque ultricies, est vitae pharetra hendrerit, elit enim interdum odio, eu malesuada nibh nulla a nisi. Ut quam tortor, feugiat sit amet malesuada eu, viverra in neque. Nullam lacinia justo sit amet porta interdum. Etiam enim orci, rutrum sit amet neque non, finibus elementum magna. Sed ac felis quis nunc fermentum suscipit.

  Ut aliquam placerat nulla eget aliquam. Phasellus sed purus mi. Morbi tincidunt est dictum, faucibus orci in, lobortis eros. Etiam sed viverra metus, non vehicula ex. Sed consectetur sodales felis, vel ultrices risus laoreet eget. Morbi ut eleifend lacus, ac accumsan risus. Donec iaculis ex nec ante efficitur vestibulum.
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)