通过onclick替换滚动操作

时间:2012-10-21 15:47:13

标签: javascript jquery ajax

我在滚动时使用了这段代码(必须适应它)自动加载(想想Twitter)我页面上的新帖子。

由于当用户快速滚动时,我的服务器资源很重,我希望在点击按钮时激活它,让我们说<button id="load_more">Load more</button>

任何人都可以帮我转换我的吗?我无法让它工作......(最后删除繁忙的指标)

以下是我用于autoscroll的代码:

<?php
 $maxPage = $this->Paginator->counter('%pages%');
 ?>
 <script type="text/javascript">
 var lastX = 0;
 var currentX = 0;
 var page = 1;

 $(window).scroll(function () {
 if (page < <?php echo $maxPage;?>) {
  currentX = $(window).scrollTop();
 if (currentX - lastX > 150 * page) {
  lastX = currentX - 150;
  page++;
  $("#busy-indicator").fadeIn();
  $.get('<?php echo $this->Html->url('/users/getmore_timeline/'.$this->Session->read('Auth.User.id')); ?>/page:' + page, function(data) {
  setTimeout(function(){
  $('#postList').append(data);
  var bi =  $("#busy-indicator");
       bi.stop(true, true);
       bi.fadeOut();
  }, 500);
 });
 }
 }
 });
 </script>

编辑:

我试过(从记忆中)

<button onlick"LoadMore()">Load More</button>

<?php  
 $maxPage = $this->Paginator->counter('%pages%');  
 ?>  
 <script type="text/javascript">  

function LoadMore () {  
 if (page < <?php echo $maxPage;?>) {  
  page++;  
  $("#busy-indicator").fadeIn();  
  $.get('<?php echo $this->Html-  >url('/users/getmore_timeline/'.$this->Session->read('Auth.User.id')); ?>/page:' + page, function(data) {
  setTimeout(function(){  
  $('#postList').append(data);  
  var bi =  $("#busy-indicator");  
       bi.stop(true, true);  
       bi.fadeOut();
  }, 500);
 });
 }
 };
 </script>

1 个答案:

答案 0 :(得分:0)

我不确定我是否错过了这里的内容,但如果你想要的只是一个按钮来调用函数LoadMore()

<强> HTML

<button id="load_more">Load more</button>

<强> JS

$('#load_more').on('click', function() {
    Loadmore()
})

PS:关于你的<button onlick"LoadMore()">Load More</button>,你有一个错字(非常有趣的一个:D),忘记了一个等号,你应该避免使用内联事件处理程序。