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
Copy code The code is as follows: window.location...
1. Permanent modification, valid for all users # ...
Table of contents umask Umask usage principle 1. ...
Docker is becoming more and more popular. It can ...
This article example shares the specific code of ...
Table of contents 1. Multiple .catch 2. Multiple ...
What is an HTML file? HTML stands for Hyper Text M...
This article example shares the specific code for...
background Last week the company trained on MySQL...
This article example shares the specific code of ...
I've been asked a lot lately about an apparen...
Every time you log in to the test server, you alw...
Table of contents Preface 1. Install Docker 2. In...
Apache Log4j2 reported a nuclear-level vulnerabil...
Preface Two types of swap space can be created un...