代码编辑器:
x
1
<style>
2
div {
3
border: 1px solid black;
4
width: 200px;
5
height: 100px;
6
overflow: scroll;
7
}
8
</style>
9
<p>演示了如何使用 addEventListener() 方法向 div 元素中添加 "onscroll" 事件</p>
10
<p>尝试滚动 div</p>
11
<div id="myDIV">In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.
12
<br><br>
13
'Whenever you feel like criticizing anyone,' he told me, just remember that all the people in this world haven't had the advantages that you've had.'</div>
14
<p id="demo"></p>
15
<script>
16
document.getElementById("myDIV").addEventListener("scroll", myFunction);
17
function myFunction() {
18
document.getElementById("demo").innerHTML = "你滚动了 div。";