jQuery 在滚动时加载更多数据 [英] jQuery load more data on scroll

查看:36
本文介绍了jQuery 在滚动时加载更多数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道如何在 div.loading 可见的情况下在滚动上实现更多数据.

I am just wondering how can i implement more data on scroll only if the div.loading is visible.

通常我们会寻找页面高度和滚动高度,看看是否需要加载更多数据.但是下面的例子就有点复杂了.

Usually we look for page height and scroll height, to see if we need to load more data. but the following example is little complicated then that.

下图就是一个很好的例子.下拉框中有两个 .loading div.当用户滚动内容时,无论哪个可见,它都应该开始为其加载更多数据.

Following image is perfect example. there are two .loading div's on the drop down box. When user scroll the content, whichever is visible it should start loading more data for it.

那么我如何才能知道 .loading div 是否对用户可见?所以我只能开始加载那个 div 的数据.

So how can i find out if .loading div is visible to user yet or not? So i can start loading data for that div only.

推荐答案

在 jQuery 中,使用滚动功能检查是否已到达页面底部.一旦你点击它,进行一个 ajax 调用(你可以在此处显示加载图像直到 ajax 响应)并获取下一组数据,将其附加到 div.当您再次向下滚动页面时,将执行此函数.

In jQuery, check whether you have hit the bottom of page using scroll function. Once you hit that, make an ajax call (you can show a loading image here till ajax response) and get the next set of data, append it to the div. This function gets executed as you scroll down the page again.

$(window).scroll(function() {
    if($(window).scrollTop() == $(document).height() - $(window).height()) {
           // ajax call get data from server and append to the div
    }
});

这篇关于jQuery 在滚动时加载更多数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆