原生js实现瀑布流效果
作者:Tangerine. 时间:2023-09-04 07:11:02
本文实例为大家分享了js实现瀑布流效果的具体代码,供大家参考,具体内容如下
CSS样式:
<style>
.cont{margin: 0 auto;position: relative;}
.box{float: left;padding: 5px;}
.imgbox{border: black solid 1px;padding: 5px;border-radius: 5px;}
.imgbox img{width: 200px;display: block;}
</style>
HTML结构:
<div class="cont">
<div class="box">
<div class="imgbox">
<img src="img/1.jpg" >
</div>
</div>
//......此处省略雷同代码
<div class="box">
<div class="imgbox">
<img src="img/2.jpg" >
</div>
</div>
</div>
JavaScript代码:
<script>
onload = function(){
var wf = new WaterF();
wf.init();
}
class WaterF{
constructor(){
this.clientW = document.documentElement.clientWidth;
this.abox = document.querySelectorAll(".box");
this.cont = document.querySelector(".cont");
}
init(){
this.maxNum = parseInt(this.clientW / this.abox[0].offsetWidth);
this.cont.style.width = this.maxNum * this.abox[0].offsetWidth + "px";
this.firstLine();
this.otherLine();
}
firstLine(){
this.heightArr = [];
for(var i=0;i<this.maxNum;i++){
this.heightArr.push(this.abox[i].offsetHeight);
}
}
otherLine(){
for(var i=this.maxNum;i<this.abox.length;i++){
var min = Math.min(...this.heightArr);
var minIndex = this.heightArr.indexOf(min);
this.abox[i].style.position = "absolute";
this.abox[i].style.top = min + "px";
this.abox[i].style.left = minIndex * this.abox[0].offsetWidth + "px";
this.heightArr[minIndex] = this.heightArr[minIndex] + this.abox[i].offsetHeight;
}
}
}
</script>
来源:https://blog.csdn.net/m0_46370469/article/details/104707999
标签:js,瀑布流
0
投稿
猜你喜欢
关于python中map函数的使用
2022-10-30 22:43:16
Vue超详细讲解重试机制示例
2024-04-30 08:45:37
Python基于tkinter模块实现的改名小工具示例
2023-08-31 10:40:01
python中创建一个包并引用使用的操作方法
2023-05-19 03:06:09
Python ValueError: invalid literal for int() with base 10 实用解决方法
2023-06-18 17:00:40
在Python中将函数作为另一个函数的参数传入并调用的方法
2023-04-10 07:32:26
python偏函数partial用法
2023-09-24 22:25:06
Python利用matplotlib绘制折线图的新手教程
2021-08-18 18:00:01
go sync.Once实现高效单例模式详解
2024-02-05 05:49:37
如何设计高效合理的MySQL查询语句
2024-01-22 04:17:21
ASP函数过滤数组中重复数据方法
2010-01-02 20:32:00
通过实例简单了解python yield使用方法
2023-03-07 23:41:02
Python selenium使用autoIT上传附件过程详解
2023-05-23 03:59:13
详解python实现简单区块链结构
2023-01-05 22:47:14
详解Open Folder as PyCharm Project怎么添加的方法
2021-06-25 05:56:53
如何使用Git实现切换分支开发过程解析
2022-07-03 20:57:06
CPQuery 解决拼接SQL的新方法
2012-11-30 20:01:46
javaScript 删除字符串空格多种方法小结
2024-05-02 16:10:33
关于python线程池的四种实现方式
2023-08-23 06:52:30
Python实现Restful API的例子
2023-03-23 18:28:10