scroll-to-element
Version:
Smooth scrolling to an element via selector or node reference
33 lines (26 loc) • 1.18 kB
JavaScript
var scroll = require('./scroll-to');
function calculateScrollOffset(elem, additionalOffset, alignment) {
var body = document.body,
html = document.documentElement;
var elemRect = elem.getBoundingClientRect();
var clientHeight = html.clientHeight;
var documentHeight = Math.max( body.scrollHeight, body.offsetHeight,
html.clientHeight, html.scrollHeight, html.offsetHeight );
additionalOffset = additionalOffset || 0;
var scrollPosition;
if (alignment === 'bottom') {
scrollPosition = elemRect.bottom - clientHeight;
} else if (alignment === 'middle') {
scrollPosition = elemRect.bottom - clientHeight / 2 - elemRect.height / 2;
} else { // top and default
scrollPosition = elemRect.top;
}
var maxScrollPosition = documentHeight - clientHeight;
return Math.min(scrollPosition + additionalOffset + window.pageYOffset,
maxScrollPosition);
}
module.exports = function (elem, options) {
options = options || {};
if (typeof elem === 'string') elem = document.querySelector(elem);
if (elem) return scroll(0, calculateScrollOffset(elem, options.offset, options.align), options);
};