1.0.0-alpha.f7841b3e • Published 8 years ago

smooth-scroll-into-view-if-needed v1.0.0-alpha.f7841b3e

Weekly downloads
25,966
License
MIT
Repository
github
Last release
8 years ago

CircleCI Status npm stat npm version semantic-release scroll-into-view-if-needed

This is a ponyfill for smooth scrolling scroll-into-view-if-needed.

Demo

Install

yarn add smooth-scroll-into-view-if-needed

Usage

import scrollIntoView from 'smooth-scroll-into-view-if-needed'
const node = document.getElementById('hero')

// If all you want is for all your users to have stuff smooth scroll into view
scrollIntoView(node, { behavior: 'smooth' })

// combine it with any of the other options
scrollIntoView(node, {
  behavior: 'smooth',
  scrollMode: 'if-needed',
  block: 'nearest',
  inline: 'nearest',
})

// It returns a promise that is resolved when the animation is finished
const sequence = async () => {
  const slide = document.getElementById('slide-3')
  // First smooth scroll to hero
  await scrollIntoView(node, { behavior: 'smooth' })
  // Then we scroll to a slide in a slideshow
  return scrollIntoView(slide, { behavior: 'smooth' })
}

Custom scrolling transition

If the default smooth scrolling ponyfill isn't the duration or easing you want, you can provide your own scrolling logic by giving behavior a callback.

import scrollIntoView from 'smooth-scroll-into-view-if-needed'
const node = document.getElementById('hero')

scrollIntoView(node, {
  // Your scroll actions will always be an array, even if there is nothing to scroll
  behavior: scrollActions =>
    // list is sorted from innermost (closest parent to your target) to outermost (often the document.body or viewport)
    scrollActions.forEach(([el, scrollTop, scrollLeft]) => {
      // implement the scroll anyway you want
      el.scrollTop = scrollTop
      el.scrollLeft = scrollLeft

      // If you need the relative scroll coordinates, for things like window.scrollBy style logic, just do the math
      const offsetTop = el.scrollTop - scrollTop
      const offsetLeft = el.scrollLeft - scrollLeft
    }),
  // all the other options (scrollMode, block, inline) still work, so you don't need to reimplement them (unless you really really want to)
})

More documentation will be added (hang in there)

dafunda-markdown-editormetta-editor@stellaris/react-rich-editordino-schedulingkontenbase-document-editor@rbrcsk/rich-markdown-editor@everything-registry/sub-chunk-2787@duhaha/editortest-react-npm-lmpswift-editorvalor-sheet-prohello-world-editormarktownmdsmirror@wapps/react-scroll@wcjx/editor@wedevign/markdown-editor@wedevign/rich-markdown-editor@wedevign/rich-markdown-editor-2@winter-love/utils@thoughtback/rich-markdown-editor@theirstoryinc/timedtext-remixeradarchitecture-uiappflow-clieasy-react-formedvoramarkem-toolkit-mba-clientflowstate-editor-tempflowstate-editor-temp-2flowstate-editorchoerodon-uidavid-markdown-editorbigquant-rich-markdown-editordework-rich-markdown-editorstyllo-markdown-editorsuchites-int-mattertipe-markdown-editortraverse-markdown-editorywb-editor-pdfyxkj-uixod-clientweb-browser-windowunified-cliznotes-editormpes-react-prosemirrornapx-uireact-accordion-zsaytum-smooth-scroll-toscrollsnap-controlsreact-horizontal-scrolling-menureact-horizontal-scrolling-menu-altreact-horizontal-scrolling-menu-dmperry-whiteplugin_backslashquick-markdown-editorrich-markdownrafae-rich-markdown-editorrich-markdown-editor-enhancedrich-markdown-editorrich-markdown-editor-customizablerich-markdown-editor-drepatedrich-markdown-editor-pstrich-markdown-editor-v2qy-self-use-editorreact-space-nav@i4o/oh-hi-markdown@i4o/ohm-old@hackernoon/rich-markdown-editor@iimm/react-shared@gordanielyan/tiptap-starter-kit@heathmont/moon-components@heathmont/moon-core-tw@hipop/rich-markdown-editor@hellotech/scheduling@isle-project/components@gaborottlik/rich-markdown-editor@fungible-systems/rich-markdown-editor@jiangweixian1994/rich-markdown-editor@jetsoms/rich-markdown-editor@jhon-idrovo/rich-markdown-editor@nicolaepaul/tiptap-starter-kit@syfxlin/tiptap-starter-kit@sunsama/rich-markdown-editor@techtrain/rich-markdown-editor@noted-md/rich-markdown-editor@notea/rich-markdown-editor@liwuming/rich-editor@taskcafe/rich-markdown-editor@kzfoo-libs/core@kyna/editor@locpd/rich-markdown-editor@jtui/carousel@ogiosnpmtest/editor@pinpt/notebook-editor@pnegahdar/rich-markdown-editor@preshonyee/react-horizontal-scrolling-menu@paragraph-xyz/rich-markdown-editor@4giving/r-m-e@purposeinplay/core
2.0.2

2 years ago

2.0.1

2 years ago

2.0.0

3 years ago

1.1.33

4 years ago

1.1.32

5 years ago

1.1.31

5 years ago

1.1.30

5 years ago

1.1.29

5 years ago

1.1.28

6 years ago

1.1.27

6 years ago

1.1.26

6 years ago

1.1.25

6 years ago

1.1.24

6 years ago

1.1.23

7 years ago

1.1.22

7 years ago

1.1.21

7 years ago

1.1.20

7 years ago

1.1.19

7 years ago

1.1.18

7 years ago

1.1.17

7 years ago

1.1.16

7 years ago

1.1.15

7 years ago

1.1.14

7 years ago

1.1.13

7 years ago

1.1.12

7 years ago

1.1.11

7 years ago

1.1.10

7 years ago

1.1.9

7 years ago

1.1.8

8 years ago

1.1.7

8 years ago

1.1.6

8 years ago

1.1.5

8 years ago

1.1.4

8 years ago

1.1.3

8 years ago

1.1.2

8 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1-alpha.4

8 years ago

1.0.1-alpha.3

8 years ago

1.0.1-alpha.2

8 years ago

1.0.1-alpha.1

8 years ago

1.0.1-alpha.0

8 years ago

1.0.2-alpha.0

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

1.0.0-alpha.0

8 years ago