【问题标题】:Run javascript function when div is scrolled to [duplicate]div滚动到[重复]时运行javascript函数
【发布时间】:2014-08-11 20:17:08
【问题描述】:

当用户滚动到 div 时,我如何运行 onclick 函数。尝试设置自动加载的无限滚动,而不是用户单击此按钮。

元素在另一个 div 中水平滚动。

<div id="bottom_end" class="next">
    <a onclick="load_more('/orderby/rising/page/12', '12', 'app_index')"></a>
    <p>Load more<br>in scroll</p>
</div>

【问题讨论】:

标签: javascript jquery scroll infinite


【解决方案1】:

您可以使用 Jquery 来实现它。

$( '#yourdiv').scroll(function() {
   if ( $(this)[0].scrollHeight - $(this).scrollTop() <= $(this).outerHeight()){
       //ajax method for getting your data
       //append response data to your div
    } 
});

请参考How to implement ajax using jquery

【讨论】:

  • 这对水平滚动有用吗?
  • 不,这适用于垂直滚动,但您也可以为水平滚动设置条件。无限自动滚动的逻辑相同。为此使用scrollWidth 和outerWidth。请参阅此jquery-height-width-inner-and-outer
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-23
  • 1970-01-01
  • 1970-01-01
  • 2017-04-19
  • 2013-06-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多