当用户到达页面上的特定位置时,如何自动执行JavaScript功能?
当我向下滚动到页面底部时,应执行AJAX调用以加载更多内容,以便您可以进一步向下滚动。
如果用户向下滚动然后再向上滚动相同距离,则不会发生任何事情。
这是我拥有并想要执行的ajax函数的调用:
refresh('refresh_status', 'refresh_status.php', '', '', '', 'refresh');
答案 0 :(得分:0)
已修复默认JavaScript:http://cdpn.io/xdjmG
HTML:
<div id="page" style="min-height: 10000px;">
<div id="anotherElement" style="margin-top: 500px; min-height: 500px; background-color:red"></div>
</div>
JavaScript,当页面到达“anotherElement”时会发出警报:
window.addEventListener('scroll', function(){
var place = document.body.scrollTop;
var alertOn = document.getElementById('anotherElement').offsetTop;
if(place > alertOn){
alert('Function execute here');
this.removeEventListener('scroll', arguments.callee, false);
}
});