计算从当前位置到页面底部的距离
const distanceToBottom = document.body.scrollHeight - window.innerHeight - window.scrollY;
获取元素相对于文档的位置
const getPosition = (ele) => (
(r = ele.getBoundingClientRect()), { left: r.left + window.scrollX, top: r.top + window.scrollY }
);
getPosition(document.body); // { left: 0, top: 0 }
检查用户是否滚动到页面底部
const isAtBottom = () =>
document.documentElement.clientHeight + window.scrollY >= document.documentElement.scrollHeight;
滚动到页面顶部
const goToTop = () => window.scrollTo(0, 0);
重新加载页面
const reload = () => location.reload();
// Or
const reload = () => (location.href = location.href);