1. 如果一个元素不能被滚动(例如,它没有溢出,或者这个元素有一个"non-scrollable"属性), scrollTop将被设置为0。
2. 设置scrollTop的值小于0,scrollTop 被设为0
3. 如果设置了超出这个容器可滚动的值, scrollTop 会被设为最大值。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>scrollHeight详测</title>
<style>
#box{
width:500px;
height: 200px;
overflow: auto;
border: 1px solid #ccc;
}
#box .inner {
height: 300px;
background-color: pink;
}
</style>
</head>
<body>
<div id="box">
<div class="inner">300px</div>
</div>
<script>
var el = document.getElementById("aa");
var box = document.getElementById("box");
console.log(box.scrollHeight);
box.scrollTop = -1;
console.log("scrollTop设置负值后 = ",box.scrollTop);
box.scrollTop = 300;
console.log("scrollTop设置超出这个容器可滚动的值后 = ",box.scrollTop);
</script>
</body>
</html>