Home >Web Front-end >CSS Tutorial >How to implement waterfall flow layout using JS or CSS, several solutions are introduced

How to implement waterfall flow layout using JS or CSS, several solutions are introduced

青灯夜游
青灯夜游forward
2021-07-16 19:45:002662browse

This article will take you through the waterfall flow layout and introduce three reliable JS solutions and N unreliable CSS solutions. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How to implement waterfall flow layout using JS or CSS, several solutions are introduced

In the spirit of practicality, we will share today Waterfall flow layout (Yesterday a little brother asked me how to do it, I looked I couldn't find it after a long time. It turns out it was written on the intranet ).

Demo address: http://www.lilnong.top/static/html/waterfall.html

What is the waterfall flow layout?

For example, huapetal.com and Mogujie (I posted pictures below), these websites use waterfall flow layout when displaying content.

We also want to make a page that displays our design draft (fixed width, variable height). Waterfall flow is a great solution.

The core of the waterfall flow layout is based on a grid layout, and the height of the item list contained in each row is random (the height changes dynamically with its own content), and each item The list is arranged in a stack, and the most important thing is that there is no unnecessary space between the stacks. Let’s take a look at the picture above to see what the waterfall flow layout we are talking about looks like.

Website Mogujie 花pet网 JD VV
screenshot How to implement waterfall flow layout using JS or CSS, several solutions are introduced How to implement waterfall flow layout using JS or CSS, several solutions are introduced How to implement waterfall flow layout using JS or CSS, several solutions are introduced How to implement waterfall flow layout using JS or CSS, several solutions are introduced
Scheme Sub-channel absolute

##grid, inline, float Magic solution

can also be regarded as a pure CSS solution, in essence it is

Depend on document flow, from left to right, top to bottom.

Schemegridinlinefloatbootstrap-gridscreenshot

You can see that there is a very obvious concept of line in the document flow layout. When a line is stretched, a blank will be left, and the lines will not overlap. The most magical thing here is the float layout.

DOM structure

div.list     // 设置 gird 或者 block,注意清除浮动
  div.item   // 设置为 inline 或者 float,使其能流动
    img      // 设置定宽,高度自适应,间距等。

grid Solution description

.wrap-waterfall--grid img{vertical-align: top;width: 100px}
.wrap-waterfall--grid .list{
    display: grid;
    grid-gap: 10px;
    /* 可以看到,网格大小,占据位置是需要提前设定的 */
    grid-template-columns: repeat(4, 1fr);
    grid-auto-rows: minmax(50px, auto);
}
grid is better to use than flex in some cases. For example, you need to

break through the row limit

, but it only applies to fixed layouts, such as the layout below. How would you implement it if you don't use grid?

How to implement waterfall flow layout using JS or CSS, several solutions are introduced

There is a plan for gird to implement waterfall flow layout

, but I saw a few of them either with color blocks or image deformation and cropping. Use nth-child to set the height, is too scary.

Columns, flex CSS implementation

Unreliable solution is also a pure CSS solution. Compared with the above solution, the solution is acceptable , but there are still some problems.

The order is vertical first, then horizontally
  • (columns) Compatibility issue
  • (flex) needs to be given a fixed height, and the set column will exceed the setting. And the setting column cannot be filled.
How to implement waterfall flow layout using JS or CSS, several solutions are introduced How to implement waterfall flow layout using JS or CSS, several solutions are introduced How to implement waterfall flow layout using JS or CSS, several solutions are introduced How to implement waterfall flow layout using JS or CSS, several solutions are introduced
Schemescreenshot
columns flex
How to implement waterfall flow layout using JS or CSS, several solutions are introduced 1How to implement waterfall flow layout using JS or CSS, several solutions are introduced
The columns scheme

is naturally supported, and you only need to set it to the parent

columns: 4; column-gap: 6px;

.

flex scheme

flex-flow: column wrap;height: 2300px;

The default is horizontal arrangement, and it can be modified to vertical arrangement and Allows wrapping, and then wraps the content with a fixed height. Absolute, channel height calculation scheme

Reliable scheme

## Schemeabsolute Get the remainder channelCalculate the height channelHead screenshot

这里的方案就靠谱起来了,可以满足我们使用要求。

我们来回忆一下我们的需求:展示一些内容,内容有特性定宽,不定高。不定高一般是因为内容长度或者高度不一致导致的,常见内容又分为两种文字和图片

  • 文字的话,在没有异步字体的情况下,可以理解为同步就可以获取到盒子高度。

  • 图片的话,因为加载是异步的,所以获取盒子的真实高度也是异步的。但是这里一般分为两种情况

  • 无高度,那么可以通过onload来监听图片加载完成。等图片加载完成再去获取高度。

  • 有高度,这种方案一般用在封面图、或者文章中,在上传图片的时候会保存原图尺寸,这个时候我们就可以直接使用已有数据。

获取图片高度

// 用于获取图片的真实高度
naturalHeight: 1180
// 用于获取图片的真实宽度
naturalWidth: 1200

//用户获取图片当前的渲染高度(会受 css 影响)
height: 98
//用户获取图片当前的渲染宽度(会受 css 影响)
width: 100

// 可返回浏览器是否已完成对图像的加载。如果加载完成,则返回 true,否则返回 fasle。
complete 属性
// 可以监听到图片加载完成的动作
onload

基于上面的内容,那我们可以先判断 complete 属性,

function getImageSize(img){
    if(img.complete){
        return Promise.resolve({
            naturalHeight: img.naturalHeight,
            naturalWidth: img.naturalWidth,
            height: img.height,
            width: img.width,
        })
    }else{
        return new Promise((resolve, reject)=>{
            img.addEventListener('load', ()=>{
                resolve({
                    naturalHeight: img.naturalHeight,
                    naturalWidth: img.naturalWidth,
                    height: img.height,
                    width: img.width,
                })
            })
        })
    }
}
/*
// 测试用例
el = document.createElement('img');
el.src = 'http://cors-www.lilnong.top/favicon.ico?'+Math.random()

getImageSize(el).then(console.log).catch(console.error)
setTimeout(()=>getImageSize(el).then(console.log).catch(console.error), 1000)
*/

absolute 计算高度方案

因为普通的布局已经无法满足我们的需求,所以我们可以考虑通过 position: absolute 来使内容通过绝对定位来显示

核心操作就是维护每个元素的 left、top,然后使用 left 和 top 去渲染到正确位置。

getListPosition(){
    // 视口宽度 / 每列宽度 得出划分为几列
    let col = this.screenWidth / this.itemWidth >> 0;
    var arr = [];
    for(var i = 0; i < col; i++) arr.push({
        list: [],
        height: 0,
    })
    // 遍历所有元素
    this.listInfo.forEach((item,idx)=>{
        // 找到最低的一列
        var colIndex = 0;
        for(var i = 1; i < col; i++){
            if(arr[colIndex].height > arr[i].height){
                // colItem = arr[i]
                colIndex = i
            }
        }
        // 修改元素的信息
        // 所属列
        item.line = colIndex;
        // 计算之后的 top 距离
        item.top = arr[colIndex].height+ &#39;px&#39;;
        // 计算之后的 left 距离
        item.left = colIndex * (this.itemWidth + 10) + &#39;px&#39;

        // 累加操作
        arr[colIndex].list.push(item);
        arr[colIndex].height += item.height + 10;
    })
    return arr
},

通过计算,我们可以到,瀑布流布局下每个元素的位置,通过绝对定位就可以实现。

根据下标,来渲染到不同的通道 idx % 4

因为上个方案用到了绝对定位,那么有没有不用绝对定位的方案呢?回到我们的问题点上 定宽,不定高,那我们完全可以通过分开渲染放弃 absolute 来实现。

jsGroupList(){
    return this.list.reduce((s,n,idx)=>{
        // 根据下标,直接分配所属列
        s[idx % 4].push({idx: idx, item: n})
        return s
    }, [[],[],[],[],])
},

看开头是实现类似的功能的,但是有一个弊端(快来评论区回复呀)。

通过高度计算,然后分通道,避免 absolute

因为上一个方案是按下标分类的,其实瀑布流是按高度分类的,所以我们分类条件换成最低的列。

jsGroupHeightList(){
    var list = [
        {height: 0, list: []},{height: 0, list: []},
        {height: 0, list: []},{height: 0, list: []},
    ]
    // 遍历每个元素
    for(var i = 0; i < this.list.length; i++){
        // 当元素有大小的时候在进行操作。
        if(!this.listInfo[i].height) return list;
        // 默认第一个通道是最小高度列
        var minHeightItem = list[0];
        // 计算最小高度列
        list.forEach(v=>{
            if(v.height < minHeightItem.height) minHeightItem = v
        })
        // 把新的元素高度累加到列中。
        minHeightItem.height += this.listInfo[i].height
        // 把新的元素push到列中
        minHeightItem.list.push({idx: i, item: this.list[i]})
    }
    return list;
},

总结

好了,到这里我能想到的方案就都介绍了。你还有什么方案吗?咱们可以在评论区讨论一下可行性。接下来就是我们的方案总结了。

1How to implement waterfall flow layout using JS or CSS, several solutions are introduced 1How to implement waterfall flow layout using JS or CSS, several solutions are introduced 1How to implement waterfall flow layout using JS or CSS, several solutions are introduced
方案 优点 缺点 点评
columns 实现简单、纯 CSS 方案 兼容性 -
flex - 需要固定高度,填充难以控制等问题 -
float、inline、bootstrapGrid - - 没点大都用不出这方案
grid - - 可以nth-child模拟实现、或者等待兼容性 masonry
absolute 效果好 - JS计算无限可能
js普通通道 - 填充难以控制 -
js优化通道 效果好、无绝对定位 在出现夸列等操作的时候不是很好控制 -

更多编程相关知识,请访问:编程入门!!

The above is the detailed content of How to implement waterfall flow layout using JS or CSS, several solutions are introduced. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete