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

@teamthunderfoot/anchor-to

v0.0.10

Published

Anchor To package

Downloads

22

Readme

Anchor-to

The Anchor-to package provides functionality for smooth scrolling to anchor elements on a web page, including scrolling to a target element specified in the URL.

Here's an example

Installation

npm install @teamthunderfoot/anchor-to

Usage

Option 1: Scroll with Buttons

import AnchorTo from "@teamthunderfoot/anchor-to"

class Index {
  constructor() {
    this.init();
  }
  init() {
    new AnchorTo({
      element: document.querySelector(".js--scroll-to"),
      checkUrl: false,
      anchorTo: "tf-data-target",
      offsetTopAttribute: "tf-data-distance",
      offsetTop: false,
      offsetTopURL: false,
    });

    document.querySelectorAll(".js--scroll-to").forEach((element) => {
        new AnchorTo({
            element: element,
            checkUrl: true, // or false
            anchorTo: "tf-data-target",
            offsetTopAttribute: "tf-data-distance",
            offsetTop: 50,
            offsetTopURL: 100,
        })
    })
  }
}

export default Index;
new Index();

In your HTML file, include elements with the class js--anchor-to and a data-target attribute specifying the ID of the anchor element. Optionally, you can add the tf-data-distance attribute to set the offset-top from there.

<a href="#" class="js--anchor-to" data-target="section1" tf-data-distance="50">Scroll to Section 1</a>

If the tf-data-distance attribute is not provided in the HTML element, the value of the offset-top will be taken from the offsetTop variable of the JS class, which can be dynamic and adjusted as needed.

  new AnchorTo({
    element: document.querySelector(".js--scroll-to"),
    checkUrl: false,
    anchorTo: "tf-data-target",
    offsetTopAttribute: "tf-data-distance",
    offsetTop: 15,
    offsetTopURL: false,
  });
<a href="#" class="js--anchor-to" data-target="section1">Scroll to Section 1</a>

In this example of the HTML, since it doesn't have the tf-data-distance attribute, it will take the value of the offset top as 15px, which is the value specified in the JS variable.

Option 2: Scroll with URL

If you want the package to scroll to a specific anchor element based on the URL, follow this approach:

import AnchorTo from "./AnchorTo";

class Index {
  constructor() {
    this.init();
  }
  init() {
    new ScrollTo({
      element: false,
      checkUrl: true,
      anchorTo: false,
      offsetTopAttribute: "tf-data-distance",
      offsetTop: 15,
      offsetTopURL: 50,
    });
  }
}

export default Index;
new Index();

If the URL contains a hash value (e.g., https://example.com?section2), the package will automatically scroll to the element with the matching ID specified in the URL.

Option 3: Combined Usage

You can also combine both options to scroll with buttons and handle URL-based scrolling simultaneously:

import AnchorTo from "./AnchorTo";

class Index {
  constructor() {
    this.init();
  }
  init() {
    new AnchorTo({
      element: document.querySelector(".js--scroll-to"),
      checkUrl: true,
      anchorTo: "tf-data-target",
      offsetTopAttribute: "tf-data-distance",
      offsetTop: 15,
      offsetTopURL: 100,
    });
  }
}

export default Index;
new Index();

Options

When initializing the AnchorTo class, you can provide several options to customize its behavior. Here's an explanation of each option:

element: This option specifies the class name of the buttons or elements that trigger the scroll action.

checkUrl: This option determines whether the package should check the URL for a hash value and scroll to the corresponding element. If set to true, it will enable URL-based scrolling. If set to false, URL-based scrolling will be disabled.

anchorTo: This option specifies the attribute used to identify the target element to scroll to. It must be an id of the element.

offsetTopAttribute: This option specifies the attribute used to determine the offset distance for scrolling. If the attribute is present in the HTML element, its value will be used as the offsetTop. If the attribute is not present, the offset top value will be taken from the offsetTop variable of the JS class.

offsetTop: This option allows you to specify an additional vertical scroll offset in pixels. It adjusts the final scroll position, but its value will be taken into account only if the tf-data-distance attribute is not present in the HTML element. If the tf-data-distance attribute is present, the value of the offsetTop option will be ignored and the offset will be determined by the attribute's value.

offsetTopURL: This option defines the vertical scroll offset applied when scrolling to an anchor element specified in the URL. It is useful when you want to add an additional offset.