Skip to content

Instantly share code, notes, and snippets.

@nicholasxjy
Last active March 6, 2018 07:35
Show Gist options
  • Save nicholasxjy/f711cb68d60f68a85e50245384698dd0 to your computer and use it in GitHub Desktop.
Save nicholasxjy/f711cb68d60f68a85e50245384698dd0 to your computer and use it in GitHub Desktop.
// 屏幕可视窗口大小
window.innerHeight // 标准浏览器及IE9+
document.documentElement.clientHeight // 标准浏览器及低版本IE标准模式
document.body.clientHeight // 低版本混杂模式
// 浏览器窗口顶部与文档顶部之间的距离,也就是滚动条滚动的距离
window.pagYOffset // 标准浏览器及IE9+
document.documentElement.scrollTop // 兼容ie低版本的标准模式
document.body.scrollTop // 兼容混杂模式
//get scroll top
var getScrollTop = function (element) {
if (element === window) {
return Math.max(window.pageYOffset || 0, document.documentElement.scrollTop);
}
return element.scrollTop;
};
var getElementTop = function (element) {
if (element === window) {
return getScrollTop(window);
}
return element.getBoundingClientRect().top + getScrollTop(window);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment