1. //css
  2. *{
  3. margin: 0;
  4. padding: 0;
  5. }
  6. .item{
  7. padding: 20px;
  8. float: left;
  9. border:1px solid #eee;
  10. font-size: 0;
  11. }
  12. .item img{
  13. width: 240px;
  14. }
  15. .content{
  16. margin: 0 auto;
  17. }
  18. //script
  19. <script>
  20. var baseURL = "http://192.168.4.18:8000/"
  21. var url = "more"
  22. axios({
  23. url,
  24. baseURL,
  25. method:"get",
  26. }).then(res=>{
  27. var a = res.data;
  28. a.forEach(item=>{
  29. var htmlItem=`
  30. <div class="item">
  31. <img src=${item.imageUrl}>
  32. </div>
  33. `
  34. $(".content").append(htmlItem)
  35. })
  36. var ww = $(window).width(); //获取窗口宽度
  37. var box = $(".item").outerWidth(); //获取item块宽度(包括内外边框)
  38. var num = Math.floor(ww/box); //得出行内item块个数
  39. var arr = [];
  40. $(".content").css({width:box*num + "px"})
  41. setTimeout(function(){
  42. $(".item").each((index,value)=>{
  43. if(index<num){
  44. var height = $(value).outerHeight();
  45. console.log(height)
  46. arr.push(height) //将第一行的item块高度加入一个列表中
  47. }else{
  48. var minHeight = Math.min(...arr); //找到高度最小的元素
  49. var index = arr.indexOf(minHeight); //找到高度最小的元素再数组中的下标
  50. var offsetLeft = $(".item").eq(index).offset().left; //找到其距矿口左边的距离
  51. $(value).css({position:"absolute",left:offsetLeft,top:minHeight});
  52. arr[index] = minHeight + $(value).outerHeight();//更新数组
  53. }
  54. })
  55. },1000)
  56. })
  57. </script>

下拉刷新

  1. <script>
  2. var baseURL = "http://192.168.4.18:8000/"
  3. var url = "more"
  4. axios({
  5. url,
  6. baseURL,
  7. method:"get",
  8. }).then(res=>{
  9. var a = res.data;
  10. a.forEach(item=>{
  11. var htmlItem=`
  12. <div class="item">
  13. <img src=${item.imageUrl}>
  14. </div>
  15. `
  16. $(".content").append(htmlItem)
  17. })
  18. var ww = $(window).width();
  19. var box = $(".item").outerWidth();
  20. var num = Math.floor(ww/box);
  21. var arr = [];
  22. $(".content").css({width:box*num + "px"})
  23. setTimeout(function(){
  24. $(".item").each((index,value)=>{
  25. if(index<num){
  26. var height = $(value).outerHeight();
  27. console.log(height)
  28. arr.push(height)
  29. }else{
  30. var minHeight = Math.min(...arr);
  31. var index = arr.indexOf(minHeight);
  32. var offsetLeft = $(".item").eq(index).offset().left;
  33. $(value).css({position:"absolute",left:offsetLeft,top:minHeight});
  34. arr[index] = minHeight + $(value).outerHeight();
  35. }
  36. })
  37. },100)
  38. })
  39. axios({
  40. url,
  41. baseURL,
  42. method:"get",
  43. }).then(res=>{
  44. var a = res.data;
  45. a.forEach(item=>{
  46. var htmlItem=`
  47. <div class="item">
  48. <img src=${item.imageUrl}>
  49. </div>
  50. `
  51. $(".content").append(htmlItem)
  52. })
  53. var ww = $(window).width();
  54. var box = $(".item").outerWidth();
  55. var num = Math.floor(ww/box);
  56. var arr = [];
  57. $(".content").css({width:box*num + "px"})
  58. setTimeout(function(){
  59. $(".item").each((index,value)=>{
  60. if(index<num){
  61. var height = $(value).outerHeight();
  62. console.log(height)
  63. arr.push(height)
  64. }else{
  65. var minHeight = Math.min(...arr);
  66. var index = arr.indexOf(minHeight);
  67. var offsetLeft = $(".item").eq(index).offset().left;
  68. $(value).css({position:"absolute",left:offsetLeft,top:minHeight});
  69. arr[index] = minHeight + $(value).outerHeight();
  70. }
  71. })
  72. },100)
  73. })
  74. $(window).scroll(function(){
  75. var scrollTop = $(window).scrollTop(); //获取滚动条距离顶部的高
  76. var availHeight = $(window).height(); //获取可视区域的高
  77. var dw = $(document).height(); //获取内容区域的高
  78. console.log(scrollTop)
  79. if((scrollTop + availHeight) > dw -100){
  80. axios({
  81. url,
  82. baseURL,
  83. method:"get",
  84. }).then(res=>{
  85. var a = res.data;
  86. a.forEach(item=>{
  87. var htmlItem=`
  88. <div class="item">
  89. <img src=${item.imageUrl}>
  90. </div>
  91. `
  92. $(".content").append(htmlItem)
  93. })
  94. var ww = $(window).width();
  95. var box = $(".item").outerWidth();
  96. var num = Math.floor(ww/box);
  97. var arr = [];
  98. $(".content").css({width:box*num + "px"})
  99. setTimeout(function(){
  100. $(".item").each((index,value)=>{
  101. if(index<num){
  102. var height = $(value).outerHeight();
  103. console.log(height)
  104. arr.push(height)
  105. }else{
  106. var minHeight = Math.min(...arr);
  107. var index = arr.indexOf(minHeight);
  108. var offsetLeft = $(".item").eq(index).offset().left;
  109. $(value).css({position:"absolute",left:offsetLeft,top:minHeight});
  110. arr[index] = minHeight + $(value).outerHeight();
  111. }
  112. })
  113. },100)
  114. })
  115. }
  116. })
  117. </script>