<template>
<div id="app">
<ul>
<li
ref='waterfallItem'
v-for="(item,index) in waterfallArr"
:key="index"
>
<img :src="item.img"> 第{{index+1}}张
<span>原价 {{item.price}}</span>
</li>
</ul>
</div>
</template>

  

<script>
export default {
data() {
return {
waterfallArr: [
{
id: 1,
img: require("./assets/images/1.jpg"),
price: "50",
},
{
id: 2,
img: require("./assets/images/2.jpg"),
price: "50",
},
{
id: 3,
img: require("./assets/images/3.jpg"),
price: "50",
},
{
id: 4,
img: require("./assets/images/4.jpg"),
price: "50",
},
{
id: 5,
img: require("./assets/images/5.jpg"),
price: "50",
},
{
id: 6,
img: require("./assets/images/6.jpg"),
price: "50",
},
{
id: 7,
img: require("./assets/images/5.jpg"),
price: "50",
},
{
id: 8,
img: require("./assets/images/6.jpg"),
price: "50",
},
],
array: [] //定义空数组存储元素高度
};
},
mounted() {
setTimeout(() => { // 防止图片高度没有加载出来
this.getWaterfall();
},100)
},
methods: {
getWaterfall() {
let columns = 2; //定义布局的列数为2
let item = this.$refs.waterfallItem; //获取每个子元素的DOM
console.log("item",item)
for (let i = 0; i < item.length; i++) {
//遍历整个子元素的DOM集合
if (i < columns) {
//小于columns的子元素作为第一行
item[i].style.top = 20 + 'px';
item[i].style.left = item[0].clientWidth * i + "px";
console.log("offsetWidth", item[0].clientHeight)
this.array.push(item[i].clientHeight); //遍历结束时,数组this.array保存的是第一行子元素的元素高度
console.log("this.array",this.array)
} else {
//大于等于columns的子元素将作其他行
let minHeight = Math.min(...this.array); // 找到第一列的最小高度
let index = this.array.findIndex(item => item === minHeight) // 找到最小高度的索引
//设置当前子元素项的位置
item[i].style.top = this.array[index] +25+ "px";
item[i].style.left = item[index].offsetLeft + "px";
//重新定义数组最小项的高度 进行累加
this.array[index]+= item[i].clientHeight
console.log("this.array[index]",this.array[index])
}
}
},
},
};
</script>

  

<style lang="scss" scoped>
#app {
width: 750px;
height: 100vh;
background-color: #0f0;
color: #333;
text-align: center;
font-size: 20px;
ul {
width: 750px;
height: 100%;
list-style: none;
box-sizing: border-box;
background-color: #fff;
overflow: hidden;
li {
width: 50%;
height: auto;
padding: 10px;
font-size: 14px;
position: absolute;
box-sizing: border-box;
margin: 0 0 10px 0;
overflow: hidden;
img {
width: 100%;
display: block;
height: auto;
}
span {
text-decoration: line-through;
line-height: 2;
margin-left: 10px;
}
}
}
}
</style>

  

vue实现瀑布流的更多相关文章

  1. vue-waterfall2 基于Vue.js 瀑布流组件

    vue-waterfall2 1.宽度自适应,数据绑定特效(适用于上拉加载更多) 2.自定义程度高 3.使用极为简便,适用于PC/移动端 4.提供resize(强制刷新布局-适用于下拉刷新)/mix( ...

  2. 自己实现vue瀑布流组件,含详细注释

    我知道vue有瀑布流插件vue-waterfall-easy,但是使用的时候与我的预期有部分别,所以就自己动手写了这个组件 人和动物的根本区别是是否会使用工具,我们不仅要会使用,还要会创造工具,别人提 ...

  3. vue.js实现瀑布流之vue-waterfall-easy

    想必大家应该很多都已经习惯了jquery的DOM操作,jquery的瀑布流实现起来也很容易. 但是,随着时代的发展,随着时代的进步..... 算了算了,扯远了,既然能找到这儿来,肯定是在vue.js上 ...

  4. 使用vue做移动端瀑布流分页

    讲到瀑布流分页有一个方法一定是要用到的 pullToRefresh() 这个也没什么好解释的,想了解的可以去百度一下 下面上代码 <div id="main" class=& ...

  5. 用vue.js写的一个瀑布流的组件

    用vue.js写的一个瀑布流的组件:https://segmentfault.com/a/1190000010741319 https://www.jianshu.com/p/db3cadc03402

  6. vue实现网络图片瀑布流 + 下拉刷新 + 上拉加载更多

    一.思路分析和效果图 用vue来实现一个瀑布流效果,加载网络图片,同时有下拉刷新和上拉加载更多功能效果.然后针对这几个效果的实现,捋下思路: 根据加载数据的顺序,依次追加标签展示效果: 选择哪种方式实 ...

  7. 分享一个Vue实现图片水平瀑布流的插件

    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 一.需求来源 今天碰到了一个需求,需要在页面里,用水平瀑布流的方式,将一些图片进行加载,这让我突然想起我很久以前写的一篇文章<JS两 ...

  8. vue 瀑布流实现

    <div class="myWrite" v-if="list.length==0"> - 这个福宝有点懒哦 - </div> < ...

  9. vue2.0的瀑布流组件-使用说明

    做一个小项目,需要瀑布流,就选他了,先看看效果 使用瀑布流布局组件:vue-waterfall-easy 下载引入: 方式一:直接从git上复制组件的完整代码,引入vue组件文件即可 import v ...

  10. vuejs和webpack项目(VueComponent)初尝试——瀑布流组件

    碎碎念:     好久不见,最近自己有些懈怠没更过多少博,主要原因之一是对自己学习方式的一些思考,翻看之前的博客多是记录学习笔记这反映出了自己对于前端还停留在学习-复习知识点的阶段压根没多少实践经验啊 ...

随机推荐

  1. 【爬虫+数据分析+数据可视化】python数据分析全流程《2021胡润百富榜》榜单数据!

    目录 一.爬虫 1.1 爬取目标 1.2 分析页面 1.3 爬虫代码 1.4 结果数据 二.数据分析 2.1 导入库 2.2 数据概况 2.3 可视化分析 2.3.1 财富分布 2.3.2 年龄分布 ...

  2. HBase详解(04) - HBase Java API使用

    HBase详解(04) - HBase Java API使用 环境准备 新建Maven项目,在pom.xml中添加依赖 <dependency> <groupId>org.ap ...

  3. CVE-2020-13933

    漏洞名称 Apache Shiro 身份验证绕过漏洞复现CVE-2020-13933 利用条件 Apache Shiro < 1.6.0 漏洞原理 Apache Shiro是一个强大且易用的Ja ...

  4. vulnhub靶场之FUNBOX: UNDER CONSTRUCTION!

    准备: 攻击机:虚拟机kali.本机win10. 靶机:Funbox: Under Construction!,下载地址:https://download.vulnhub.com/funbox/Fun ...

  5. [C++]C++11右值引用

    右值引用的概念(摘自C++Primer) 左值和右值的概念 1.左值和右值是表达式的属性,一些表达式要求生成左值,一些表达式要求生成右值:左值表达式通常是一个对象的身份,而一个右值表达式表示的是对象的 ...

  6. 使用“宝塔一键迁移”工具,将typecho博客迁移到京东云cvm云主机

    作者:京东科技 林中 服务器更换.网站搬家,对于很多开发者新手来说不是一件容易的事情,需要迁移网站程序.数据库,修改数据库连接文件等.在云迁移方案中,宝塔是非常简单好用的服务器运维面板,能够极大提升运 ...

  7. Python3+Selenium3自动化测试-(六)

    这里来说一说selenium中的等待方式,其实在webdriver只有两种类型等待方式,显式等待和隐式等待,之前是在程序运行过程中使用time模块中的sleep进行代码的休眠进行强制等待,是显式等待中 ...

  8. LeetCode_单周赛_329

    2544. 交替数字和 代码1 转成字符串,逐个判断 class Solution { public int alternateDigitSum(int n) { char[] s = (" ...

  9. el-transfer 数据量过大加载慢卡顿解决办法:el-transfer虚拟滚动懒加载的实现

    参考链接 1)https://github.com/GreenHandLittleWhite/blog/issues/152)https://github.com/GreenHandLittleWhi ...

  10. 解决Linux上tomcat解析war包中文文件乱码

    解决Linux上tomcat解析war包中文文件乱码 第一步 编辑tomcat/conf server.xml vim /usr/local/src/tomcat/conf/server.xml us ...