rc-hooks
Version:
React Hooks Library.
72 lines (71 loc) • 1.67 kB
JavaScript
import { isBrowser, isWindow } from 'ut2';
/**
* 获取垂直滚动的像素数
*
* @param el 滚动容器,默认 window
* @returns
*/
export var getScrollTop = function (el) {
if (el === void 0) { el = window; }
if (!isBrowser || !el) {
return 0;
}
if (isWindow(el)) {
return window.scrollY;
}
return el.scrollTop;
};
/**
* 获取元素内容高度,包括由于溢出导致的视图中不可见内容
*
* @param el 滚动容器,默认 window
* @returns
*/
export var getScrollHeight = function (el) {
if (el === void 0) { el = window; }
if (!isBrowser || !el) {
return 0;
}
if (isWindow(el)) {
return document.documentElement.scrollHeight;
}
return el.scrollHeight;
};
/**
* 获取元素内部的高度,包含内边距,但不包括水平滚动条、边框和外边距
*
* @param el 滚动容器,默认 window
* @returns
*/
export var getClientHeight = function (el) {
if (el === void 0) { el = window; }
if (!isBrowser || !el) {
return 0;
}
if (isWindow(el)) {
return document.documentElement.clientHeight;
}
return el.clientHeight;
};
/**
* 检查文档是否可见
*
* @returns
*/
export function isDocumentVisible() {
if (typeof document !== 'undefined' && typeof document.visibilityState !== 'undefined') {
return document.visibilityState !== 'hidden';
}
return true;
}
/**
* 检查是否在线
*
* @returns
*/
export function isOnline() {
if (typeof navigator !== 'undefined' && typeof navigator.onLine !== 'undefined') {
return navigator.onLine;
}
return true;
}