


CSS3 custom scroll bar style: detailed explanation of webkit-scrollbar
Do you think the original scroll bars that come with the browser are very unsightly? At the same time, you have also seen that the customized scroll bars of many websites look high-end. Even the chrome32.0 development board has abandoned the original scroll bars and is much more beautiful. . So how does the webkit browser customize the scroll bar?
Preface
Webkit supports areas with overflow attributes, list boxes, drop-down menus, and textarea scroll bar custom styles, so it is quite useful. Of course, there is currently no scroll bar style that is compatible with all browsers.
Demo
Let’s take a look at these two scroll bar demos: demo1 (picture version), demo2 (pure CSS3 version)
Scroll bar composition
::-webkit-scrollbar The whole part of the scroll bar
::-webkit-scrollbar-thumb The small square inside the scroll bar can Move up and down (or left and right, depending on whether it is a vertical scroll bar or a horizontal scroll bar)
::-webkit-scrollbar-track The track of the scroll bar (which contains Thumb)
::-webkit-scrollbar-button Buttons at both ends of the scroll bar's track allow fine-tuning the position of the small square by clicking on it.
::-webkit-scrollbar-track-piece The inner track, the middle part of the scroll bar (removed)
::-webkit- scrollbar-corner corner, that is, the intersection of two scroll bars
::-webkit-resizer The small corner at the intersection of two scroll bars that is used to adjust the size of the element by dragging Control
Concise version
The detailed code will not be posted here. You can find the specific style settings by viewing the source code in the demo. Let’s take a look at the style of the second scroll bar in demo2
/*定义滚动条高宽及背景 高宽分别对应横竖滚动条的尺寸*/ ::-webkit-scrollbar { width: 16px; height: 16px; background-color: #F5F5F5; } /*定义滚动条轨道 内阴影+圆角*/ ::-webkit-scrollbar-track { -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3); border-radius: 10px; background-color: #F5F5F5; } /*定义滑块 内阴影+圆角*/ ::-webkit-scrollbar-thumb { border-radius: 10px; -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,.3); background-color: #555; }
Detailed settings
To define scroll bars is to use pseudo elements and pseudo classes. So what are pseudo elements and pseudo classes?
Everyone should be familiar with pseudo-classes: link, :focus, :hover. In addition, many pseudo- class selectors have been added to CSS3, such as :nth-child, :last-child, :nth-last-of-type() etc.
You have seen the pseudo elements in CSS before: :first-line,:first-letter,:before,:after. Then in CSS3, the pseudo-element has been adjusted, and a ":" has been added to the previous one, which is now "::first-letter,::first-line,::before,::after", In addition, CSS3 also adds a "::selection". Two "::" and one ":" are mainly used to distinguish pseudo-classes and pseudo-elements in CSS3.
Webkit’s implementation of pseudo-classes and pseudo-elements is very strong. You can define the scroll bar as a page element, and then combine it with some advanced CSS3 attributes, such as gradients, rounded corners, RGBa, etc. Then if you need to use images in some places, you can also convert the images to Base64, otherwise you have to load multiple images every time and increase the number of requests.
Any object can be set: borders, shadows, background images, etc. The created scroll bar will still complete its interactive behavior according to the settings of the operating system itself. The following pseudo-classes can be applied to the above pseudo-elements. It's a bit complicated. For details on how to write it, you can see the first demo. There are also comments there.
:horizontal //horizontal伪类适用于任何水平方向上的滚动条 :vertical //vertical伪类适用于任何垂直方向的滚动条 :decrement //decrement伪类适用于按钮和轨道碎片。表示递减的按钮或轨道碎片,例如可以使区域向上或者向右移动的区域和按钮 :increment //increment伪类适用于按钮和轨道碎片。表示递增的按钮或轨道碎片,例如可以使区域向下或者向左移动的区域和按钮 :start //start伪类适用于按钮和轨道碎片。表示对象(按钮 轨道碎片)是否放在滑块的前面 :end //end伪类适用于按钮和轨道碎片。表示对象(按钮 轨道碎片)是否放在滑块的后面 :double-button //double-button伪类适用于按钮和轨道碎片。判断轨道结束的位置是否是一对按钮。也就是轨道碎片紧挨着一对在一起的按钮。 :single-button //single-button伪类适用于按钮和轨道碎片。判断轨道结束的位置是否是一个按钮。也就是轨道碎片紧挨着一个单独的按钮。 :no-button no-button伪类表示轨道结束的位置没有按钮。 :corner-present //corner-present伪类表示滚动条的角落是否存在。 :window-inactive //适用于所有滚动条,表示包含滚动条的区域,焦点不在该窗口的时候。 ::-webkit-scrollbar-track-piece:start { /*滚动条上半边或左半边*/ } ::-webkit-scrollbar-thumb:window-inactive { /*当焦点不在当前区域滑块的状态*/ } ::-webkit-scrollbar-button:horizontal:decrement:hover { /*当鼠标在水平滚动条下面的按钮上的状态*/ }
The above is the detailed content of CSS3 custom scroll bar style: detailed explanation of webkit-scrollbar. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In Vue.js, the placeholder attribute specifies the placeholder text of the input element, which is displayed when the user has not entered content, provides input tips or examples, and improves form accessibility. Its usage is to set the placeholder attribute on the input element and customize the appearance using CSS. Best practices include being relevant to the input, being short and clear, avoiding default text, and considering accessibility.

The span tag can add styles, attributes, or behaviors to text. It is used to: add styles, such as color and font size. Set attributes such as id, class, etc. Associated behaviors such as clicks, hovers, etc. Mark text for further processing or citation.

REM in CSS is a relative unit relative to the font size of the root element (html). It has the following characteristics: relative to the root element font size, not affected by the parent element. When the root element's font size changes, elements using REM will adjust accordingly. Can be used with any CSS property. Advantages of using REM include: Responsiveness: Keep text readable on different devices and screen sizes. Consistency: Make sure font sizes are consistent throughout your website. Scalability: Easily change the global font size by adjusting the root element font size.

There are five ways to introduce images in Vue: through URL, require function, static file, v-bind directive and CSS background image. Dynamic images can be handled in Vue's computed properties or listeners, and bundled tools can be used to optimize image loading. Make sure the path is correct otherwise a loading error will appear.

The SPAN tag is an inline HTML tag that is used to highlight text by applying attributes such as style, color, and font size. This includes emphasizing text, grouping text, adding hover effects, and dynamically updating content. It is used by placing <span> and </span> tags around the text you want to emphasize, and is manipulated via CSS styling or JavaScript. The benefits of SPAN tags include semantic clarity, styling flexibility, and ease of maintenance.

When using the prompt() method in JavaScript, you can achieve line breaks through the following three methods: 1. Insert the "\n" character at the position where you want to break the line; 2. Use the line break character in the prompt text; 3. Use CSS's "white" -space: pre" style forces line breaks.

Browser plug-ins are usually written in the following languages: Front-end languages: JavaScript, HTML, CSS Back-end languages: C++, Rust, WebAssembly Other languages: Python, Java

Nodes are entities in the JavaScript DOM that represent HTML elements. They represent a specific element in the page and can be used to access and manipulate that element. Common node types include element nodes, text nodes, comment nodes, and document nodes. Through DOM methods such as getElementById(), you can access nodes and operate on them, including modifying properties, adding/removing child nodes, inserting/replacing nodes, and cloning nodes. Node traversal helps navigate within the DOM structure. Nodes are useful for dynamically creating page content, event handling, animation, and data binding.
