vuestic-ui
Version:
Vue 3 UI Framework
47 lines (46 loc) • 1.51 kB
JavaScript
const getTopCoordinate = (element) => element.offsetTop;
const getBottomCoordinate = (element) => element.offsetTop + element.offsetHeight;
const getCenterCoordinate = (element) => element.offsetTop + element.offsetHeight / 2;
const getScrollTop = (element, scrollTarget, verticalAlignment) => {
const viewHeight = scrollTarget.offsetHeight;
const currentPosition = scrollTarget.scrollTop;
const top = getTopCoordinate(element) - scrollTarget.offsetTop;
const center = getCenterCoordinate(element) - scrollTarget.offsetTop;
const bottom = getBottomCoordinate(element) - scrollTarget.offsetTop;
if (verticalAlignment === "start") {
return top;
}
if (verticalAlignment === "end") {
return bottom - viewHeight;
}
if (verticalAlignment === "center") {
return center - viewHeight / 2;
}
if (verticalAlignment === "any") {
if (top - currentPosition < 0) {
return top;
}
if (bottom - currentPosition > viewHeight) {
return bottom - viewHeight;
}
}
};
const scrollToElement = (element, options = {
scrollTarget: element.parentElement,
verticalAlignment: "any",
smooth: false
}) => {
const scrollTarget = options.scrollTarget || element.parentElement;
const top = getScrollTop(element, scrollTarget, options.verticalAlignment);
if (top === void 0) {
return;
}
scrollTarget.scroll({
top,
behavior: options.smooth ? "smooth" : "auto"
});
};
export {
scrollToElement as s
};
//# sourceMappingURL=scroll-to-element.mjs.map