<!DOCTYPE html>
<html id="html">
<head>
<title></title>
</head>
<body id="body">
<div id="div" style="height: 2000px; width:100%"></div>
<div id="div2"></div>
<script>
window.onscroll = function(){
console.log(document.getElementById('html').scrollTop);//这种是给html加了id的
console.log(document.documentElement.scrollTop);//这种是不需要给html加id的
}
</script>
</body>
</html>
当html文档头部不包含任何“文档类型声明”时,需要用document.body.scrollTop获得正确的值,而document.documentElement.scrollTop的值为0