1. 接口地址: http://localhost:3000/top/playlist/?cat=华语&limit=20&offset=${offset}
    1. function http({
    2. offset=0,
    3. success
    4. }){
    5. var url =`http://localhost:3000/top/playlist?cat=华语&limit=20&offset=${offset}`
    6. $.ajax({
    7. url,
    8. success:res=>{
    9. success(res)
    10. }
    11. })
    12. }
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Document</title>
    7. <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
    8. <script src="lib/http.js"></script>
    9. <style>
    10. .item img {
    11. width: 150px;
    12. height: 150px;
    13. padding: 5px;
    14. }
    15. .item {
    16. border: 1px solid #eee;
    17. float: left;
    18. margin-right: 80px;
    19. margin-top: 50px;
    20. }
    21. </style>
    22. </head>
    23. <body>
    24. <div id="app">
    25. </div>
    26. <script>
    27. /* 第一http请求 */
    28. loadData();
    29. jump();
    30. $(window).scroll(function(){
    31. if(isReachBottom()){
    32. var offset = $(".item").length;
    33. loadData(offset)
    34. }
    35. })
    36. function jump(){
    37. setTimeout(()=>{
    38. $(".item").click(function(event){
    39. let aid = event.currentTarget.dataset.aid;
    40. location.href = `datail.html?id=${aid}`
    41. })
    42. },200)
    43. }
    44. function isReachBottom() {
    45. var scrollTop = $(window).scrollTop();
    46. var availHeight = $(window).height();
    47. var dh = $(document).height();
    48. console.log(scrollTop + availHeight)
    49. return (scrollTop + availHeight == dh) ? true : false;
    50. }
    51. function loadData(offset){
    52. http({
    53. offset,
    54. success: res => {
    55. var playlists = res.playlists;
    56. playlists.forEach(item => {
    57. var template = `
    58. <div class="item" data-aid=${item.id}>
    59. <img src=${item.coverImgUrl}>
    60. </div>
    61. `
    62. $("#app").append(template)
    63. })
    64. }
    65. })
    66. }
    67. </script>
    68. </body>
    69. </html>