1.0.0-alpha.d4e115e7 • Published 8 years ago

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

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@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@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@purposeinplay/core-v2@alexkurasa/react-horizontal-scrolling-menu@alobato/use-scroll-to-error@bobojio/rich-markdown-editor@citychallenge/rich-markdown-editor@apicat/editor@archguard/archdoc-editor@andreyblinov/rich-markdown-editor@ampolon/rich-markdown-editor-v2@ajnandi/markdown-editor@akord/rich-markdown-editor@brainfish-ai/rich-markdown-editor@brotime/dino-scheduling@broncha/rich-markdown-editor@byhuz/huz-ui-yanyan@byhuz/huz-ui-prisma
2.0.2

3 years ago

2.0.1

3 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

6 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

8 years ago

1.1.18

8 years ago

1.1.17

8 years ago

1.1.16

8 years ago

1.1.15

8 years ago

1.1.14

8 years ago

1.1.13

8 years ago

1.1.12

8 years ago

1.1.11

8 years ago

1.1.10

8 years ago

1.1.9

8 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