How to use JS to implement waterfall layout of web pages

How to use JS to implement waterfall layout of web pages

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:

  • The picture is arranged in multiple rows of equal-width elements, and the following elements are added to them in sequence, with equal widths but different heights. The picture is scaled according to its original proportions until the width meets our requirements, and then placed in the specified positions in sequence according to the rules.
  • For ease of understanding, here are the HTML and CSS codes

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 image

function 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 bandwidth

 var 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 page

var dWidth = document.documentElement.clientWidth //page width var num = Math.floor(dWidth / imgWidth)
//Math.floor() rounds down

4. Compare image heights

Because 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. Illustrations

    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' //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:
  • JS realizes the effect of picture waterfall flow
  • Using js to achieve waterfall effect
  • js realizes the dynamic loading of data by waterfall flow bottoming out
  • js to achieve waterfall flow layout (infinite loading)
  • 3 ways to implement waterfall layout with JavaScript
  • Have you learned how to implement JavaScript waterfall flow?

<<:  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

Recommend

Several common redirection connection example codes in html

Copy code The code is as follows: window.location...

How to use docker to deploy front-end applications

Docker is becoming more and more popular. It can ...

Vue implements a simple shopping cart example

This article example shares the specific code of ...

JS 9 Promise Interview Questions

Table of contents 1. Multiple .catch 2. Multiple ...

HTML basics HTML structure

What is an HTML file? HTML stands for Hyper Text M...

JavaScript implements the generation of 4-digit random verification code

This article example shares the specific code for...

javascript to switch pictures by clicking a button

This article example shares the specific code of ...

User-centered design

I've been asked a lot lately about an apparen...

How to set up ssh password-free login to Linux server

Every time you log in to the test server, you alw...

An example of using Dapr to simplify microservices from scratch

Table of contents Preface 1. Install Docker 2. In...

Apache Log4j2 reports a nuclear-level vulnerability and a quick fix

Apache Log4j2 reported a nuclear-level vulnerabil...

Summary of five commands to check swap space in Linux

Preface Two types of swap space can be created un...