第一种方式:使用CSS和JavaScript来实现

| <div class="waterfall-container"> |
| |
| <div class="waterfall-column"> |
| <div class="waterfall-item"> |
| |
| <div class="waterfall_item_box"></div> |
| </div> |
| <div class="waterfall-item"> |
| |
| <div class="waterfall_item_box"></div> |
| </div> |
| |
| </div> |
| |
| |
| <div class="waterfall-column"> |
| <div class="waterfall-item"> |
| |
| <div class="waterfall_item_box"></div> |
| </div> |
| <div class="waterfall-item"> |
| |
| <div class="waterfall_item_box"></div> |
| </div> |
| |
| </div> |
| </div> |
| |
| <style> |
| .waterfall-container { |
| display: flex; |
| justify-content: space-between; |
| } |
| |
| .waterfall-column { |
| flex: 1; |
| } |
| |
| .waterfall-item { |
| margin-bottom: 20px; |
| background-color: #f2f2f2; |
| border-radius: 5px; |
| padding: 10px; |
| } |
| </style> |
| |
| |
| <script> |
| window.addEventListener('load', function() { |
| |
| const columns = document.querySelectorAll('.waterfall-column'); |
| |
| |
| columns.forEach(column => { |
| |
| const items = column.querySelectorAll('.waterfall-item'); |
| |
| |
| items.forEach(item => { |
| |
| const contentHeight = item.querySelector('.waterfall_item_box').offsetHeight; |
| |
| |
| item.style.height = contentHeight + 'px'; |
| |
| |
| }); |
| }); |
| }); |
| </script> |
复制
第二种方式:
注意要引入jQuery库
。

代码如下:
| <!DOCTYPE html> |
| <html> |
| <head> |
| <meta charset="utf-8"> |
| <title></title> |
| <script src="html/js/jquery.min.js"></script> //引入jq库 |
| <style> |
| .waterfall{ |
| position: relative; |
| margin: 0 auto; |
| } |
| .column{ |
| position: absolute; |
| width: 200px; |
| margin: 4px; |
| border: 1px solid #ddd; |
| transition: all 1s; |
| } |
| .column img{ |
| width: 100%; |
| height: 100%; |
| display: block; |
| } |
| |
| </style> |
| </head> |
| <body> |
| <div class="waterfall"> |
| <div class="column"> |
| <img src="https://img2.baidu.com/it/u=3853345508,384760633&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=1200" alt="Image 1"> |
| </div> |
| <div class="column"> |
| <img src="https://img1.baidu.com/it/u=1458656822,2078909008&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=750" alt="Image 2"> |
| </div> |
| <div class="column"> |
| <img src="https://img.zhisheji.com/bbs/forum/201401/05/153945tbr7pg5torfzptso.jpg" alt="Image 2"> |
| </div> |
| <div class="column"> |
| <img src="https://img1.baidu.com/it/u=1458656822,2078909008&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=750" alt="Image 2"> |
| </div> |
| ... |
| |
| </div> |
| </body> |
| <script> |
| $(function(){ |
| init(); |
| $(window).on('resize',function(){ |
| init(); |
| }); |
| }) |
| |
| function init(){ |
| var boxWidth=$(".column").outerWidth(true); |
| |
| var cols=parseInt($(window).width()/boxWidth); |
| |
| var heightArr=[]; |
| for(var i=0;i<cols;i++){ |
| heightArr.push(0); |
| }; |
| |
| |
| $(".column").each(function(index,column) { |
| var idx=0; |
| var minBoxHeight=heightArr[0]; |
| |
| for(var i=0;i<heightArr.length;i++){ |
| if(heightArr[i]<minBoxHeight){ |
| minBoxHeight=heightArr[i]; |
| idx=i; |
| |
| } |
| }; |
| |
| |
| $(column).css({ |
| left:boxWidth*idx, |
| top:minBoxHeight |
| }); |
| heightArr[idx]+=$(column).outerHeight(true); |
| |
| }); |
| }; |
| </script> |
| </html> |
复制
完成~