|
1 |
| -# compute-scroll-into-view |
2 |
| -Lower level API that is used by scroll-into-view-if-needed |
| 1 | +[](https://circleci.com/gh/stipsan/compute-scroll-into-view) |
| 2 | +[](https://npm-stat.com/charts.html?package=compute-scroll-into-view) |
| 3 | +[](https://www.npmjs.com/package/compute-scroll-into-view) |
| 4 | +[![gzip size][gzip-badge]][unpkg-dist] |
| 5 | +[![size][size-badge]][unpkg-dist] |
| 6 | +[![module formats: umd, cjs, and es][module-formats-badge]][unpkg-dist] |
| 7 | +[](https://github.com/semantic-release/semantic-release) |
| 8 | + |
| 9 | +Lower level API that is used by the [ponyfill](https://ponyfill.com) [scroll-into-view-if-needed](https://github.com/stipsan/scroll-into-view-if-needed) and others to compute where (if needed) elements should scroll based on [options defined in the spec](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView). |
| 10 | +Use this if you want the smallest possible bundlesize and is ok with implementing the actual scrolling yourself. |
| 11 | + |
| 12 | +## Install |
| 13 | + |
| 14 | +```bash |
| 15 | +yarn add compute-scroll-into-view |
| 16 | +``` |
| 17 | + |
| 18 | +The UMD build is also available on [unpkg](https://unpkg.com/compute-scroll-into-view/umd/): |
| 19 | + |
| 20 | +```html |
| 21 | +<script src="https://unpkg.com/compute-scroll-into-view/umd/compute-scroll-into-view.min.js"></script> |
| 22 | +``` |
| 23 | + |
| 24 | +You can find the library on `window.computeScrollIntoView`. |
| 25 | + |
| 26 | +## Usage |
| 27 | + |
| 28 | +```js |
| 29 | +// es6 import |
| 30 | +import computeScrollIntoView from 'compute-scroll-into-view' |
| 31 | +// or es5 |
| 32 | +const computeScrollIntoView = require('compute-scroll-into-view') |
| 33 | + |
| 34 | +const node = document.getElementById('hero') |
| 35 | + |
| 36 | +// same behavior as Element.scrollIntoView({block: "nearest", inline: "nearest"}) |
| 37 | +// see: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView |
| 38 | +const actions = computeScrollIntoView(node, { |
| 39 | + scrollMode: 'if-needed', |
| 40 | + block: 'nearest', |
| 41 | + inline: 'nearest', |
| 42 | +}) |
| 43 | + |
| 44 | +// same behavior as Element.scrollIntoViewIfNeeded(true) |
| 45 | +// see: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoViewIfNeeded |
| 46 | +const actions = computeScrollIntoView(node, { scrollMode: 'if-needed' block: 'center', inline: 'center' }) |
| 47 | + |
| 48 | +// Then perform the scrolling, use scroll-into-view-if-needed if you don't want to implement this part |
| 49 | +actions.forEach(({ el, top, left }) => { |
| 50 | + el.scrollTop = top |
| 51 | + el.scrollLeft = left |
| 52 | +}) |
| 53 | +``` |
| 54 | + |
| 55 | +[gzip-badge]: http://img.badgesize.io/https://unpkg.com/compute-scroll-into-view/umd/compute-scroll-into-view.min.js?compression=gzip&label=gzip%20size&style=flat-square |
| 56 | +[size-badge]: http://img.badgesize.io/https://unpkg.com/compute-scroll-into-view/umd/compute-scroll-into-view.min.js?label=size&style=flat-square |
| 57 | +[unpkg-dist]: https://unpkg.com/compute-scroll-into-view/umd/ |
| 58 | +[module-formats-badge]: https://img.shields.io/badge/module%20formats-umd%2C%20cjs%2C%20es-green.svg?style=flat-square |
0 commit comments