瀑布流效果

瀑布流效果

1.使用js方法

Masonry.js
Isotope.js

原理:通过js来计算一共有多少个方格,在计算每一个方格的宽度和高度,匹配容器的宽度可以放置多少行;全部方格的position都设置为absolute,逐一计算top和left进行定位,因此当浏览器容器变动的时候,所有元素进行动态重新计算和排列;

2.使用css两个属性:column-count,column-gap

http://picsum.photos/360/460?random=1

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="day_10.css">
</head>
<body>
<div class="masonry">
    <div class="item"><img src="http://picsum.photos/360/460? random=1" alt=""></div>
    <div class="item"><img src="http://picsum.photos/360/460? random=2" alt=""></div>
    <div class="item"><img src="http://picsum.photos/360/460?random=3" alt=""></div>
    <div class="item"><img src="http://picsum.photos/360/460?random=4" alt=""></div>
    <div class="item"><img src="http://picsum.photos/360/460?random=5" alt=""></div>
    <div class="item"><img src="http://picsum.photos/360/460?random=6" alt=""></div>
    <div class="item"><img src="http://picsum.photos/360/460?random=7" alt=""></div>
    <div class="item"><img src="http://picsum.photos/360/460?random=8" alt=""></div>
    <div class="item"><img src="http://picsum.photos/360/460?random=9" alt=""></div>
    <div class="item"><img src="http://picsum.photos/360/460?random=10" alt=""></div>
    <div class="item"><img src="http://picsum.photos/360/460?random=11" alt=""></div>
    <div class="item"><img src="http://picsum.photos/360/460?random=12" alt=""></div>
    <div class="item"><img src="http://picsum.photos/360/460?random=13" alt=""></div>
    <div class="item"><img src="http://picsum.photos/360/460?random=14" alt=""></div>
    <div class="item"><img src="http://picsum.photos/360/460?random=15" alt=""></div>
</div>
</body>
</html>

CSS

*{
    margin: 0;
    padding: 0;
}
body{
    margin:4px;
    font-family: "Microsoft JhengHei UI Light";
}
.masonry{
    column-count:4;
    column-gap:0;
}
.item{
    padding:2px;
    position:relative;
    counter-increment:item-counter;
}
.item img{
    /*设置图片按照item容器的大小方法缩小*/
    display:block;
    width:100%;
    height:auto;
}
.item::after{
    content:counter(item-counter);
    position:absolute;
    display:block;
    top:2px;
    left:2px;
    width: 24px;
    height: 24px;
    text-align:center;
    line-height: 24px;
    background-color: #efeeee;
    color:#9370db;
}

column-count,column-gap

column-count:定义元素被划分的列数,默认值为auto;也可直接设置数值;
column-gap:指定列之间的距离

counter-increment

属性代表递增计数器的值

  • none:表示没有计数器递增
  • id number:id定义将增加计数的选择器,id,class; number定义增量,可为正数,0,负数
  • inherbit:继承父类

counter-reset

  • counter-reset属性创建或重置一个或多个计数器。 其默认值为none(不能对选择器的计数器进行重置);其属性值有 none ,id number ,inherit三个属性值。
  • id number:id 定义将重置计数的选择器、id 或 class。number 。可以设置选择器出现个数number 可以是正数、零或者负数。
  • inherit: 继承父类的属性值。

效果

瀑布流效果

缺点

是由上至下,由左至右的排列;

3.使用flex-box

*{
    margin: 0;
    padding: 0;
}
body{
    margin:4px;
    font-family: "Microsoft JhengHei UI Light";
}
.masonry{
    display:flex;
    flex-direction:column;
    flex-wrap:wrap;
    height: 1000px;

}

.item{
    position:relative;
    width: 25%;
    padding:2px;
    box-sizing:border-box;
    counter-increment:item-counter;
}
.item img{
    display: block;
    width: 100%;
    height:auto;
}
.item::after{
    content:counter(item-counter);
    position:absolute;
    display:block;
    top:2px;
    left:2px;
    width: 24px;
    height: 24px;
    text-align:center;
    line-height: 24px;
    background-color: #efeeee;
    color:#9370db;
}
.item:nth-child(4n+1){
    order:1;
}
.item:nth-child(4n+2){
    order:2;
}
.item:nth-child(4n+3){
    order:3;
}
.item:nth-child(4n){
    order:4;
}

效果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

this_is_Azou

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值