当前位置: 首页 > news >正文

vue2实现【瀑布流布局】

瀑布流

    • 1. 解释
    • 2. 形成结构和样式
    • 3. 自定义指令

1. 解释

在这里插入图片描述

  • 瀑布流特征:
  1. 等宽不等高:元素宽度固定,高度根据内容自适应。
  2. 错落排列:元素像瀑布一样从上到下依次填充,自动寻找最短列插入

体现:图中第一排1,2,3元素中,3是最短高度,就是该行最短列。

2. 形成结构和样式

<template><div class="page-main"><div class="card"><!-- v-waterfall是自定义指令 属性值是个回调函数--><div v-waterfall="el => updateLayout(el)" class="card-item" v-for="item in cardDate" :key="item.id":style="{ background: item.color, height: item.height }"><p class="text">{{ item.id }}</p></div></div></div>
</template>
//这是<script>data中的数据,结构是通过v-for遍历的cardDate: [{ id: 1, color: '#76da6e', height: '100px' },{ id: 2, color: '#57c797', height: '80px' },{ id: 3, color: '#54cac7', height: '70px' },{ id: 4, color: '#b16dc7', height: '120px' },{ id: 5, color: '#dc933d', height: '130px' },{ id: 6, color: '#b74acf', height: '90px' },{ id: 7, color: '#d93e5a', height: '80px' },{ id: 8, color: '#5474dd', height: '100px' },{ id: 9, color: '#e42526', height: '120px' },{ id: 10, color: '#e86c92', height: '90px' }],
<style scoped>
/* .page-main {position: relative;
} */.card {position: relative;margin: 0 auto;width: calc(3 * 200px);/* 3列,每列200px宽度*/
}.card-item {/* 相对于最近的非static定位的祖先元素(.card)定位(0,0) */position: absolute;width: 200px;/* 使得内容居中(可省略) */display: flex;align-items: center;justify-content: center;color: black;font-size: 24px;/* 监听到css样式变化,过渡0.3s效果 */transition: all 0.3s;
}
</style>

未加上js动态计算每个元素的位置—初始样子是将所有元素重叠在一起(如图)
在这里插入图片描述
然后就要计算出每块元素应该存在的位置(top和left)【css中每块元素position都是absolute,而且没有设置边偏移,默认都是0】
在这里插入图片描述
DOM元素尺寸属性不了解可以看这里有图文说明

<script>
export default {name: 'App',data() {return {cardDate: [{ id: 1, color: '#76da6e', height: '100px' },{ id: 2, color: '#57c797', height: '80px' },{ id: 3, color: '#54cac7', height: '70px' },{ id: 4, color: '#b16dc7', height: '120px' },{ id: 5, color: '#dc933d', height: '130px' },{ id: 6, color: '#b74acf', height: '90px' },{ id: 7, color: '#d93e5a', height: '80px' },{ id: 8, color: '#5474dd', height: '100px' },{ id: 9, color: '#e42526', height: '120px' },{ id: 10, color: '#e86c92', height: '90px' }],columnHeights: [0, 0, 0] //每列元素的高度}},directives: {waterfall: {inserted(el, binding) {const callback = binding.value || {};callback(el);}}},methods: {updateLayout(item) {const column = this.getMinColumnHeight(this.columnHeights);//计算每个元素位移x轴和y轴距离const itemTop = this.columnHeights[column];const itemLeft = column * item.clientWidth;item.style.transform = `translate(${itemLeft}px,${itemTop}px)`;//此时该元素item已经定位完成//则重新跟新原本最短列为this.columnHeights[column] + item.offsetHeight; this.columnHeights[column] += item.offsetHeight;},getMinColumnHeight(arr) {let min = Math.min(...arr);return arr.indexOf(min) != -1 ? arr.indexOf(min) : 0;}},
}
</script>

3. 自定义指令

directives: {waterfall: {inserted(el, binding) {console.log(binding);const callback = binding.value || {};callback(el);}}},

在这里插入图片描述
el则是每个cart-item元素

(注:本文例子参考这个b站视频,我进行了补充)

http://www.xdnf.cn/news/553357.html

相关文章:

  • 粤港澳编程题
  • 【HTML-2】HTML 标题标签:构建网页结构的基础
  • Tomcat配置详情
  • 解码数据语言:如何优雅的进行数仓字典建设?
  • C++:迭代器
  • C++数据结构——红黑树
  • 如何使用通义灵码辅助开发鸿蒙OS - AI编程助手提升效率
  • centos7配置静态ip 网关 DNS
  • 数据实时同步:inotify + rsync 实现数据实时同步
  • 《深入理解指针数组:创建与使用指南》
  • 【C/C++】static关键字的作用
  • 计算机图形学Games101笔记--几何
  • 计算机视觉与深度学习 | matlab实现ARIMA-WOA-CNN-LSTM时间序列预测(完整源码和数据)
  • VMD查看蛋白质-配体的分子动力学模拟轨迹
  • 【Redis实战篇】达人探店
  • Golang的代码注释规范与实践
  • 机器学习第十八讲:混淆矩阵 → 诊断模型在医疗检查中的误诊情况
  • 33、魔法防御术——React 19 安全攻防实战
  • 每日算法刷题Day11 5.20:leetcode不定长滑动窗口求最长/最大6道题,结束不定长滑动窗口求最长/最大,用时1h20min
  • AMO——下层RL与上层模仿相结合的自适应运动优化:让人形行走操作(loco-manipulation)兼顾可行性和动力学约束
  • 【优秀三方库研读】在 quill 开源库中 QUILL_MAGIC_SEPARATOR 的作用是什么,解决了什么问题
  • 【爬虫】12306自动化购票
  • 【VS Code】Qt程序的调试与性能分析
  • SN生成流水号并且打乱
  • LTX-Videov本地部署教程:时空扩散+多尺度渲染,重塑AI视频研究范式
  • 第 4 章:网络与总线——CAN / Ethernet / USB-OTG
  • STM32中的ADC
  • CSS之box-sizing、图片模糊、计算盒子宽度clac、(重点含小米、进度条案例)过渡
  • 喷涂喷漆机器人详解
  • python-leetcode 68.有效的括号