Vue瀑布流插件的使用示例
我自己写的一个的Vue瀑布流插件,列数自适应,不用设置每个卡片的高度。
测试页面:Page.vue 模板页面:WaterFollow.vue 和 WFColumn.vue
在Page.vue中,修改itemW的值,设置每列的最小宽度。例如:itemW="200"(意为200px)。list为数组。高度不用设置,:style="{height:item+'px'}"是我为了创造卡片高度加上去的,不加就显示卡片的原来大小。
经测试,created加载数据正常,mounted加载、更新数据正常。
Page.vue
<template> <div class="container"> <w-f-column itemW="200"> <template slot-scope="{columnNum,columnIndex}"> <water-follow :list="list" :columnNum="columnNum" :columnIndex="columnIndex"> <template slot-scope="{item,index}"> <div class="my-box" :style="{height:item+'px'}">{{item}}-{{index}}</div> </template>//前端全栈学习交流圈:866109386 </water-follow>//面向1-3年前端人员 </template>//帮助突破技术瓶颈,提升思维能力。 </w-f-column> </div> </template> <script> import WFColumn from '../waterFollow/WFColumn' import WaterFollow from '../waterFollow/WaterFollow' export default { name: 'page', components: {WaterFollow, WFColumn}, data () { return { list: [] } }, created () { // 有初始数据 for (let i = 0; i < 50; i++) { this.list.push(Math.floor(Math.random() * 301 + 200)) } }, mounted () { // 模拟网络请求 // window.setTimeout(() => { // this.list = [] // for (let i = 0; i < 50; i++) { // this.list.push(Math.floor(Math.random() * 301 + 200)) // } // }, 1000) // -- 分割 -- // 模拟数据不断变化 // window.setInterval(() => { // this.list = [] // for (let i = 0; i < 50; i++) { // this.list.push(Math.floor(Math.random() * 301 + 200)) // } // }, 1000) } } </script> <style scoped lang="scss"> .container{ width: 100%; background: gray; .my-box{ width: 200px; background: #000; margin-bottom: 20px; color: #fff; } } </style>
WFColumn.vue
<template> <div class="wf-container"> <div class="wf-column" v-for="(item,index) in columnNum" :key="'column-'+index" :name="index"> <slot :columnNum="columnNum" :columnIndex="index"></slot> </div> </div> </template> <script> export default { name: 'WFColumn', props: ['itemW'], data () { return { columnNum: 0 } }, created () { this.columnNum = Math.floor(document.body.clientWidth / this.itemW) window.onresize = () => { this.columnNum = Math.floor(document.body.clientWidth / this.itemW) } } } </script> <style scoped lang="scss"> .wf-container{ width: 100%; display: flex; .wf-column{ flex: 1; } } </style>
WaterFollow.vue
<template> <div> <div v-for="(item,index) in list" :key="'item-'+index" class="item" :id="'card-'+columnIndex+'-'+index" v-if="load?(record[index].index===columnIndex):true"> <slot :item="item" :index="index"></slot> </div> </div> </template> <script> export default { name: 'WaterFollow', props: ['list', 'columnNum', 'columnIndex'], data () { return { column: 0, record: [], load: false, update: false } }, methods: { calculateColumn () { let cList = [] for (let i = 0; i < this.columnNum; i++) { cList.push(0) } for (let i = 0; i < this.record.length; i++) { let index = 0 for (let j = 0; j < cList.length; j++) { if (cList[index] > cList[j]) { index = j } } cList[index] += this.record[i].height this.record[i].index = index } }, recordInit () { for (let i = 0; i < this.list.length; i++) { this.record.push({index: -1, height: -1}) } }, initHeightData () { for (let i = 0; i < this.list.length; i++) { if (document.getElementById('card-' + this.columnIndex + '-' + i)) { let h = document.getElementById('card-' + this.columnIndex + '-' + i).offsetHeight this.record[i].height = h } } } }, beforeCreate () {}, created () { this.load = false this.recordInit() }, beforeMount () {}, mounted () { this.initHeightData() this.calculateColumn() this.load = true }, beforeUpdate () {}, updated () { if (this.update) { this.initHeightData() this.calculateColumn() this.update = false this.load = true } }, beforeDestroy () {}, destroyed () {}, watch: { columnNum (curr, old) { this.calculateColumn() }, list (curr, old) { console.log('list change') this.recordInit() this.load = false this.update = true } } } </script> <style scoped> </style>
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
程序员,你怎么对待常见的数据一致性问题?
现象 应用系统中的关键服务绝大部分都会是对数据库的依赖。 当多个进程同时操作同一个数据,会产生资源争抢,数据一致性的问题。 如果只有一个数据库服务器,数据一致性问题也就不存在了。 可是,随着系统访问量、数据量的不断增长,数据库出现多个服务器,又出现缓存服务,又要拆分数据库,还要分拆到不同的子应用等等。 这样一来,数据一致性问题就会变得越来越突出。 举个栗子 我们来看这样一个数据流程。 用户提交一个订单(2个不同商家各一件商品)——数据源头 应用服务器验证用户信息、订单信息、库存信息等等,然后将这个订单发送到订单消息队列——消息队列 订单处理服务器从消息队列中拿到新订单,接下来的处理,可能做的数据操作有: 生成一个订单/也可能会分拆为两个订单 更新两个商品库存数量 更新商家的销售数据 生成订单对应的支付信息 生成用户订单成功的状态信息 思路 上面的数据处理中,涉及到的数据有:订单数据、商品数据、商家数据、支付数据、用户数据。 涉及到的应用和服务有:前端应用系统,消息队列,后端应用系统,数据库,缓存,甚至订单、商品、商家、支付、用户可能都是独立的子应用。 可能大部分系统不会像上面这么庞大。...
- 下一篇
前端黑科技:美团网页首帧优化实践
前言 自JavaScript诞生以来,前端技术发展非常迅速。移动端白屏优化是前端界面体验的一个重要优化方向,Web 前端诞生了 SSR 、CSR、预渲染等技术。在美团支付的前端技术体系里,通过预渲染提升网页首帧优化,从而优化了白屏问题,提升用户体验,并形成了最佳实践。 在前端渲染领域,主要有以下几种方式可供选择: CSR 预渲染 SSR 同构 优点 不依赖数据FP 时间最快客户端用户体验好内存数据共享 不依赖数据FCP 时间比 CSR 快客户端用户体验好内存数据共享 SEO 友好首屏性能高,FMP 比 CSR 和预渲染快 SEO 友好首屏性能高,FMP 比 CSR 和预渲染快客户端用户体验好内存数据共享客户端与服务端代码公用,开发效率高 缺点 SEO 不友好FCP 、FMP 慢 SEO 不友好FMP 慢 客户端数据共享成本高模板维护成本高 Node 容易形成性能瓶颈 通过对比,同构方案集合 CSR 与 SSR 的优点,可以适用于大部分业务场景。但由于在同构的系统架构中,连接前后端的 Node 中间层处于核心链路,系统可用性的瓶颈就依赖于 Node ,一旦作为短板的 Node 挂了,整个...
相关文章
文章评论
共有0条评论来说两句吧...