PrefaceWhen I was browsing Xianyu today, I noticed that the height of each row was not the same. After learning about it, I realized that this was a waterfall flow layout. It felt very interesting, so I decided to study it and found some solutions on the Internet. There are about 3 ways to implement waterfall flow. 1. JS realizes waterfall flowThought Analysis
Code Implementation <!DOCTYPE html> <html> <head> <style> .box { width: 100%; position:relative; } .item { position: absolute; } .item img{ width: 100%; height:100%; } </style> </head> <body> <div class="box"> <div class="item"> <img src="banner.jpg" alt="" /> </div> <div class="item"> <img src="show.jpg" alt="" /> </div> <div class="item"> <img src="cloth.jpg" alt="" /> </div> <div class="item"> <img src="banner.jpg" alt="" /> </div> <div class="item"> <img src="show.jpg" alt="" /> </div> <div class="item"> <img src="cloth.jpg" alt="" /> </div> <div class="item"> <img src="banner.jpg" alt="" /> </div> <div class="item"> <img src="show.jpg" alt="" /> </div> <div class="item"> <img src="cloth.jpg" alt="" /> </div> <div class="item"> <img src="show.jpg" alt="" /> </div> <div class="item"> <img src="cloth.jpg" alt="" /> </div> <div class="item"> <img src="banner.jpg" alt="" /> </div> </div> </body> <script src="jquery.min.js"></script> <script> function waterFall() { // 1 Determine the width of the image - scroll bar width var pageWidth = getClient().width-8; var columns = 3; //3 columns var itemWidth = parseInt(pageWidth/columns); //Get the width of the item $(".item").width(itemWidth); //Set to the width of the item var arr = []; $(".box .item").each(function(i){ var height = $(this).find("img").height(); if (i < columns) { // 2 The first line is laid out in order$(this).css({ top:0, left:(itemWidth) * i+20*i, }); //Push the row height into the array arr.push(height); } else { // Other lines // 3 Find the minimum height and its index in the array var minHeight = arr[0]; var index = 0; for (var j = 0; j < arr.length; j++) { if (minHeight > arr[j]) { minHeight = arr[j]; index = j; } } // 4 Set the first box position of the next row // The top value is the height of the smallest column $(this).css({ top:arr[index]+30, //Set the distance to 30 left:$(".box .item").eq(index).css("left") }); // 5 Modify the height of the minimum column // The height of the minimum column = the current height + the height of the spliced column arr[index] = arr[index] + height+30; // Set a distance of 30} }); } //clientWidth handles compatibility function getClient() { return { width: window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth, height: window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight } } // Triggered in real time when the page size changes window.onresize = function() { //Redefine the waterfall flowwaterFall(); }; // Initialize window.onload = function(){ //Realize waterfall flowwaterFall(); } </script> </html> The effect is as follows 2. Column multi-line layout to achieve waterfall flowThought analysis:
Code implementation: <!DOCTYPE html> <html> <head> <style> .box { margin: 10px; column-count: 3; column-gap: 10px; } .item { margin-bottom: 10px; } .item img{ width: 100%; height:100%; } </style> </head> <body> <div class="box"> <div class="item"> <img src="banner.jpg" alt="" /> </div> <div class="item"> <img src="show.jpg" alt="" /> </div> <div class="item"> <img src="cloth.jpg" alt="" /> </div> <div class="item"> <img src="banner.jpg" alt="" /> </div> <div class="item"> <img src="show.jpg" alt="" /> </div> <div class="item"> <img src="cloth.jpg" alt="" /> </div> <div class="item"> <img src="banner.jpg" alt="" /> </div> <div class="item"> <img src="show.jpg" alt="" /> </div> <div class="item"> <img src="cloth.jpg" alt="" /> </div> <div class="item"> <img src="show.jpg" alt="" /> </div> <div class="item"> <img src="cloth.jpg" alt="" /> </div> <div class="item"> <img src="banner.jpg" alt="" /> </div> </div> </body> The effect is as follows: 3. Flex layout to achieve waterfall flowThought analysis: To achieve waterfall flow with flex, the outermost element needs to be set to display: flex, that is, horizontal arrangement. Then set flex-flow: column wrap to wrap the columns. Set height: 100vh to fill the height of the screen to accommodate child elements. The width of each column can be set using the calc function, i.e. width: calc(100%/3 - 20px). Divide into 3 columns of equal width minus the left and right margins. Code implementation: <!DOCTYPE html> <html> <head> <style> .box { display: flex; flex-flow:column wrap; height: 100vh; } .item { margin: 10px; width: calc(100%/3 - 20px); } .item img{ width: 100%; height:100%; } </style> </head> <body> <div class="box"> <div class="item"> <img src="banner.jpg" alt="" /> </div> <div class="item"> <img src="show.jpg" alt="" /> </div> <div class="item"> <img src="cloth.jpg" alt="" /> </div> <div class="item"> <img src="banner.jpg" alt="" /> </div> <div class="item"> <img src="show.jpg" alt="" /> </div> <div class="item"> <img src="cloth.jpg" alt="" /> </div> <div class="item"> <img src="banner.jpg" alt="" /> </div> <div class="item"> <img src="show.jpg" alt="" /> </div> <div class="item"> <img src="cloth.jpg" alt="" /> </div> <div class="item"> <img src="show.jpg" alt="" /> </div> <div class="item"> <img src="cloth.jpg" alt="" /> </div> <div class="item"> <img src="banner.jpg" alt="" /> </div> </div> </body> The effect is as follows: 4. Comparison of 3 methodsIf it is just a simple page display, you can use column multi-column layout and flex flexible layout. If you need to add data dynamically, or set the number of columns dynamically, you need to use JS + jQuery. The above are three ways to implement waterfall flow layout introduced by the editor. I hope it will be helpful to everyone. I would also like to thank everyone for their support of the 123WORDPRESS.COM website! You may also be interested in:
|
<<: Basic usage of wget command under Linux
>>: Simple implementation of html hiding scroll bar
Table of contents 1. What is an Operating System ...
Introduction to Nginx Nginx is a high-performance...
When I created a Docker container today, I accide...
Table of contents 1 Version and planning 1.1 Vers...
Introduction: When using MySQL to create a table,...
In the vue scaffolding, we can see that in the ne...
After MySQL database optimization, not only can t...
1|0 Compile the kernel (1) Run the uname -r comma...
This project shares the specific code of Vue+Rout...
As a lightweight open source database, MySQL is w...
Table of contents 1. Component 2. keep-alive 2.1 ...
Table of contents # Post-data preparation # SQL q...
Docker Installation curl -fsSL https://get.docker...
Table of contents 1. Setup 1. The first parameter...
Recently, I encountered many problems when instal...