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

smooth-scroll-handler

v2.0.3

Published

Silky smooth scroll

Downloads

48

Readme

smooth-scroll-handler

Smooth mouse scroll.
Package based on Manuel Otto's answer to this post: https://stackoverflow.com/a/47206289/9006591

It fixes a bug with the original code, where the page scrolled back to its original position if you used the arrow keys to scroll, or if you dragged the scrollbar up or down.
It also provides useful methods, events, properties and options.

⚠️ Warning: Scroll-hijacking can have a negative impact on the user experience and is therefore not suitable for all projects.

Usage

import ScrollHandler from 'smooth-scroll-handler';

const scrollHandler = new ScrollHandler();

function loop() {
    requestAnimationFrame(loop);
    
    scrollHandler.update();
}

That's all it takes, but you can do much more!

Options

Options should be provided as one object.

speed

How fast the page goes as the user scrolls.
Default is 1.

smooth

A higher value means a smoother scroll.
Default is 10, minimum value is 1 (meaning no smoothing).

import ScrollHandler from 'smooth-scroll-handler';

const scrollHandler = new ScrollHandler({ speed: 1, smooth: 10 });

Methods

disable( allowScroll = false, noScrollApproach = 'fixed' )

Deactivate the instance.
This allowScroll option is false by default, the page scroll will be blocked. If this option is set to true, the native scroll (without smoothing) will be set back to normal.
There's more than one way to block the scroll, so the noScrollApproach option lets you decide which approach to use. By default the fixed approach is used, meaning the documentElement position will be set to fixed. But you can also try the overflow approach, meaning an overflow: hidden will be applied on the documentElement.

enable()

Reactivate the instance, meaning the scroll will be reactivated and smoothed again.

scrollTo(destination, options)

let options = {
    duration: 1,
    // Duration of the animation in milliseconds.
    // Default is .5

    ease: 'power2.out',
    // GSAP easing function (https://greensock.com/docs/v3/Eases)
    // Default is 'power2.inOut'

    allowScrollInterruption: false,
    // Defines if the scrollTo animation can be interrupted by a manual scroll
    // in the opposite direction.
    // Default is false

    offset: 200
    // Offset of the scrollTo stop position, from the target element.
    // A positive value will stop the scrollTo position above the target element.
    // Can be usefull when the page contains a sticky header, for instance.
    // Default is 0
}
scrollHandler.scrollTo('#somewhere', options);
// or
scrollHandler.scrollTo(200, options);

handleNoScrollElements()

If elements with the data-no-smooth-scroll attribute are added asynchronously to the DOM, this method can be used to manually parse the DOM for those elements.

Events

scrollHandler.onStart.add(() => {
    console.log('scroll started');
});

scrollHandler.onStop.add(() => {
    console.log('scroll stopped');
});

scrollHandler.onDirectionChange.add(direction => {
    console.log('scroll direction changed to: ', direction);
});

Properties

.active = (boolean)

If set to false, the instance will stop updating, meaning the mouse scroll events will no longer be smoothed, but you'll still be able to use the keyboard or the scrollbar.
Default is true.

.direction (read-only)

Returns 1 if the page scrolled last towards the bottom, and -1 if it scrolled towards the top.

.lerpedDelta (read-only)

Returns the difference between the previous and the next scroll position. This is useful if you need to make an animation adapt to the scrolling speed.

Disabling the smooth scroll

Use the data-no-smooth-scroll attribute on elements that needs to use an inner native scroll, like textareas, or drop-down menus.

<textarea data-no-smooth-scroll></textarea>

Todo

  • Returning completion