douhuang9886 2016-09-12 21:57
浏览 31

这是在我的网站上创建无限滚动系统的正确方法吗?

I've recently learned the basics of SQL, Ajax and (object oriented) PHP, which I've put to use by creating a user build feed of information. However, at present the feed only loads the 20 most recent posts in the database (since loading all of them at once would have a negative impact on the page load time).

I've decided that trying to implement a system of infinite-scrolling (similar the method used by Twitter and Facebook) would be the best solution to this problem, but I'm not entirely sure how to do this.

I know that the first thing I need is a function to tell whether the user has reached the bottom of the page. To do this, I've written the following simple JQuery:

  $(window).scroll(function() { 
    var scrollBottom = $(document).height() - $(document).scrollTop() - $(window).height(); 
    if(scrollBottom < 200) {
      // send Ajax request
    }
  });

This bit of code should work, but I am concerned about the risk of multiple Ajax requests being sent to the server if the user scroll multiple times before the feed of posts has been updated.

I gather that the next thing I need to do is send off an Ajax request. Naturally, the script which currently retrieves the first 20 posts in the database uses a line of php/SQL looking like

$query = "SELECT * FROM table DESC LIMIT 0,20";

I'm guessing that I need to replace this with something like

$query = "SELECT * FROM table DESC LIMIT " . $x . "," . $x+20;

and then create another script which keeps track of how many times the Ajax request has been sent off, and use this to determine the variable $x to ensure the correct results are fetched.

Finally I know I'd need to create some sort of callback function to display the posts that had been fetched.

Is this the correct (best) method to use to create a system of infinite scrolling? Also, how can I alter the jQuery to stop multiple requests being sent off?

  • 写回答

1条回答 默认 最新

  • doudongdang4483 2016-09-12 22:17
    关注

    Yes you are correct, what you are looking for is $query = "SELECT * FROM table DESC LIMIT " . $x . ", 20"; which should get the next 20 rows and prevent more from being gotten until $x is updated.

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么