npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

handy-scroll

v1.1.5

Published

Handy dependency-free floating scrollbar widget

Downloads

9,497

Readme

handy-scroll

Handy dependency-free floating scrollbar widget

Synopsis

handy-scroll is a dependency-free module which can be used to solve the problem of scrolling some lengthy containers horizontally when those containers don’t fit into the viewport. The widget is just a scrollbar which is attached at the bottom of the container’s visible area. It doesn’t get out of sight when the page is scrolled, thereby making horizontal scrolling of the container much handier.

[!TIP]

  • If you are rather looking for a jQuery plugin with the same functionality, please check out the sibling project floating-scroll instead.
  • Developing a Vue.js-based app? Consider using the vue-handy-scroll component.

Usage

The widget requires the CSS file handy-scroll.css to be included on the page (you may also import it in your CSS/LESS files). The module script file handy-scroll.min.js may be added on the web page either via a separate <script> element, or it may be loaded by any AMD/CommonJS compatible module loader.

:bulb: Tip: If you don’t care about supporting Internet Explorer, feel free to use the file handy-scroll.es6.min.js, which is de facto the same as handy-scroll.min.js but is written in ES6, and is a bit smaller.

The handy-scroll package is available on npm, so you may add it to your project as usual:

npm install handy-scroll

Details & API

The module exports a single object handyScroll which provides the following methods:

  • mount — initializes and “mounts” the widgets in the specified containers;
  • mounted — checks if the widget is already mounted in the given container;
  • update — updates the widget parameters and position;
  • destroy — destroys the widgets mounted in the specified containers and removes all related event handlers;
  • destroyDetached — destroys handy-scroll widget instances whose containers were removed from the document.

Mounting the widget

The only thing required to attach the widget to a static container (whose sizes will never change during the session) is a single call of the handyScroll.mount() method. The method expects a single argument, the target containers reference, which can be either an element, or a list of elements, or a selector.

// mount widget in the specified container element
handyScroll.mount(document.getElementById("spacious-container"));

// mount widgets in all the container elements in the collection
handyScroll.mount(document.getElementsByClassName("spacious-container"));
handyScroll.mount([myDOMElement1, myDOMElement2, myDOMElement3]);

// mount widgets in all the container elements matching the selector
handyScroll.mount(".examples > .spacious-container");

Auto-initialisation

There is another way to mount the handy-scroll widget without writing a single line of JavaScript code. Just add the attribute data-handy-scroll to the desired containers. As the DOM is ready the module will detect all such elements and will mount widgets automatically.

<div class="spacious-container" data-handy-scroll>
    <!-- Horizontally wide contents -->
</div>

Checking widget existence

You may check if the widget is already mounted in the given container by calling the handyScroll.mounted() method.

handyScroll.mount("#spacious-container");
console.log(handyScroll.mounted("#spacious-container")); // true

Updating scrollbar

If you mount the widget in a container whose size and/or content may dynamically change, you need a way to update the scrollbar each time the container’s sizes change. This can be done by invoking the method handyScroll.update() as in the example below.

handyScroll.mount(".spacious-container");
// ... some actions which change the total scroll width of the container ...
handyScroll.update(".spacious-container");

The method expects a single argument, the target containers reference, which can be either an element, or a list of elements, or a selector.

Destroying the widget

To unmount the widget and remove all related event handlers, use the method handyScroll.destroy() as follows:

handyScroll.destroy(".spacious-container");

The method expects a single argument, the target containers reference, which can be either an element, or a list of elements, or a selector.

Destroying detached widgets

If your app completely re-renders a large portion of DOM where handy-scroll widgets were mounted, actual container references are lost, and therefore you cannot unmount the widgets and perform related cleanup using the destroy method. In this case, you may just call the handyScroll.destroyDetached() method, and the module will find all “zombie” instances and will destroy them for you.

handyScroll.mount(".main-view .spacious-container");
// ... the app re-renders the main view ...
document.querySelector(".main-view").innerHTML = "...";
// destroy handy-scroll widgets whose containers are not in the document anymore
handyScroll.destroyDetached();

Special cases

If you want to attach the widget to a container living in a positioned box (e.g. a modal popup with position: fixed) then you need to apply two special indicating class names in the markup. The module detects these indicating class names (they are prefixed with handy-scroll-) and switches to a special functioning mode.

<div class="handy-scroll-viewport"><!-- (1) -->
    <div class="handy-scroll-body"><!-- (2) -->
        <div class="spacious-container">
            <!-- Horizontally wide contents -->
        </div>
    </div>
</div>

The .handy-scroll-viewport element (1) is a positioned block (with any type of positioning except static) which serves for correct positioning of the widget. Note that this element itself should not be scrollable. The .handy-scroll-body element (2) is a vertically scrollable block (with overflow: auto) which encloses the target container the widget is mounted in. After applying these special class names, you may initialise the widget as usual:

handyScroll.mount(".spacious-container");

The handy-scroll.css file provides some basic styles for elements with classes .handy-scroll-viewport and .handy-scroll-body. Feel free to adjust their styles in your stylesheets as needed.

“Unobtrusive” mode

You can make the widget more “unobtrusive” so that it will appear only when the mouse pointer hovers over the scrollable container. To do so just apply the class handy-scroll-hoverable to the desired scrollable container owning the widget.

Live demos

Check out some usage demos here.