触发在近视口中单击

时间:2015-06-03 20:41:39

标签: javascript jquery ajax scroll infinite-scroll

我有一个“加载更多”按钮,它位于页脚。

现在当我滚动body并且正好在 #loadSomeMoreCrap之上时,我想触发runthisfunction();

任何人都可以帮我解决这个问题。我经历了JScroll,但没有找到我的答案。任何帮助都会很棒谢谢。

HTML

<div id="PasteCrapHere"></div>
<button id="loadSomeMoreCrap">Load More</button>

的jQuery

$('#loadSomeMoreCrap').click(function(){
   runthisfunction();
});
function runthisfunction(){
  //something done here! none your'e business
}

1 个答案:

答案 0 :(得分:1)

原油,但你可以添加任何你想要的东西:

$(document).scroll(function() {
  var b = $('#btn1').offset().top;
  var s = $(document).scrollTop() + $(window).height();
  if (s > b)
    YourFunctionCallHere();
});

function YourFunctionCallHere() {
  /* Fill container until it no longer fits on screen */
  while ($(document).scrollTop() + $(window).height() > $('#btn1').offset().top *.8) {
    $('#container').append(count + '<br>');
    count = count + 1;
  }
}

/* Initialize container with data */
window.count = 1;
$(function() {
  YourFunctionCallHere();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container"></div>
<button id="btn1">click me</button>