繁体   English   中英

用onclick替换滚动动作

[英]Replace scrolling action by onclick

我使用此代码段(必须对其进行调整)来在滚动时自动在页面上加载(认为Twitter)新帖子。

由于当用户快速滚动时这对于我的服务器来说是很重的资源,因此我希望在单击按钮时将其激活,可以说<button id="load_more">Load more</button>

谁能帮我转换我的? 我无法正常工作...(最后删除了忙碌指示器)

这是我用于自动滚动的代码:

<?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>

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

的HTML

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

JS

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM