我想在页面滚动到最后一个块#scroll
时执行console.log()
,但它不起作用:
<div id="posts">my very own posts</div>
<div id="scroll" style="height:300px; width:100%" onscroll="onScroll()"></div>
<script>
function onScroll(){
console.log('YES');
}
</script>
<div>
内没有任何内容,因此内容不会滚动。
偶数侦听器始终引用它所在对象的滚动内容。
尝试将其放在<body>
标签上。
<body onscroll="onScroll()">
<p>lots of content. <p>lots of content. <p>lots of content
</body>
或者把东西放在<div>
里面,这样它就可以滚动了。
您的div
需要有overflow: scroll;
,并且其中包含一些内容,以便它有自己的滚动条。
<div style="height:50px; width:100%; overflow: scroll;" onscroll="onScroll()">
Blah
blah
<br>
some content
</div>
<script>
function onScroll(){
console.log('YES');
}
</script>