1. http://localhost:8000/more
    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. <style>
    9. .item {
    10. float: left;
    11. border: 1px solid #eee;
    12. }
    13. .item img {
    14. width: 240px;
    15. vertical-align: bottom;
    16. padding: 5px;
    17. }
    18. </style>
    19. </head>
    20. <body>
    21. <div id="app">
    22. </div>
    23. <script>
    24. http();
    25. $(window).scroll(function(){
    26. if(isReachBottom()){
    27. http();
    28. }
    29. })
    30. function http() {
    31. var url = "http://localhost:8000/more"
    32. $.ajax({
    33. url,
    34. success: res => {
    35. res.forEach(item => {
    36. var template = `
    37. <div class ="item">
    38. <img src = ${item.imageUrl}
    39. </div>
    40. `
    41. $("#app").append(template)
    42. })
    43. setTimeout(sortBox, 100)
    44. }
    45. })
    46. }
    47. function sortBox() {
    48. /* 1.获取一排能放几个盒子 */
    49. var ww = $(window).width();
    50. var box = $(".item").width();
    51. var num = Math.floor(ww / box);
    52. var arr = [];
    53. /* 2.将第一排的高度放到一个数组中 */
    54. $(".item").each((index, item) => {
    55. if (index < num) {
    56. var height = $(item).outerHeight();
    57. arr.push(height)
    58. } else {
    59. /* 3.从最小高度的地方添加图片 */
    60. var minHeight = Math.min(...arr);
    61. var index = arr.indexOf(minHeight);
    62. var offsetLeft = $(".item").eq(index).offset().left;
    63. $(item).css({position:"absolute",top:minHeight,left:offsetLeft});
    64. arr[index] = minHeight + $(item).outerHeight();
    65. }
    66. })
    67. }
    68. function isReachBottom(){
    69. var scrollTop = $(window).scrollTop();
    70. var availHeight = $(window).height();
    71. var dh = $(document).height();
    72. return (scrollTop+availHeight>dh-200)?true:false;
    73. }
    74. </script>
    75. </body>
    76. </html>