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-smooth-scroll

v1.7.1

Published

That's a simple to use realization of a smooth scroll for your website. It's especially useful if you have some fixed elements and want to correct the scroll destination.

Downloads

22

Readme

Solves the same problem as https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-padding.

Smooth smooth scroll

That's a simple to use realization of a smooth scroll for your website. It's especially useful if you have some fixed elements and want to correct the scroll destination.

Advantages

  • Very lightweight - only 1.94 KB

  • Works in all alive browsers (requires 4.1 KB of polyfills)

  • You can change parameters of scroll after the first activation with a bit of js and easily add new links

  • It's built with scrollIntoView what gives a lot of advantages other smooth-scroll packages don't have:

    • It has good performance without weird jankiness and other bugs
    • You will not have any problems if you start clicking on the smooth-scroll trigger button as crazy
    • If the scroll event has already started it can be easily stoped before the end
    • There is a way to choose the position of the destination element. So, let's just say, if you have some element on your page, you can set either it's going to be at the top edge of the screen, at center, or the bottom edge after scroll
    • The same thing works with horizontal scrolling or even with both at the same time
  • And the last but one of the most essential features of the package is the ability to set an offset for scroll. It may be useful just for small adjustments, but the most obvious use case is to prevent content overlap with a fixed header

Disadvantages

  • There is no a proper way to know when the scroll event is finished
  • You cannot set up easing for scroll animation
  • Some people may complain about accessibility
  • URL doesn't change, but, on the other hand, you can make any element a link

How to

1. Install via npm

npm i smooth-smooth-scroll

2. Include smooth-smooth-scroll in your project

// Don't forget polyfill first if you need it (Required by Safari and IE)
import 'smooth-smooth-scroll/polyfill';
import { initSmoothScroll } from 'smooth-smooth-scroll';

Note: Polyfill file contains two things: smoothscroll-polyfill and closest polyfill.

So, it means you can use them further in the project without any old browser support worries.

3. Initialize

const disableSmoothScroll = initSmoothScroll(smoothScrollOptions);

The initSmoothScroll returns a function which removes the eventListener used by smooth-smooth-scroll.

4. Create a link

You just need to add a data attribute to any element. You can do it initially by hand in your Html file or later with javascript.

<div class="some-element" data-smooth-scroll-to="element-id"></div>
<div class="another-element" id="element-id"></div>
const someElement = document.querySelector('.some-element');

someElement.setAttribute('data-smooth-scroll-to', 'element-id');
// or
someElement.dataset.smoothScrollTo = 'element-id';

I recommend to use <button> tags for link elements.

5. Let's talk about options

By default they are like this:

{
  offsetTop: 0,
  offsetBottom: 0,
  offsetLeft: 0,
  offsetRight: 0,
  block: 'start',
  inline: 'nearest',
  eventListenerOptions: {},
}

You'll probably need only the offsetTop property. Just set your fixed header height and thats all. All the other offsets may be needed if you have some other elements from other directions.

Talking about block and inline, they represent literally the same thing as in scrollIntoView do. But don't afraid if you use only block: 'start' for all links on the page and suddenly realize you need block: 'center' for one element - there is a solution for this at the next step.

The eventListenerOptions object goes to the smooth-smooth-scroll eventListener options. There is only one, it is setten on document so you can not to afraid of performance issues if you have thousands of links.

6. Options for specific element

You can set them via data-attributes, such as:

data-smooth-scroll-block="center"
<!-- and -->
data-smooth-scroll-inline="end"

Look at the third step for more code examples.

Finally

At first, I didn't want to create an npm package for this, but the name smooth-smooth-scroll I come up with sounded so cool so I decided to do it. I hope you find it useful!