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

@terrahq/anchor-to

v0.0.1

Published

The `AnchorTo` class enables smooth scrolling between elements on a webpage. It provides options for configuring scroll speed, offset, URL updates, debugging, and custom event emissions. Ideal for Single Page Applications (SPAs) and sites requiring a smoo

Readme

AnchorTo Class

The AnchorTo class enables smooth scrolling between elements on a webpage. It provides options for configuring scroll speed, offset, URL updates, debugging, and custom event emissions. Ideal for Single Page Applications (SPAs) and sites requiring a smooth scrolling experience.

Installation

npm install terrahq/anchor-to

Usage Example

import AnchorTo from 'terrahq/anchor-to';

const anchor = new AnchorTo({
    trigger: document.querySelector('.my-button'),      // The button that triggers scrolling
    destination: document.querySelector('#section4'),   // The target element to scroll to
    offset: 50,                                         // Offset in pixels or a function for dynamic offset
    url: 'hash',                                        // URL behavior: 'hash', 'query', or 'none'
    speed: 500,                                         // Scroll speed in milliseconds
    emitEvents: true,                                   // Emits start and end events for custom listeners
    popstate: true,                                     // Supports browser back/forward navigation
    debug: true,                                        // Enables console debug output
    onComplete: () => console.log('Scroll complete!')   // Callback executed after scroll ends
});

Options

  • trigger {HTMLElement} - The element that triggers the scroll when clicked.

  • destination {HTMLElement} - The target element to scroll to.

  • offset {number | function} - Distance from the target. Can be a static number or a function that receives destination and trigger as parameters.

  • url {string} - Determines URL behavior:

    • : Adds the destination's id as a hash in the URL.
    • 'query': Adds the destination's id as a query parameter.
    • 'none': No URL update.
  • speed {number} - Duration of scroll animation in milliseconds (default is 1500).

  • emitEvents {boolean} - Emits custom events anchorToStart and anchorToEnd during scroll.

  • popstate {boolean} - Enables scroll behavior for back/forward navigation.

  • debug {boolean} - If true, logs configuration and instance properties to the console.

  • onComplete {function} - A callback function executed when the scroll animation completes.

Methods

scrollTo(element)

Triggers a scroll animation to the specified element.

// Manually trigger the scroll to the destination element
anchor.scrollTo(anchor.DOM.destination);

destroy

Removes event listeners on the trigger and window, cleaning up the instance.

// Removes the click and popstate event listeners
anchor.destroy();

emitEvents

With emitEvents enabled, AnchorTo dispatches custom events:

  • AnchorToStart - Emitted when the scroll animation begins.
  • AnchorToEnd - Emitted when the scroll animation completes.
  • These events allow additional customization through external listeners.
const anchor = new AnchorTo({
    trigger: document.querySelector('.my-button'),
    destination: document.querySelector('#section4'),
    emitEvents: true
});

anchor.DOM.trigger.addEventListener('AnchorToStart', (event) => {
    console.log('Scrolling has started!', event.detail.element);
});

anchor.DOM.trigger.addEventListener('AnchorToEnd', (event) => {
    console.log('Scrolling has ended!', event.detail.element);
});