noeldelgado / gemini-scrollbar

:first_quarter_moon: Custom overlay-scrollbars with native scrolling mechanism for web applications
https://noeldelgado.github.io/gemini-scrollbar/
MIT License
429 stars 63 forks source link
custom-scrollbar custom-scrollbars gemini-scrollbar javascript js-library native-scrollbars overlay-scrollbars scrollbars

gemini-scrollbar

npm-image bower-image license-image Known Vulnerabilities Dependencies Total alerts Language grade: JavaScript

Custom overlay-scrollbars with native scrolling mechanism for web applications (if needed).

There is a React wrapper too — react-gemini-scrollbar.

Problem Description

Nowadays, some OS’s provides “overlay-scrollbars” natively. Those scrollbars look nice and work well (mostly mobile browsers and OSX opt-in). The problem came when you have to customize the remaining ‘ugly’ scrollbars out there. e.g: “having a sidebar with a dark background + native-non-floating-scrollbars” ...hum, ugly. Even when this problem can be merely visual, for me is a way of enhancing the user experience.

Constraints
Solution Proposal

Check the scrollbar size. If the scrollbar size is zero (which means the scrollbars are already “over the content”) then we do nothing. Otherwise we simply “hide” native scrollbar and show custom in its place.

Demo

https://noeldelgado.github.io/gemini-scrollbar/

Dependencies

None

Installation

NPM

npm i gemini-scrollbar --save

Bower

bower install gemini-scrollbar --save

Usage

JS

var GeminiScrollbar = require('gemini-scrollbar')

var myScrollbar = new GeminiScrollbar({
    element: document.querySelector('.my-scrollbar')
}).create();

LESS

@import (inline) "<path-to-gemini-scrollbar>/gemini-scrollbar.css";

CSS

@import url(<path-to-gemini-scrollbar>/gemini-scrollbar.css);

Or, you can add the relevant files in your document.

<link href="https://github.com/noeldelgado/gemini-scrollbar/blob/master/<path-to-gemini-scrollbar>/gemini-scrollbar.css" rel="stylesheet">
<script src="https://github.com/noeldelgado/gemini-scrollbar/raw/master/<path-to-gemini-scrollbar>/index.js"></script>

Options

name type default description
element * HTMLElement null The element to apply scrollbars
autoshow Boolean false Show scrollbars upon hovering
createElements Boolean true Create and append the require HTMLElements at runtime.
forceGemini Boolean false Force Gemini scrollbars even if native overlay-scrollbars are available. Useful for development.
onResize Function null Hook by which clients can be notified of resize events.
minThumbSize Number (px) 20 Sets the minimum size of the thumbs.

* required

Basic Methods

name description
create Bind the events, create the required elements and display the scrollbars.
update Recalculate the viewbox and scrollbar dimensions.
destroy Unbind the events and remove the custom scrollbar elements.

Other Mehods

name description
getViewElement Returns the scrollable element

Customization

You can change the styles of the scrollbars using CSS. e.g:

/* override gemini-scrollbar default styles */

/* vertical scrollbar track */
.gm-scrollbar.-vertical {
  background-color: #f0f0f0
}

/* horizontal scrollbar track */
.gm-scrollbar.-horizontal {
  background-color: transparent;
}

/* scrollbar thumb */
.gm-scrollbar .thumb {
  background-color: rebeccapurple;
}
.gm-scrollbar .thumb:hover {
  background-color: fuchsia;
}

Notes

This way you can be sure the library will not touch/change your nodes structure. You can read more about the reason of this option on this commit.

Related

License

MIT © Noel Delgado