Vue瀑布流插件的使用示例
我自己寫的一個(gè)的Vue瀑布流插件,列數(shù)自適應(yīng),不用設(shè)置每個(gè)卡片的高度。
測(cè)試頁面:Page.vue
模板頁面:WaterFollow.vue 和 WFColumn.vue
在Page.vue中,修改itemW的值,設(shè)置每列的最小寬度。例如:itemW="200"(意為200px)。list為數(shù)組。高度不用設(shè)置,:style="{height:item+'px'}"是我為了創(chuàng)造卡片高度加上去的,不加就顯示卡片的原來大小。
經(jīng)測(cè)試,created加載數(shù)據(jù)正常,mounted加載、更新數(shù)據(jù)正常。
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> </water-follow> </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 () { // 有初始數(shù)據(jù) for (let i = 0; i < 50; i++) { this.list.push(Math.floor(Math.random() * 301 + 200)) } }, mounted () { // 模擬網(wǎng)絡(luò)請(qǐng)求 // window.setTimeout(() => { // this.list = [] // for (let i = 0; i < 50; i++) { // this.list.push(Math.floor(Math.random() * 301 + 200)) // } // }, 1000) // -- 分割 -- // 模擬數(shù)據(jù)不斷變化 // 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>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue3?element?plus?table?selection展示數(shù)據(jù),默認(rèn)選中功能方式
這篇文章主要介紹了vue3?element?plus?table?selection展示數(shù)據(jù),默認(rèn)選中功能方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07Vue中實(shí)現(xiàn)路由跳轉(zhuǎn)傳參的4種方式
本文詳盡的講了在Vue項(xiàng)目中,如何實(shí)現(xiàn)路由跳轉(zhuǎn)傳參的4四種方式(2大路由跳轉(zhuǎn)方式,每種方式包括4種路由傳參實(shí)現(xiàn)形式),以及每種方式中實(shí)現(xiàn)路由跳轉(zhuǎn)包括路由傳參的方法的各種寫法,需要的朋友可以參考下2024-04-04解決Vue路由導(dǎo)航報(bào)錯(cuò):NavigationDuplicated:?Avoided?redundant?navig
這篇文章主要給大家介紹了關(guān)于解決Vue路由導(dǎo)航報(bào)錯(cuò):NavigationDuplicated:?Avoided?redundant?navigation?to?current?location的相關(guān)資料,這是最近做項(xiàng)目時(shí)候遇到的一個(gè)問題,現(xiàn)將解決辦法分享出來,需要的朋友可以參考下2023-01-01vue自定義組件(通過Vue.use()來使用)即install的用法說明
這篇文章主要介紹了vue自定義組件(通過Vue.use()來使用)即install的用法說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08解決Vue中引入swiper,在數(shù)據(jù)渲染的時(shí)候,發(fā)生不滑動(dòng)的問題
今天小編就為大家分享一篇解決Vue中引入swiper,在數(shù)據(jù)渲染的時(shí)候,發(fā)生不滑動(dòng)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09vue-element-admin中node-sass換成dart-sass,安裝依賴報(bào)code?128多種問題的解決
這篇文章主要介紹了vue-element-admin中node-sass換成dart-sass,安裝依賴報(bào)code?128多種問題的解決方法,本文給大家分享問題原因分析及解決方法,需要的朋友可以參考下2023-02-02關(guān)于Nuxt的五種渲染模式的差異和使用場(chǎng)景全解析
這篇文章主要介紹了關(guān)于Nuxt的五種渲染模式的差異和使用場(chǎng)景全解析,在過去傳統(tǒng)開發(fā)中,頁面渲染任務(wù)是由服務(wù)端完成的,那么Nuxt是如何渲染的呢,需要的朋友可以參考下2023-04-04webpack dev-server代理websocket問題
這篇文章主要介紹了webpack dev-server代理websocket問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08