Git Product home page Git Product logo

minibar's Introduction

MiniBar

Maintenance Code Climate maintainability npm license GitHub release npm GitHub issues GitHub issues

A lightweight, dependency-free scrollbar library written in vanilla javascript.

  • Fully customisable via CSS
  • Native scrolling behaviour preserved
  • Vertical and horizontal scroll support
  • Textarea support
  • Horizontal scrolling with mousewheel

Demo | Changelog

Note

MiniBar is currently in a pre-release state so is not yet suitable for production so use with care. The API will be in constant flux until v1.0.0 is released so check back for any changes.

Horizontal scrolling with mousewheel and textarea support are experimental and may not work in certain browsers.

MiniBar utilizes the MutationObserver API to automatically detect changes in content so the dimensions can be updated. It will only use the API if your browser supports it. If it doesn't then you must call the update() method when adding / removing / updating the containers content otherwise the scroll bar position and size will be incorrect.


Install

These methods install original MiniBar from Moebius1

bower

bower install minibarjs

npm

npm install minibarjs

Browser

Grab the files from the CDN and include them in your project:

<link href="https://unpkg.com/minibarjs@latest/dist/minibar.min.css" rel="stylesheet" type="text/css">
<script src="https://unpkg.com/minibarjs@latest/dist/minibar.min.js" type="text/javascript"></script>

You can replace latest with the required release number if needed.


Initialisation

You can instantiate MiniBar by passing a reference to your content as the first parameter of the constructor as either a DOM node or a CSS3 selector string:

new MiniBar(document.getElementById('myContent'));

// or

new MiniBar('#myContent');

MiniBar also accepts an object as a second parameter of the constructor for user defined options:

new MiniBar('#myContent', {
    barType: "default",
    minBarSize: 10,
    hideBars: false,  /* v0.4.0 and above */
    alwaysShowBars: false,
    horizontalMouseScroll: false,

    scrollX: true,
    scrollY: true,

    navButtons: false,
    scrollAmount: 10,

    mutationObserver: {
        attributes: false,
        childList: true,
        subtree: true
    },

     /* v0.4.0 and above */
    onInit: function() {
    /* do something on init */
    },

     /* v0.4.0 and above */
    onUpdate: function() {
    /* do something on update */
    },

     /* v0.4.0 and above */
    onScroll: function() {
    /* do something on init */
    },

    classes: {
        container: "mb-container",
        content: "mb-content",
        track: "mb-track",
        bar: "mb-bar",
        visible: "mb-visible",
        progress: "mb-progress",
        hover: "mb-hover",
        scrolling: "mb-scrolling",
        textarea: "mb-textarea",
        wrapper: "mb-wrapper",
        nav: "mb-nav",
        btn: "mb-button",
        btns: "mb-buttons",
        increase: "mb-increase",
        decrease: "mb-decrease",
        item: "mb-item", /* v0.4.0 and above */
        itemVisible: "mb-item-visible", /* v0.4.0 and above */
        itemPartial: "mb-item-partial", /* v0.4.0 and above */
        itemHidden: "mb-item-hidden" /* v0.4.0 and above */
    }
});

You can also define global options with the MiniBarOptions object:

MiniBarOptions = {
    barType: "default",
    minBarSize: 10,
    hideBars: false,
    ...
};

Options

hideBars (v0.4.0 and above)

Type: Boolean

Default: false

When set to true the scrollbars will be hidden.

minBarSize

Type: Integer

Default: 50

Sets the minimum size of the scrollbars. This can prevent the scollbar becoming to small when you have a ton of content.

alwaysShowBars

Type: Boolean

Default: false

By default the scrollbars aren't visible until hovering over the content. Set this to true to keep the scrollbars visible at all times.

barType

Type: String

Default: default

Set to progress to display the scrollbars as progress bars.

observableItems (v0.4.0 and above)

Type: Mixed

Default: false

Allows MiniBar to observe descendents and determine whether they're fully or partially visible within the scrolling container or completely out of view.

To use you must pass a CSS3 selector string of the scrolling containers descendents that you want to monitor. When monitored, each descendant will have a className added depending on it's visibility:

  • .mb-item-visible - item boundaries are completely within the scrolling container.
  • .mb-item-partial - item is visible, but it's boundaries are not completely within the scrolling container.
  • .mb-item-hidden - item is not visible.

NOTE: Your browser must support the IntersectionObserver API for this to work. The only code run in it's callback is the className changes, so latency is kept to a minimum.

horizontalMouseScroll

Type: Boolean

Default: false

Allow horizontal scrolling with the mousewheel.

navButtons

Type: Object

Default: false

Enable scrollbars with navigation buttons.

scrollAmount

Type: Integer

Default: 10

Increase or decrease the amount scrolled when clicking a nav button.

wheelScrollAmount

Type: Integer

Default: 100

Increase or decrease the amount scrolled when rolling mouse wheel.


Public Methods

destroy()

Destroy the current instance. Removes all nodes and event listeners attached to the DOM by MiniBar.

init()

Initialise the instance after destroying.

update()

Recalculate scollbar sizes / positions. This method is called automatically when the content and window are resized or if content is added / removed. You can call this method manually if you add or remove content.

scrollTo(position, axis) (v0.4.0 and above)

/**
 * @param  {Number|String} 	position   | Position to scroll to
 * @param  {String} 	    axis       | Scroll axis
 */

Scroll the content to the defined position. This can either be an integer to represent the position in pixels to scroll to or "start" / "end" to scroll to the start / end position.

scrollBy(amount, axis, duration, easing)

/**
 * @param  {Number} 	amount   Number of pixels to scroll
 * @param  {String} 	axis     Scroll axis
 * @param  {Number} 	duration Duration of scroll animation in ms
 * @param  {Function} 	easing   Easing function
 */

Scroll the content by a certain amount. You can define which axis to scroll with the axis parameter (defaults to "y").

By default this method animates the scrolling. To control the duration of the animation simply set the number of ms with the duration parameter. Setting to 0 will disable animation.

The default easing used is easeOutQuad, but you can pass your own easing function with the easing parameter.

scrollToTop() (v0.5.0 and above)

Scroll the container to the top

scrollToBottom() (v0.5.0 and above)

Scroll the container to the bottom


To Do

  • Add touch / mobile support
  • Implement Mutation Observers to detect DOM changes? Added in v0.3.0

Copyright © 2017 Karl Saunders | MIT license

minibar's People

Contributors

mobius1 avatar zalemcitizen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

minibar's Issues

autorwapping on / off?

-Does it allow to disable auto wrapping and use your existent container?=
is this what the class option is for? or that is just changing the class names?

Initializing MiniBar

Hi there! I'm kind of confused on how you're initializing it. How exactly is it supposed to look in inside the tag?

I've tried both with <script> tags and without and it doesn't seem to work.

Is there a complete example that I haven't seen yet? Some of the ones I found I can't even poke through the inspector cause it's hidden.

Thank you.

Possible to scroll by clicking on the track?

I wonder if it is possible (with the current release?) or possibly as a future feature to scroll simply by clicking on the track. Just as it works on a system default scrollbar...

In the progress-bar demo it is possible to scroll by clicking the track though.

minibar

getElementById

Hi,
In the example, there is
new MiniBar(document.getElementById('#myContent'));

it's good but without #

Horizontal scroll is jumping

Hello, I tried to implement your lib in my project but I observe the same issue as in your horizontal scroll demo on this page : https://mobius1.github.io/MiniBar/

Device : desktop win10
Chrome 71 : mouse wheel jump from start to end, touch is OK
Firefox 64 : works as expected (wheel and touch)

Device : desktop macOS
Chrome 71 : seems to work as expected (can't be sure, my Chrome is buggy on macOS VM)

Device : android 8
Chrome 70 : works as expected (touch)

Any chance you bring a fix ?

Is the update() method not working or I don't know how to use it?

Hi,
I wanted to use MiniBar on my website to have scrollbar visible on mobile devices all the time but I hit the wall while trying to use the update() method. I tried different methods but I couldn't make the update() work. :/

https://flyingatom.com/testowanie/kantor-online/ - I'm using it here at 'Znajdź najbliższy kantor lub bitomat' section and I wanted to use update() because scrollbar is disappearing after selecting Kantor / Bitomat. :<

still supported?

Hi,

-Is this still supported? I noticed there is a no as of 2018 but I see you still merge tickets?

Thanks!

Styling

Hi! Considering using this for a project, but wondering how I can custom style it. I have some specific needs and especially need to know if I can style it with gradients and if I can change the styles in JavaScript. Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.