国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Vue瀑布流插件的使用示例

2019-11-19 12:54:28
字體:
來源:轉載
供稿:網友

我自己寫的一個的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>    </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 () {  // 有初始數據  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>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 孝感市| 辉南县| 攀枝花市| 南阳市| 乐清市| 读书| 边坝县| 曲麻莱县| 阳东县| 肃南| 汾西县| 桦甸市| 凤台县| 嘉鱼县| 民县| 商水县| 砀山县| 阳原县| 长武县| 富源县| 新野县| 保定市| 成都市| 随州市| 通道| 凤台县| 砀山县| 太原市| 福海县| 信宜市| 红原县| 金堂县| 淮南市| 哈尔滨市| 华容县| 交城县| 东光县| 鄂伦春自治旗| 石嘴山市| 虞城县| 满洲里市|