Preface:Waterfall flow, also known as waterfall layout, is a popular website page layout method. That is, multiple lines of equal-width elements are arranged, and the following elements are added to them in sequence, with equal widths but different heights. The images are scaled according to their original proportions until the width meets our requirements, and are placed in the specified positions in sequence according to the rules. What is waterfall layout?First look at the effect:
Incomplete HTML code: <div id="container"> <div class="box"> <div class="box-img"> <img src="./img/1.jpg" alt=""> </div> </div> <div class="box"> <div class="box-img"> <img src="./img/2.jpg" alt=""> </div> </div> <div class="box"> <div class="box-img"> <img src="./img/3.jpg" alt=""> </div> </div> </div> ......<!-- Pictures omitted, you can decide how many pictures you want--> Complete CSS code *{ padding: 0; margin: 0; } #container{ position: relative; } .box{ float: left; padding: 15px; } .box-img { width: 150px; padding: 5px; border: 1px solid #ccc ; box-shadow: 0 0 5px #ccc; border-radius: 5px; } .box-img img{ width: 100%; height: auto; } How to achieve:Simply put, if you want to achieve a waterfall layout, you have to do these things ✍ 1. Get the imagefunction getChildElemnt() { const contentArr = [] //Define an array to prepare for loading images const parent = document.getElementById(container) //Get the entire page const allContent = parent.getElementsByTagName('*') //Get the entire tag console.log(allContent); for (var i = 0; i < allContent.length; i++) { if (allContent[i].className == 'box') { contentArr.push(allContent[i])//Load the label of class='box' into the array} } console.log(contentArr); return contentArr //Return array} 2. Set the image bandwidthvar ccontent = getChildElemnt() var imgWidth = ccontent[0].offsetWidth // Make all images equal in width to the first image 3. Calculate the maximum number of images that can be stored in one line of a browser pagevar dWidth = document.documentElement.clientWidth //page width var num = Math.floor(dWidth / imgWidth) //Math.floor() rounds down 4. Compare image heightsBecause in the waterfall layout, when the first row of pictures is full, the first picture in the second row should be placed below the smallest picture in the first row. var BoxHeightArr = [] //Define an array and put the height of each picture into it in turn for (var i = 0; i < ccontent.length; i++) { if (i < num) { BoxHeightArr[i] = ccontent[i].offsetHeight //Store the height of the image in an array} else { //When the first row can no longer hold images var minHeight = Math.min.apply(null, BoxHeightArr) //Compare the minimum height of the previous row} } 5. Get the position of the image with the minimum height in the previous row//Define a getMinHeightLocation function, pass it all the pictures in the previous row of BoxHeightArr and the minimum height of the pictures in the previous row of minHeight function getMinHeightLocation(BoxHeightArr, minHeight) { for (var i in BoxHeightArr) { if (BoxHeightArr[i] === minHeight) { //When the image height is equal to the minimum height, the position of the image is the position of the minimum height image return i } } } 6. Illustrationsfor (var i = 0; i < ccontent.length; i++) { if (i < num) { BoxHeightArr[i] = ccontent[i].offsetHeight } else { var minHeight = Math.min.apply(null, BoxHeightArr) var minIndex = getMinHeightLocation(BoxHeightArr, minHeight) ccontent[i].style.position = 'absolute' //Absolutely position the image to be inserted, that is, the position of the element is specified by the "left", "top", "right" and "bottom" attributes ccontent[i].style.top = minHeight + 'px' //Make the distance from the inserted image to the top just equal to the height of the image to be inserted below it ccontent[i].style.left = ccontent[minIndex].offsetLeft + 'px' //Make the distance from the inserted image to the leftmost just equal to the distance from the image to be inserted below it to the leftmost BoxHeightArr[minIndex] = BoxHeightArr[minIndex] + ccontent[i].offsetHeight //After inserting the image, the height of this position must be set to the sum of the heights of the two images} } The complete code is as follows:Optimize code and improve performance window.onload = function() { imgLocation('container', 'box') //Constructor imgLocation } //Use window.onload = function() {} function to execute without waiting for the call in the body page //Get how many pictures are currently to be placed function imgLocation(parent, content) { //Let parent='container', content='box' // Get all the content under parent var cparent = document.getElementById(parent) var ccontent = getChildElemnt(cparent, content) var imgWidth = ccontent[0].offsetWidth var num = Math.floor(document.documentElement.clientWidth / imgWidth) cparent.style.cssText = `width: ${imgWidth * num} px` var BoxHeightArr = [] for (var i = 0; i < ccontent.length; i++) { if (i < num) { BoxHeightArr[i] = ccontent[i].offsetHeight } else { var minHeight = Math.min.apply(null, BoxHeightArr) var minIndex = getMinHeightLocation(BoxHeightArr, minHeight) ccontent[i].style.position = 'absolute' ccontent[i].style.top = minHeight + 'px' ccontent[i].style.left = ccontent[minIndex].offsetLeft + 'px' BoxHeightArr[minIndex] = BoxHeightArr[minIndex] + ccontent[i].offsetHeight } } // console.log(BoxHeightArr); } function getChildElemnt(parent, content) {parent='container', content='box' const contentArr = [] const allContent = parent.getElementsByTagName('*') console.log(allContent); for (var i = 0; i < allContent.length; i++) { if (allContent[i].className == content) { contentArr.push(allContent[i]) } } console.log(contentArr); return contentArr } function getMinHeightLocation(BoxHeightArr, minHeight) { for (var i in BoxHeightArr) { if (BoxHeightArr[i] === minHeight) { return i } } } The above is the details of how to use JS to implement the waterfall flow layout of web pages. For more information about JS to implement the waterfall flow layout of web pages, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: How to use mysql to complete the data generation in excel
>>: Three ways to check whether a port is open in a remote Linux system
Preface Recently, I was analyzing the startup pro...
The react version when writing this article is 16...
1. Go to the official website www.mysql.com and s...
I just joined a new company recently. After getti...
For some systems with large amounts of data, the ...
function 0. Display current time Command: select ...
Written in front A database is essentially a shar...
1. Definition of offsetParent: offsetParent is th...
Table of contents Preface 1. Brief Analysis of th...
This article shares the specific code of JS+AJAX ...
Table of contents DragEvent Interface DataTransfe...
Record the installation and configuration method ...
Overview Backup is the basis of disaster recovery...
1. Import the basic style of external CSS files U...
Since we are going to upload pictures, the first ...