GithubHelp home page GithubHelp logo

scrivito / scroll-to-fragment Goto Github PK

View Code? Open in Web Editor NEW
5.0 4.0 1.0 1.15 MB

#️⃣Make single page apps scroll according to the current URL hash.

License: MIT License

TypeScript 100.00%
spa hash anchor scroll observe link fragment

scroll-to-fragment's Introduction

Scroll to fragment

Build status Package size NPM Version Types included Supported by Scrivito

Make single page apps scroll to the current URL hash.

When you follow a link that ends with a hash fragment (https://example.com#my-headline) on a traditional server-side-rendered (SSR) website, the browser automatically scrolls to the matching element. With JavaScript-driven single page apps (SPA), current browsers do not scroll by default.

This helper provides single page apps with the classic scrolling behavior. It updates the scroll position on load, and checks for updates on clicks and browser history changes. To keep the fragment in line with asynchronously updated content (for example in a ReactJS based app) it also adjusts the scroll position on DOM changes.

Installation

npm install scroll-to-fragment

Usage

In your app's initialization code, for example in index.js:

import { scrollToFragment } from "scroll-to-fragment";

scrollToFragment();

Options

You can customize the behavior with the following options:

import { scrollToFragment } from "scroll-to-fragment";
import { createBrowserHistory } from "history";

scrollToFragment({
  // customize the target of a given fragment ID (default is getElementById):
  getElement: (fragmentId) => document.getElementsByName(fragmentId)[0],

  // adjust the scroll position after history PUSH events:
  history: createBrowserHistory(),

  // customize scrolling behavior:
  scrollIntoView: (element) => element.scrollIntoView({ behavior: "smooth" }),
});

Manual trigger

If you are listening for any other events to trigger a scroll position update, simply call scrollToFragment() again. This will automatically stop the previous instance.

Tips

  • Triggering an update on popstate or hashchange may result in unwanted scrolling after browser back and forward navigation.
  • If the scroll position after navigating back is wrong, we recommend using a dedicated package like delayed-scroll-restoration-polyfill.

Development

npm run watch

scroll-to-fragment's People

Contributors

anne-schulz avatar apepper avatar dcsaszar avatar dependabot-preview[bot] avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

apepper

scroll-to-fragment's Issues

dependencies are wrong

hi, there is a misstake in the package.json

  • history should be in dependencies
  • @types/history should be in devDependencies

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.