获取窗口属性
获取窗口属性
1. 滚动条
// 兼容
document.body.scrollLeft + document.documentElement.scrollLeft;
function getScrollOffset() {
if (window.pageXOffset) {
return {
x: window.pageXOffset,
y: window.pageYOffset,
};
} else {
return {
x: document.body.scrollLeft + document.documentElement.scrollLeft,
y: document.body.scrollTop + document.documentElement.scrollTop,
};
}
}
2. 可视区窗口的尺寸
function getViewportOffset() {
if (window.innnerWidth) {
return {
w: window.innerWidth,
h: window.innerHeight,
};
} else {
if (document.compatMode === "BackCompat") {
return {
w: document.body.clientWidth,
h: document.body.clientHeight,
};
} else {
return {
w: document.documentElement.clientWidth,
h: document.documentElement.clientHeight,
};
}
}
}
3. 查看元素几何属性
domEle.getBoundingClientRect(); // 舍弃 静态 兼容性好,ie无hw
dom.offsetHeight dom.offsetWidth // 视觉上的尺寸,包含padding
dom.offsetLeft dom.offsetTop // 距离父级 / 文档
dom.offsetParent // 有定位的父级
function getElementPosition() {}
4. 让滚动条滚动
window上有三个方法 scroll(0, 10); scrollTo(0, 10); // 一样 x定位至10 scrollBy(0, 10); // 向x滚动 10距离 // 小说自动阅读