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. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7. <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
    8. <script src="lib/base.js"></script>
    9. <title>Document</title>
    10. <style>
    11. * {
    12. margin: 0;
    13. padding: 0
    14. }
    15. img {
    16. width: 240px;
    17. }
    18. .item {
    19. border: 1px solid rgb(131, 130, 130);
    20. padding: 20px;
    21. float: left;
    22. }
    23. </style>
    24. </head>
    25. <body>
    26. <div class="content">
    27. <div class="item">
    28. <img src="images/01.jpg" alt="">
    29. </div>
    30. <div class="item">
    31. <img src="images/02.jpg" alt="">
    32. </div>
    33. <div class="item">
    34. <img src="images/03.jpg" alt="">
    35. </div>
    36. <div class="item">
    37. <img src="images/04.jpg" alt="">
    38. </div>
    39. <div class="item">
    40. <img src="images/05.jpg" alt="">
    41. </div>
    42. <div class="item">
    43. <img src="images/06.jpg" alt="">
    44. </div>
    45. <div class="item">
    46. <img src="images/07.jpg" alt="">
    47. </div>
    48. <div class="item">
    49. <img src="images/08.jpg" alt="">
    50. </div>
    51. <div class="item">
    52. <img src="images/09.jpg" alt="">
    53. </div>
    54. <div class="item">
    55. <img src="images/10.jpg" alt="">
    56. </div>
    57. <div class="item">
    58. <img src="images/11.jpg" alt="">
    59. </div>
    60. <div class="item">
    61. <img src="images/12.jpg" alt="">
    62. </div>
    63. </div>
    64. <script>
    65. /* 假设是从服务器取得的数据 */
    66. var url = {
    67. dataUrl: [{
    68. src: "01.jpg"
    69. }, {
    70. src: "02.jpg"
    71. }, {
    72. src: "03.jpg"
    73. }, {
    74. src: "04.jpg"
    75. }, {
    76. src: "05.jpg"
    77. }, {
    78. src: "06.jpg"
    79. }, {
    80. src: "07.jpg"
    81. }, {
    82. src: "08.jpg"
    83. }, {
    84. src: "09.jpg"
    85. }, {
    86. src: "10.jpg"
    87. }, {
    88. src: "11.jpg"
    89. }, {
    90. src: "12.jpg"
    91. }]
    92. }
    93. /* 1.一排能放置几张图片 */
    94. window.onload = function () {
    95. place_img();
    96. url.dataUrl.forEach(item => {
    97. let list = `<div class="item"><img src="images/${item.src}"/></div>`
    98. $(".content").append(list);
    99. place_img()
    100. })
    101. }
    102. function place_img() {
    103. var ww = $(window).width()
    104. var itemWidth = $(".item").outerWidth();
    105. var num = Math.floor(ww / itemWidth);
    106. /* 2.将第一排的高度放置在一个数组中 */
    107. var arrHeight = []
    108. $(".item").each((index, item) => {
    109. if (index < num) {
    110. var itemHeight = $(item).outerHeight();
    111. arrHeight.push(itemHeight)
    112. } else {
    113. /* 3.从高度最小的地方开发放置图片 */
    114. var minHeight = Math.min(...arrHeight);
    115. var minIndex = arrHeight.indexOf(minHeight);
    116. var offsetLeft = $(".item").eq(minIndex).offset().left;
    117. $(".item").eq(index).css({
    118. position: 'absolute',
    119. top: minHeight,
    120. left: offsetLeft
    121. })
    122. /* 4.重置数组中的最小高度 */
    123. arrHeight[minIndex] = minHeight + $(".item").eq(index).outerHeight();
    124. }
    125. })
    126. }
    127. </script>
    128. </body>
    129. </html>