在我的应用程序中,滚动条到来时它应该更改为自定义滚动条.在css中有任何方式来设置滚动条的样式.如果没有,请提供有关自定义滚动条的任何建议.
我已经看到了自定义滚动条的jquery插件,但是我需要更改自定义滚动条的每个位置.相反,有没有办法在一个地方进行更改会影响滚动条在我的应用程序中的所有位置.
解决方法:
我开发了custom scrollbar library named SimpleScrollbar.
它不依赖于任何其他库/框架,并且在gzip和minification之后它不到1KB.
它使用原生滚动,所以没有黑客,性能很棒.
您只需要在页面中包含库,并在要使其可滚动的任何div中使用ss-container属性.实例:
<link href="http://buzinas.github.io/simple-scrollbar/simple-scrollbar.css" rel="stylesheet"/>
<div style="height: 180px; width: 200px; display: inline-block;" ss-container>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
<section style="height: 180px; width: 300px; display: inline-block;" ss-container>
<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</p>
<p>But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?</p>
</section>
<!--[if IE 9]><script src="https://cdn.jsdelivr.net/classlist/2014.01.31/classList.min.js"></script><![endif]-->
<script src="http://buzinas.github.io/simple-scrollbar/simple-scrollbar.min.js"></script>
它适用于所有现代浏览器(Firefox,Chrome,Opera,Safari,Edge)以及IE10和IE11.您还可以通过包含classList polyfill在IE9中使用它.
也支持所有Android,iOS和Windows Phone浏览器.
如果要自动查找已有滚动条的所有元素,可以执行以下操作:
// After all the content in your page has loaded
document.addEventListener('DOMContentLoaded', function() {
// Get all elements in your page
var elements = document.querySelectorAll('*'), el;
for (var i = 0; i < elements.length, el = elements[i]; i++) {
// If the scrollheight is higher than the clientheight, it's because it has a scrollbar
if (el.scrollHeight > el.clientHeight) {
SimpleScrollbar.initEl(el); // turn the element into a SimpleScrollbar one
}
}
});