scroll-to-anchor
Advanced tools
Comparing version
module.exports = scrollToAnchor | ||
function scrollToAnchor (anchor) { | ||
function scrollToAnchor (anchor, options) { | ||
if (anchor) { | ||
try { | ||
var el = document.querySelector(anchor) | ||
if (el) el.scrollIntoView(true) | ||
if (el) el.scrollIntoView(options) | ||
} catch (e) {} | ||
} | ||
} |
@@ -5,3 +5,3 @@ { | ||
"repository": "yoshuawuyts/scroll-to-anchor", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"scripts": { | ||
@@ -8,0 +8,0 @@ "deps": "dependency-check . && dependency-check . --extra --no-dev", |
@@ -11,9 +11,12 @@ # scroll-to-anchor [![stability][0]][1] | ||
scrollToAnchor(window.location.hash) | ||
scrollToAnchor(window.location.hash, { behavior: 'smooth' }) | ||
``` | ||
## API | ||
### `scrollToAnchor(anchor)` | ||
### `scrollToAnchor(anchor, options)` | ||
Scroll to an anchor if possible. Does nothing if no anchor is provided or if the | ||
anchor doesn't exist on the page. | ||
You can also specify the same options as the [scrollIntoView](https://developer.mozilla.org/en/docs/Web/API/Element/scrollIntoView#Parameters) method which allows for more positioning options and scroll behaviour changes. | ||
## License | ||
@@ -20,0 +23,0 @@ [MIT](https://tldrlegal.com/license/mit-license) |
3699
8.99%36
9.09%