This package has been deprecated

Author message:

No longer maintained, please switch to react-scroll-percentage

@charlietango/use-scroll-percentage
TypeScript icon, indicating that this package has built-in type declarations

1.5.1 • Public • Published

useScrollPercentage

Monitor the the amount an element is scrolled inside the viewport. It's combined with an IntersectionObserver, so it only updates as long as the element is inside the viewport

Checkout the Storybook demo.

Installation

yarn add @charlietango/use-scroll-percentage

API

const [ref, percentage] = useScrollPercentage(options, horizontal)

Options

Options should match the Intersection Observer options.

Name Type Default Required Description
root Element window false The Element that is used as the viewport for checking visibility of the target. Defaults to the browser viewport (window) if not specified or if null.
rootMargin string '0px' false Margin around the root. Can have values similar to the CSS margin property, e.g. "10px 20px 30px 40px" (top, right, bottom, left).
threshold number | number[] 0 false Number between 0 and 1 indicating the percentage that should be visible before triggering. Can also be an array of numbers, to create multiple trigger points.

Example

import React from 'react'
import useScrollPercentage from '@charlietango/use-scroll-percentage'

const Component = () => {
  const [ref, percentage] = useScrollPercentage()
  return <div ref={ref}>Scroll percentage: {percentage}</div>
}

export default Component

Readme

Keywords

Package Sidebar

Install

npm i @charlietango/use-scroll-percentage

Weekly Downloads

4

Version

1.5.1

License

MIT

Unpacked Size

10.4 kB

Total Files

8

Last publish

Collaborators

  • frederik_bosch
  • thebuilder