使用 CSS 自定义滚动条

使用 CSS 自定义滚动条

浏览器为网页上的滚动条设置默认样式。您可以自定义滚动条样式并使用 CSS 伪元素覆盖滚动条样式。::-webkit-scrollbar 伪元素有助于使用 CSS 自定义滚动条样式。

自定义滚动条对于将滚动条样式与网页设计相匹配非常有用。在此示例代码片段中,我们将使用 CSS 创建自定义滚动条。我们为滚动条轨道和滑块使用了特定的颜色,您可以根据您的网页样式在 CSS 中设置任何颜色。

CSS代码:

/* scrollbar */
::-webkit-scrollbar {
width: 20px;
}

/* scrollbar track */
::-webkit-scrollbar-track {
box-shadow: inset 0 0 5px grey;
border-radius: 10px;
}

/* scrollbar thumb */
::-webkit-scrollbar-thumb {
background: #015bea;
border-radius: 10px;
}
::-webkit-scrollbar-thumb:hover {
background: #0254d1;
}

HTML代码:

<div style="height: 250px;overflow-y: scroll;">
<h2>Create Custom Scrollbar with CSS</h2>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</div>

 

THE END
分享
二维码
海报
使用 CSS 自定义滚动条
浏览器为网页上的滚动条设置默认样式。您可以自定义滚动条样式并使用 CSS 伪元素覆盖滚动条样式。::-webkit-scrollbar 伪元素有助于使用 CSS 自定义滚动条样式。
<<上一篇
下一篇>>