scroll 事件到底是發生在那個元素上的?

測試的瀏覽器有 1.  chrome : Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.170 Safari/537.36
        2. safari: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/604.4.7 (KHTML, like Gecko) Version/11.0.2 Safari/604.4.7html

        3. Firefox: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:59.0) Gecko/20100101 Firefox/59.0
代碼以下:
chrome

<!DOCTYPE html>

<html>

<head>
<meta charset="UTF-8">
</head>
<style>
.scroll-container {
width: 200px;
border: 10px solid blue;
height: 300px;
overflow: scroll;
}

.scroll-container>div {
height: 1000px;
background-color: burlywood;
}
</style>

<body>
<div class="scroll-container" id="scrollContainer">
<div id="scrollChild">
1 2 3
</div>
</div>
</body>
<script>
window.onload = function () {
document.getElementById('scrollChild').onscroll = function () {
console.log('scrollChild scroll?')
}
document.getElementById("scrollContainer").onscroll = function () {
console.log('scrollContainer scroll?')
}
}
</script>

</html>

結果都是在控制檯打印出  scrollContainer scroll?瀏覽器

why? 爲何事件的監聽須要在外側包裹元素(父元素)上?而且scrollTop等值在child Element 上面都是零,在父元素上纔是有值的~
在MDN文檔上面
 
並無特別說明須要在父元素監聽scroll事件,說明理應打印的是scrollChild scroll?
待續。。。
相關文章
相關標籤/搜索