UNPKG

@krmao/react-basic

Version:
101 lines (100 loc) 4.17 kB
"use strict"; // noinspection JSUnusedGlobalSymbols,JSUnusedLocalSymbols,JSUnresolvedVariable Object.defineProperty(exports, "__esModule", { value: true }); var BasicScrollContainerUtil = /** @class */ (function () { function BasicScrollContainerUtil() { } BasicScrollContainerUtil.currentScrollTop = function (id) { var _a; return (_a = document.getElementById(id)) === null || _a === void 0 ? void 0 : _a.scrollTop; }; BasicScrollContainerUtil.scrollToTop = function (containerId, smooth) { if (smooth === void 0) { smooth = true; } var element = document.getElementById(containerId); if (!!element) { if (smooth) { element === null || element === void 0 ? void 0 : element.scrollTo({ top: 0, behavior: "smooth" }); } else { element.scrollTop = 0; } } }; BasicScrollContainerUtil.scrollToBottom = function (containerId, smooth) { if (smooth === void 0) { smooth = true; } var element = document.getElementById(containerId); if (!!element) { if (smooth) { element.scrollTo({ top: element.scrollHeight - element.clientHeight, behavior: "smooth" }); } else { element.scrollTop = element.scrollHeight - element.clientHeight; } } }; BasicScrollContainerUtil.isOnBottom = function (id) { var element = document.getElementById(id); return !!element && element.scrollTop === element.scrollHeight - element.clientHeight; }; BasicScrollContainerUtil.isOnBottomByScrollEvent = function (event) { return (event.target.scrollTop === event.target.scrollHeight - event.target.clientHeight); }; BasicScrollContainerUtil.isOnTop = function (id) { var element = document.getElementById(id); return (element === null || element === void 0 ? void 0 : element.scrollTop) === 0; }; BasicScrollContainerUtil.isOnTopByScrollEvent = function (event) { var _a; return ((_a = event.target) === null || _a === void 0 ? void 0 : _a.scrollTop) === 0; }; BasicScrollContainerUtil.scrollToLeft = function (containerId, smooth) { if (smooth === void 0) { smooth = true; } var element = document.getElementById(containerId); if (smooth) { element.scrollTo({ left: 0, behavior: "smooth" }); } else { element.scrollLeft = 0; } }; BasicScrollContainerUtil.scrollToRight = function (containerId, smooth) { if (smooth === void 0) { smooth = true; } var element = document.getElementById(containerId); if (smooth) { element.scrollTo({ left: element.scrollWidth - element.clientWidth, behavior: "smooth" }); } else { element.scrollLeft = element.scrollWidth - element.clientWidth; } }; /** * @param containerId * @param percent [0,1] * @param smooth */ BasicScrollContainerUtil.scrollToBottomPercent = function (containerId, percent, smooth) { if (smooth === void 0) { smooth = true; } var element = document.getElementById(containerId); if (smooth) { element.scrollTo({ top: (element.scrollHeight - element.clientHeight) * percent, behavior: "smooth" }); } else { element.scrollTop = (element.scrollHeight - element.clientHeight) * percent; } }; /** * @param containerId * @param percent [0,1] * @param smooth */ BasicScrollContainerUtil.scrollToRightPercent = function (containerId, percent, smooth) { if (smooth === void 0) { smooth = true; } var element = document.getElementById(containerId); if (smooth) { element.scrollTo({ left: (element.scrollWidth - element.clientWidth) * percent, behavior: "smooth" }); } else { element.scrollLeft = (element.scrollWidth - element.clientWidth) * percent; } }; return BasicScrollContainerUtil; }()); exports.default = BasicScrollContainerUtil;