Home > Web Front-end > JS Tutorial > body text

How to use JavaScript to implement image waterfall flow layout?

WBOY
Release: 2023-10-20 15:30:50
Original
717 people have browsed it

如何使用 JavaScript 实现图片瀑布流布局?

How to use JavaScript to implement image waterfall flow layout?

Introduction:
With the popularity of social media, people’s demand for pictures continues to increase. Image waterfall layout is a popular way to display images, which allows images to be adaptively arranged at different heights and widths, thereby presenting a more beautiful and interesting effect. This article will introduce how to use JavaScript to implement a simple image waterfall layout and provide specific code examples.

1. Layout Principle
The basic principle of picture waterfall flow layout is to arrange pictures of different sizes in each column in order to achieve adaptive layout in a container. To achieve this goal, we can use JavaScript to dynamically calculate the height of each column and add new images to the column with the smallest height to achieve automatic adaptation.

2. Implementation steps

  1. Create HTML structure
    We first need to create an HTML structure to accommodate images. We can use a div or ul element to act as a container and create a corresponding child element for each image.
<div id="waterfall-container">
  <div class="column">
    <img src="image1.jpg">
  </div>
  <div class="column">
    <img src="image2.jpg">
  </div>
  ...
</div>
Copy after login
  1. Set CSS style
    In order to achieve the effect of waterfall flow layout, we need to set the width and number of columns of the container in CSS, and add styles to each column.
#waterfall-container {
  width: 1000px;
  column-count: 4;
  column-gap: 20px;
}

.column {
  display: inline-block;
  width: 100%;
}
Copy after login
  1. Use JavaScript to calculate the height of each column
    In order to dynamically calculate the height of each column, we can use JavaScript to get the width of the container and calculate it based on the width and proportion of the image to calculate the height of each column.
window.onload = function() {
  var container = document.getElementById("waterfall-container");
  var columns = container.getElementsByClassName("column");

  function calculateColumnHeight() {
    var containerWidth = container.offsetWidth;
    var columnWidth = containerWidth / columns.length;

    for (var i = 0; i < columns.length; i++) {
      var column = columns[i];
      var images = column.getElementsByTagName("img");
      var totalImageHeight = 0;

      for(var j = 0; j < images.length; j++) {
        var image = images[j];
        var imageWidth = image.offsetWidth;
        var imageHeight = image.offsetHeight;
        var imageRatio = imageWidth / imageHeight;
        var adjustedImageHeight = columnWidth / imageRatio;

        totalImageHeight += adjustedImageHeight;
      }

      column.style.height = totalImageHeight + "px";
    }
  }

  calculateColumnHeight();
  window.addEventListener("resize", calculateColumnHeight);
}
Copy after login
  1. Add new pictures
    The last step is to implement the function of adding new pictures. When a new image is loaded, we need to first find the column with the smallest height, add the new image to that column, and then recalculate the height of each column.
function addNewImage(imageUrl) {
  var container = document.getElementById("waterfall-container");
  var columns = container.getElementsByClassName("column");
  
  var minHeightColumn = columns[0];
  for (var i = 1; i < columns.length; i++) {
    if (columns[i].offsetHeight < minHeightColumn.offsetHeight) {
      minHeightColumn = columns[i];
    }
  }
  
  var newImage = document.createElement("img");
  newImage.src = imageUrl;
  minHeightColumn.appendChild(newImage);
  
  calculateColumnHeight();
}

addNewImage("image3.jpg");
Copy after login

Summary:
Through the above steps, we can use JavaScript to implement a simple image waterfall flow layout. By dynamically calculating the height of each column and adding new images to the column with the smallest height, we can achieve an adaptive effect. This layout method can create a unique and interesting effect when displaying images, improving user experience. Through practice and more exploration, we can further optimize the performance and effect of the entire layout, making the waterfall layout more smooth and beautiful.

The above is the detailed content of How to use JavaScript to implement image waterfall flow layout?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!