簡體   English   中英

當用戶到達頁面中的特定元素時,如何執行JS函數?

[英]How to execute a JS function when the user reaches a specific element in the page?

當用戶到達頁面上的特定位置時,如何自動執行JavaScript函數?

當我向下滾動到頁面底部時,將執行AJAX調用,該調用將加載更多內容,以便您可以進一步向下滾動。

如果用戶向下滾動然后再次向上滾動相同的距離,則不會發生任何事情。

這是我要執行的ajax函數的調用:

refresh('refresh_status', 'refresh_status.php', '', '', '', 'refresh');

已修復默認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);
  }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM