All Projects → n8tb1t → Use Scroll Position

n8tb1t / Use Scroll Position

Licence: mit
Use scroll position ReactJS hook done right

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Use Scroll Position

use-smooth-scroll
React hook which gives a smooth scrolling function.
Stars: ✭ 41 (-90.1%)
Mutual labels:  hooks, scroll
React Use Gesture
👇Bread n butter utility for component-tied mouse/touch gestures in React and Vanilla Javascript.
Stars: ✭ 5,704 (+1277.78%)
Mutual labels:  hooks, scroll
use-gesture
👇Bread n butter utility for component-tied mouse/touch gestures in React and Vanilla Javascript.
Stars: ✭ 6,624 (+1500%)
Mutual labels:  hooks, scroll
React Native Swiper Flatlist
👆 Swiper component implemented with FlatList using Hooks & Typescript + strict automation tests with Detox
Stars: ✭ 217 (-47.58%)
Mutual labels:  hooks, scroll
use-scroll-direction
A simple, performant, and cross-browser hook for detecting scroll direction in your next react app.
Stars: ✭ 24 (-94.2%)
Mutual labels:  hooks, scroll
React Query
⚛️ Hooks for fetching, caching and updating asynchronous data in React
Stars: ✭ 24,427 (+5800.24%)
Mutual labels:  hooks
Local Storage
React hook which syncs localStorage[key] with the comp.
Stars: ✭ 402 (-2.9%)
Mutual labels:  hooks
Hc Sticky
JavaScript library that makes any element on your page visible while you scroll.
Stars: ✭ 375 (-9.42%)
Mutual labels:  scroll
React Uploady
Modern file uploading - components & hooks for React
Stars: ✭ 372 (-10.14%)
Mutual labels:  hooks
Release It
🚀 Automate versioning and package publishing
Stars: ✭ 4,773 (+1052.9%)
Mutual labels:  hooks
Lethargy
Distinguish between scroll events initiated by the user, and those by inertial scrolling
Stars: ✭ 407 (-1.69%)
Mutual labels:  scroll
Hamburger React
Animated hamburger menu icons for React (1.5 KB) 🍔
Stars: ✭ 391 (-5.56%)
Mutual labels:  hooks
React Hook Form
📋 React Hooks for form state management and validation (Web + React Native)
Stars: ✭ 24,831 (+5897.83%)
Mutual labels:  hooks
React Admin Template
Simple React admin template - Hooks, Redux, Bootstrap ✅ 🤘
Stars: ✭ 404 (-2.42%)
Mutual labels:  hooks
Jxpagelistview
高仿闲鱼、转转、京东、中央天气预报等主流APP列表底部分页滚动视图
Stars: ✭ 377 (-8.94%)
Mutual labels:  scroll
React Native Version
🔢 Version your React Native or Expo app in a `npm version` fashion.
Stars: ✭ 408 (-1.45%)
Mutual labels:  hooks
Hooks
FullStack | Zero Api | Using "React Hooks" to develop the back-end | Vite
Stars: ✭ 367 (-11.35%)
Mutual labels:  hooks
React Native Parallax Scroll
Parallax scroll view for react-native
Stars: ✭ 385 (-7%)
Mutual labels:  scroll
Binee
Binee: binary emulation environment
Stars: ✭ 408 (-1.45%)
Mutual labels:  hooks
Slidablelayout
SlidableLayout is devoted to build a stable, easy-to-use and smooth sliding layout.
Stars: ✭ 385 (-7%)
Mutual labels:  scroll

use-scroll-position

Node version Node version Node version

Screenshot

use-scroll-position is a React hook that returns the browser viewport X and Y scroll position. It is highly optimized and using the special technics to avoid unnecessary rerenders!

It uses the default react hooks rendering lifecycle, which allows you to fully control its behavior and prevent unnecessary renders.

Important Update Notice

Starting from v1.0.44 the project has moved to typescript.

Also, some bugs have been fixed, and thanks to our contributors we added an option to track the scroll position of specified element inside some custom container.

Just pass the element reference and the boundingElement - (parent container) reference and track their corresponding position! (boundingElement should be scrollable with overflow hidden or whatever)

Demo

Edit use-scroll-position

Install

yarn add @n8tb1t/use-scroll-position

Usage

useScrollPosition(effect,deps, element, useWindow, wait)
Arguments Description
effect Effect callback.
deps For effects to fire on selected dependencies change.
element Get scroll position for a specified element by reference.
useWindow Use window.scroll instead of document.body.getBoundingClientRect() to detect scroll position.
wait The timeout in ms. Good for performance.
boundingElement Only works with useWindow set to false, Just pass the element reference and the boundingElement - (parent container) reference and track their corresponding position, boundingElement should be scrollable with overflow hidden or whatever

The useScrollPosition returns prevPos and currPos.

Examples

Log current scroll position

import { useScrollPosition } from '@n8tb1t/use-scroll-position'

useScrollPosition(({ prevPos, currPos }) => {
  console.log(currPos.x)
  console.log(currPos.y)
})

Change state based on scroll position - Inline CSS

import React, { useState } from 'react'
import { useScrollPosition } from '@n8tb1t/use-scroll-position'

const [headerStyle, setHeaderStyle] = useState({
  transition: 'all 200ms ease-in'
})

useScrollPosition(
  ({ prevPos, currPos }) => {
    const isVisible = currPos.y > prevPos.y

    const shouldBeStyle = {
      visibility: isVisible ? 'visible' : 'hidden',
      transition: `all 200ms ${isVisible ? 'ease-in' : 'ease-out'}`,
      transform: isVisible ? 'none' : 'translate(0, -100%)'
    }

    if (JSON.stringify(shouldBeStyle) === JSON.stringify(headerStyle)) return

    setHeaderStyle(shouldBeStyle)
  },
  [headerStyle]
)

const Header = <header style={{ ...headerStyle }} />

Change state based on scroll position - Styled Components

import React, { useState } from 'react'
import { useScrollPosition } from '@n8tb1t/use-scroll-position'

const [hideOnScroll, setHideOnScroll] = useState(true)

useScrollPosition(({ prevPos, currPos }) => {
  const isShow = currPos.y > prevPos.y
  if (isShow !== hideOnScroll) setHideOnScroll(isShow)
}, [hideOnScroll])

Get scroll position for custom element

  const [elementPosition, setElementPosition] = useState({ x: 20, y: 150 })
  const elementRef = useRef()

    // Element scroll position
  useScrollPosition(
    ({ currPos }) => {
      setElementPosition(currPos)
    }, [], elementRef
  )

Why to use

use-scroll-position returns the scroll position of the browser window, using a modern, stable and performant implementation.

Most of the time scroll listeners do very expensive work, such as querying dom elements, reading height / width and so on. use-scroll-position solves this by using throttling technic to avoid too many reflows (the browser to recalculate everything).

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].