CSS3 custom scroll bar style, css3 custom scroll bar
Do you think that the original scroll bar that comes with the browser is not beautiful? At the same time, you can see that the custom scroll bars of many websites appear high-end. Even the chrome32.0 Development Board discards the original scroll bar, which is more beautiful. Then how does the webkit browser customize the scroll bar?
Preface
Webkit supports areas with overflow attributes, list boxes, drop-down menus, and custom style of the textarea scroll bar, so it is very useful. Of course, the scroll bar style compatible with all browsers does not currently exist.
Demo
Let's take a look at the two scroll bar demos: demo1 (image version) and demo2 (pure CSS3 version)
Scroll bar Composition
- :-Webkit-scrollbar overall part of the scroll bar
- :-Webkit-scrollbar-thumb the small block in the scroll bar can be moved up and down (or to the left to the right, depending on the vertical scroll bar or horizontal scroll bar)
- :-Webkit-scrollbar-track the track of the scroll bar (which contains Thumb)
- :-Webkit-scrollbar-button the buttons at both ends of the track of the scroll bar. You can click the button to fine-tune the position of the small square.
- :-Webkit-scrollbar-track-piece inner orbital, middle part of the scroll bar (excluded)
- :-Webkit-scrollbar-corner, that is, the intersection of two scroll bars
- :-Webkit-resizer: a widget used to adjust the element size by dragging at the intersection of two scroll bars
Simplified Version
The detailed code will not be posted here. In the demo, you can view the source code to find the specific style settings. Let's see the style of the second scroll bar in demo2.
[Css]View plain copy
- /* Define the size of the horizontal and vertical scroll bars corresponding to the height and width of the scroll bar and the background height */
- :-Webkit-scrollbar
- {
- Width: 16px;
- Height: 16px;
- Background-color: # F5F5F5;
- }
- /* Define the shadow + rounded corner of the scroll bar track */
- :-Webkit-scrollbar-track
- {
- -Webkit-box-shadow: inset 0 0 6px rgba (0.3, 0 );
- Border-radius: 10px;
- Background-color: # F5F5F5;
- }
- /* Define the shadow + rounded corner in the slider */
- :-Webkit-scrollbar-thumb
- {
- Border-radius: 10px;
- -Webkit-box-shadow: inset 0 0 6px rgba (0, 0,. 3 );
- Background-color: #555;
- }
Detailed settings
Defining a scroll bar is to use pseudo elements and pseudo classes. What are pseudo elements and pseudo classes?
We should be familiar with the pseudo class: link,: focus,: hover. In addition, many pseudo class selectors are added in CSS3, such as nth-child,: last-child ,: nth-last-of-type.
The pseudo elements in CSS have been viewed before: first-line,: first-letter,: before,: after. In CSS3, the pseudo element is adjusted, and a ":" is added based on the previous one, that is, the ": first-letter,: first-line ,:: before,: after, and ": selection" is added to CSS3 ". Two ":" and one ":" are mainly used in css3 to distinguish between pseudo classes and pseudo elements.
Webkit pseudo-classes and pseudo-elements are strongly implemented. You can define the scroll bar as a page element and combine them with some advanced CSS3 attributes, such as gradient, rounded corners, and RGBa. If you want to use an image in some places, you can convert the image to Base64. Otherwise, you have to load the multiple images each time to increase the number of requests.
Any object can be set: border, shadow, background image, etc. Any created scroll bar can complete interaction based on the operating system settings. The following pseudo classes can be applied to the above pseudo elements. It's a little complicated. For details, see the first demo, where there are annotations.
[Css]View plain copy
- : Horizontal
- // The horizontal pseudo class applies to the scroll bars in any horizontal direction.
- : Vertical
- // The vertical pseudo class applies to the scroll bars in any vertical direction.
- : Decrement
- // The decrement pseudo class applies to buttons and orbital fragments. Indicates a descending button or orbital fragment, for example, a region or button that can move the area up or down to the right.
- : Increment
- // The increment pseudo class applies to buttons and orbital fragments. Indicates an incremental button or orbital fragment. For example, you can move the area down or to the left.
- : Start
- // The start pseudo-class applies to buttons and orbital fragments. Indicates whether the object (Button rail fragment) is placed in front of the slider
- : End
- // The end pseudo class applies to buttons and orbital fragments. Indicates whether the object (Button rail fragment) is placed behind the slider
- : Double-button
- // The double-button pseudo class applies to buttons and orbital fragments. Determines whether the end of the track is a pair of buttons. That is, orbital fragments are placed together with a pair of buttons.
- : Single-button
- // The single-button pseudo class applies to buttons and orbital fragments. Determines whether the end of the track is a button. That is, the orbital fragment is placed next to a separate button.
- : No-button
- The no-button pseudo-class indicates that there is no button at the end of the track.
- : Corner-present
- // The corner-present pseudo class indicates whether the corner of the scroll bar exists.
- : Window-inactive
- // Applicable to all the scroll bars, indicating the areas that contain the scroll bars. The focus is not in this window.
- :-Webkit-scrollbar-track-piece: start {
- /* Top half or left half of the scroll bar */
- }
- :-Webkit-scrollbar-thumb: window-inactive {
- /* When the focus is not in the status of the slider in the current area */
- }
- :-Webkit-scrollbar-button: horizontal: decrement: hover {
- /* When the mouse is in the status of the button under the horizontal scroll bar */
- }
Address: http://www.xuanfengge.com/css3-webkit-scrollbar.html