| New file |
| | |
| | | /** |
| | | * 计算某个类名元素的可用高度(视口高度 - 元素顶部距离) |
| | | * @param className 元素类名(不带.) |
| | | * @returns 可用高度(px),找不到元素则返回 0 |
| | | */ |
| | | export function getAvailableHeightByClass(className: string): number { |
| | | const el = document.querySelector(`.${className}`) as HTMLElement | null |
| | | if (!el) return 0 |
| | | const rect = el.getBoundingClientRect() |
| | | return window.innerHeight - rect.top |
| | | } |
| | | |
| | | /** |
| | | * 将字符串转为数字 |
| | | * @param value |
| | | * @returns |
| | | */ |
| | | export function tryConvertToInt(value: string): string | number { |
| | | const num = Number(value); |
| | | // 判断是否是有效数字且是整数(小数部分为 0) |
| | | if (!isNaN(num) && Number.isInteger(num)) { |
| | | return num; |
| | | } |
| | | |
| | | // 判断是否是有效数字且小数部分为 0(例如 "1.0") |
| | | if (!isNaN(num) && Number(value).toString().endsWith('.0')) { |
| | | return parseInt(value, 10); |
| | | } |
| | | |
| | | return value; |
| | | } |