jQuery OverlayScrollbars插件提供了一种简单的方法,可以在任何可滚动元素上创建可自定义、可主题化的覆盖滚动条,同时保留本地滚动功能。
香草JS版本的DOC在这里可用。
1.在网页的首页部分包含您选择的主样式表和主题CSS。
<!-- Main --> <link href="css/OverlayScrollbars.css" rel="stylesheet"> <!-- round dark --> <link href="css/os-theme-round-dark.css" rel="stylesheet"> <!-- round light --> <link href="css/os-theme-round-light.css" rel="stylesheet"> <!-- block dark --> <link href="css/os-theme-block-dark.css" rel="stylesheet"> <!-- block light --> <link href="css/os-theme-block-light.css" rel="stylesheet"> <!-- minimal dark --> <link href="css/os-theme-minimal-dark.css" rel="stylesheet"> <!-- minimal light --> <link href="css/os-theme-minimal-light.css" rel="stylesheet"> <!-- thick dark --> <link href="css/os-theme-thick-dark.css" rel="stylesheet"> <!-- thick light --> <link href="css/os-theme-thick-light.css" rel="stylesheet"> <!-- thin dark --> <link href="css/os-theme-thin-dark.css" rel="stylesheet"> <!-- thin light --> <link href="css/os-theme-thin-light.css" rel="stylesheet">
2.包括jQuery JavaScript库和jquery.overlay滚动条.js页面上的脚本。
<script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ" crossorigin="anonymous"></script> <script src="js/jquery.overlayScrollbars.js"></script>
3.调用目标可滚动元素上的函数,并指定要使用的主题Class。可用主题:
$('body').overlayScrollbars({
className: "os-theme-dark"
});
4.更多用于自定义滚动条的配置选项。
$('body').overlayScrollbars({
// none || both || horizontal || vertical || n || b || h || v
resize : 'none',
// true || false
sizeAutoCapable : true,
// true || false
clipAlways : true,
// true || false
normalizeRTL : true,
// true || false
paddingAbsolute : false,
// true || false || null
autoUpdate : null,
// number
autoUpdateInterval : 33,
// Control on which elements / selectors OverlayScrollbars shall update automatically after the emit of a load event.
// You can set it to null to disable this auto updating entierly or add your own selectors to update only on special img elements or on for example loaded iframes.
updateOnLoad: ['img'],
// These options are only respected if the native scrollbars are overlaid.
nativeScrollbarsOverlaid : {
showNativeScrollbars : false, //true || false
initialize : true //true || false
},
// Defines how the overflow should be handled for each axis
overflowBehavior : {
// visible-hidden || visible-scroll || hidden || scroll || v-h || v-s || h || s
x : 'scroll',
// visible-hidden || visible-scroll || hidden || scroll || v-h || v-s || h || s
y : 'scroll'
},
// Defines the behavior of the custom scrollbars.
scrollbars : {
visibility : 'auto', //visible || hidden || auto || v || h || a
autoHide : 'never', //never || scroll || leave || n || s || l
autoHideDelay : 800, //number
dragScrolling : true, //true || false
clickScrolling : false, //true || false
touchSupport : true, //true || false
snapHandle: true //true || false
},
// Defines special behavior of textarea elements.
textarea : {
dynWidth : false, //true || false
dynHeight : false, //true || false
inheritedAttrs : inheritedAttrsTemplate //string || array || null
}
});
5.回调函数。
$('body').overlayScrollbars({
callbacks : {
onInitialized : null, //null || function
onInitializationWithdrawn : null, //null || function
onDestroyed : null, //null || function
onScrollStart : null, //null || function
onScroll : null, //null || function
onScrollStop: null, //null || function
onOverflowChanged : null, //null || function
onOverflowAmountChanged : null, //null || function
onDirectionChanged : null, //null || function
onContentSizeChanged : null, //null || function
onHostSizeChanged : null, //null || function
onUpdated : null //null || function
}
});
6.API方法。
var instance = $('body').overlayScrollbars({
// options here
});
// adds new options
instance.options(optionName, optionValue)
// updates scrollbars
instance.update([force])
// puts the instance to sleep
instance.sleep()
// returns the current scroll information.
instance.scroll()
// sets the scroll position.
instance.scroll(coordinates [, duration] [, easing] [, complete])
// stops the current scroll-animation.
instance.scrollStop()
// returns all relevant elements.
instance.getElements()
// returns a object which describes the current state of this instance.
instance.getState()
// removes scrollbars from DOM
instance.destroy()
// checks whether a passed object is a non-destroyed OverlayScrollbars instance
instance.valid()v2.1.0版本(2023-02-06)
v1.13.0 (2020-08-03)
v1.11.0 (2020-04-06)
v1.11.0 (2020-03-01)
v1.10.3 (2020-02-03)
v1.9.0 (2019-07-27)
v1.8.0 (2019-07-09)
v1.7.3 (2019-06-24)
v1.7.0(2019年4月19日)
2019-01-17