[关闭]
@rushui2018 2016-05-17T11:55:53.000000Z 字数 2677 阅读 982

滚动条定制

scrollbar


最近遇到一个需求要求定制滚动条:

chrome对于滚动条定制有很多相关css就能实现的伪类:

  1. ::-webkit-scrollbar { /* 1 */ }
  2. ::-webkit-scrollbar-button { /* 2 */ }
  3. ::-webkit-scrollbar-track { /* 3 */ }
  4. ::-webkit-scrollbar-track-piece { /* 4 */ }
  5. ::-webkit-scrollbar-thumb { /* 5 */ }
  6. ::-webkit-scrollbar-corner { /* 6 */ }
  7. ::-webkit-resizer { /* 7 */ }
  8. :horizontal
  9. :vertical
  10. :decrement
  11. :increment
  12. :start
  13. :end
  14. :double-button
  15. :single-button
  16. :no-button
  17. :corner-present
  18. :window-inactive

:horizontal

The horizontal pseudo-class applies to any scrollbar pieces that have a horizontal orientation.

:vertical

The vertical pseudo-class applies to any scrollbar pieces that have a vertical orientation.

:decrement

The decrement pseudo-class applies to buttons and track pieces. It indicates whether or not the button or track piece will decrement the view’s position when used (e.g., up on a vertical scrollbar, left on a horizontal scrollbar).

:increment

The increment pseudo-class applies to buttons and track pieces. It indicates whether or not a button or track piece will increment the view’s position when used (e.g., down on a vertical scrollbar, right on a horizontal scrollbar).

:start

The start pseudo-class applies to buttons and track pieces. It indicates whether the object is placed before the thumb.

:end

The end pseudo-class applies to buttons and track pieces. It indicates whether the object is placed after the thumb.

:double-button

The double-button pseudo-class applies to buttons and track pieces. It is used to detect whether a button is part of a pair of buttons that are together at the same end of a scrollbar. For track pieces it indicates whether the track piece abuts a pair of buttons.

:single-button

The single-button pseudo-class applies to buttons and track pieces. It is used to detect whether a button is by itself at the end of a scrollbar. For track pieces it indicates whether the track piece abuts a singleton button.

:no-button

Applies to track pieces and indicates whether or not the track piece runs to the edge of the scrollbar, i.e., there is no button at that end of the track.

:corner-present

Applies to all scrollbar pieces and indicates whether or not a scrollbar corner is present.

:window-inactive

Applies to all scrollbar pieces and indicates whether or not the window containing the scrollbar is currently active. (In recent nightlies, this pseudo-class now applies to ::selection as well. We plan to extend it to work with any content and to propose it as a new standard pseudo-class.)

我的自定义滚动条样式

  1. .box-scroller{
  2. background:rgb(94, 128, 204);
  3. }
  4. .box-scroller::-webkit-scrollbar {
  5. box-shadow: inset 0 0 0 4px rgb(94, 128, 204);
  6. background: -webkit-linear-gradient(
  7. left,
  8. #5e80cc 0%,
  9. rgb(133,219, 218) 50%,
  10. #5e80cc 100%
  11. );
  12. width: 10px;
  13. }
  14. .box-scroller::-webkit-scrollbar-thumb {
  15. background: -webkit-linear-gradient(
  16. top,
  17. rgba(0, 0, 0, 0) 29%,
  18. #85dbda 30%,
  19. #85dbda 70%,
  20. rgba(0, 0, 0, 0) 71%
  21. );
  22. }
  23. .box-scroller::-webkit-scrollbar-thumb:hover {
  24. background: -webkit-linear-gradient(
  25. top,
  26. rgba(133, 219, 218,.2) 0%,
  27. rgba(0, 0, 0, 0) 29%,
  28. #85dbda 30%,
  29. #85dbda 70%,
  30. rgba(0, 0, 0, 0) 71%,
  31. rgba(133, 219, 218,.2) 100%
  32. );
  33. }

定制结果如下
如下:
自定义的滚动条

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注