jQuery实现滚动到底部时自动加载更多的方法示例
网络 2018-02-25 1613
本文实例讲述了jQuery实现滚动到底部时自动加载更多的方法。分享给大家供大家参考,具体如下:
这里利用AJAX,实现滚动到底加载数据功能:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <script src="js/jquery.min.js"></script> <script type="text/javascript"> $(function () { AddSth(); }); $(function () { $(".main").unbind("scroll").bind("scroll", function (e) { var sum = this.scrollHeight; if (sum <= $(this).scrollTop() + $(this).height()) { AddSth(); } }); }); function AddSth() { $.ajax({ type: 'POST', url: "index.aspx/ReturnSth", dataType: "json", contentType: "application/json; charset=utf-8", //data: "", success: function (data) { json = $.parseJSON(data.d); for (var i in json) { var tbBody = "<ul><li>" + json[i].sth + "</li></ul>"; $(".main").append(tbBody); } $(".main").append("<hr />"); } }); } </script> </head> <body> <form id="form1" runat="server"> <div>下拉加载更多</div><br /> <div class="main" style="border: 1px solid red; height: 700px; overflow: auto;"></div> </form> </body> </html> |