欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue+uniapp瀑布流布局多種實現(xiàn)方式示例代碼

 更新時間:2023年03月23日 10:46:25   作者:pixle0  
由于使用uniapp開發(fā)的微信小程序不需要考慮響應(yīng)式,因此瀑布流的實現(xiàn)相對于pc端更為簡單,下面這篇文章主要給大家介紹了關(guān)于vue+uniapp瀑布流布局多種實現(xiàn)方式的相關(guān)資料,需要的朋友可以參考下

前言

瀑布流布局是網(wǎng)頁設(shè)計常見的一種布局,一般用于圖片多列展示。列寬固定,圖片根據(jù)自身高度自適應(yīng)交錯排列。

一、實現(xiàn)原理

通過動態(tài)計算哪一列高度最低,就把圖片放置該列下顯示,直至所有圖片分列完畢

計算哪一列高度最低具體實現(xiàn)過程又分2種方式:

方式1:通過計算每一列每張圖片渲染后高度進行累加就是該列的高度,記錄每列累加高度比較大小

方式2:直接通過圖片父級元素高度(列div)來判斷哪一列最低

區(qū)別:方式1無需等待圖片真實渲染完成在比較高度,方式2需要等待圖片真實渲染完成在獲取高度

二、代碼實現(xiàn)

以左右2列為例

<template>
  <div class="page">
    <!-- 左圖片列表 -->
    <div class="left" ref="left">
      <img
        class="img"
        v-for="(item, index) in leftList"
        :key="index"
        :src="item"
      />
    </div>
    <!-- 右圖片列表 -->
    <div class="right" ref="right">
      <img
        class="img"
        v-for="(item, index) in rightList"
        :key="index"
        :src="item"
      />
    </div>
  </div>
</template>
<style scoped>
.page {
  width: 100%;
  display: flex;
  align-items: flex-start;
  padding: 0 1%;
  box-sizing: border-box;
}
.left,
.right {
  margin: 0 auto;
  width: 48%;
}
.img {
  width: 100%;
  height: auto;
  margin-bottom: 10px;
}
</style>

1.方式1(圖片高度累加比較法)

<script>
export default {
  data() {
    return {
      imgList: [
        "https://img0.baidu.com/it/u=1345303087,1528317222&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=1082",
        "https://img2.baidu.com/it/u=1893470775,4143435497&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500",
        "https://img0.baidu.com/it/u=1088754973,1390499664&fm=253&fmt=auto&app=138&f=JPEG?w=335&h=500",
        "https://img1.baidu.com/it/u=3866290852,3566512524&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500",
        "https://img2.baidu.com/it/u=1114729443,1120710416&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500",
        "https://img0.baidu.com/it/u=1345303087,1528317222&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=1082",
        "https://img2.baidu.com/it/u=1893470775,4143435497&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500",
        "https://img2.baidu.com/it/u=1088754973,1390499664&fm=253&fmt=auto&app=138&f=JPEG?w=335&h=500",
      ], //所有圖片
      leftList: [], //左邊列圖片
      rightList: [], //右邊列圖片
      leftHeight: 0, //左邊列高度
      rightHeight: 0, //右邊列高度
      columnWidth: 0, //列寬度
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.columnWidth = this.$refs.left.clientWidth;
      this.setWaterFallLayout();
    });
  },
  methods: {
    //方法1
    async setWaterFallLayout() {
      for (let item of this.imgList) {
        let img = new Image();
        img.src = item;
        try{
          let h = await this.getImgHeight(img);//圖片渲染后高度
          if (this.leftHeight <= this.rightHeight) {//左邊列比右邊低,圖片放入左邊
            this.leftList.push(item);
            this.leftHeight += h;
          } else {//否則,圖片放入右邊
            this.rightList.push(item);
            this.rightHeight += h;
          }
        }catch(e){
          console.log(e)
        }
      }
    },
    //獲取圖片高度
    getImgHeight(img) {
      return new Promise((resolve,reject) => {
      //圖片加載完成
        img.onload = () => {
          let h = (img.height / img.width) * this.columnWidth;//計算圖片渲染后高度
          resolve(h);
        };
        //加載出錯
        img.onerror=()=>{
          reject('error')
        }
      });
    },
  },
};
</script>

2.方式2(父元素高度比較法)

每次放入圖片需要等待渲染后再重新計算父元素高度,關(guān)鍵代碼 await this.$nextTick()

<script>
export default {
  data() {
    return {
      imgList: [
        "https://img0.baidu.com/it/u=1345303087,1528317222&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=1082",
        "https://img2.baidu.com/it/u=1893470775,4143435497&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500",
        "https://img0.baidu.com/it/u=1088754973,1390499664&fm=253&fmt=auto&app=138&f=JPEG?w=335&h=500",
        "https://img1.baidu.com/it/u=3866290852,3566512524&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500",
        "https://img2.baidu.com/it/u=1114729443,1120710416&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500",
        "https://img0.baidu.com/it/u=1345303087,1528317222&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=1082",
        "https://img2.baidu.com/it/u=1893470775,4143435497&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500",
        "https://img2.baidu.com/it/u=1088754973,1390499664&fm=253&fmt=auto&app=138&f=JPEG?w=335&h=500",
      ], //所有圖片
      leftList: [], //左邊列表圖片
      rightList: [], //右邊列表圖片
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.setWaterFallLayout2();
    });
  },
  methods: {
    //方法2
    async setWaterFallLayout2() {
      for (let item of this.imgList) {
        if (this.$refs.left.clientHeight <= this.$refs.right.clientHeight) {//左邊列比右邊低,圖片放入左邊
          this.leftList.push(item);
        } else {//否則圖片放入右邊
          this.rightList.push(item);
        }
        await this.$nextTick();//等待渲染完成后重新比較左右高度
      }
    },
  },
};
</script>

三.uniapp實現(xiàn)

由于uniapp獲取元素高度和vue有所區(qū)別,造成實現(xiàn)瀑布流方式也需要調(diào)整。我們知道uniapp不能通過this.$ref.xx.clientHeight獲取元素高度,而需要通過uni.createSelectorQuery().in(this).select(‘.xxxx’).boundingClientRect().exec()來獲取,且經(jīng)過實測當圖片動態(tài)加入列后通過該api計算出父元素真實高度是不準確的,所以uniapp瀑布流布局實現(xiàn)方式只能通過方法1(也即圖片高度累加法)進行實現(xiàn),除了上面方法1通過img.onload來獲取圖片高度外,uniapp還提供uni.getImageInfo方法來更方便獲取圖片高度。

代碼實現(xiàn)

<template>
	<view class="page">
		<view class="left" ref="left">
			<image class="image" v-for="(item,i) in leftList" :key="i" :src="item" mode="widthFix"></image>
		</view>
		<view class="right" ref="right">
			<image class="image" v-for="(item,i) in rightList" :key="i" :src="item" mode="widthFix"></image>
		</view>
	</view>
</template>
<style lang="scss">
	.page {
		width: 100%;
		display: flex;
		align-items: flex-start;
		padding: 0 1%;
		box-sizing: border-box;
	}

	.left,
	.right {
		margin: 0 auto;
		width: 48%;
	}

	.image {
		width: 100%;
		height: auto;
		margin-bottom: 10px;
	}
</style>
<script>
	export default {
		data() {
			return {
				imageList: [
					"https://img0.baidu.com/it/u=1345303087,1528317222&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=1082",
					"https://img2.baidu.com/it/u=1893470775,4143435497&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500",
					"https://img0.baidu.com/it/u=1088754973,1390499664&fm=253&fmt=auto&app=138&f=JPEG?w=335&h=500",
					"https://img1.baidu.com/it/u=3866290852,3566512524&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500",
					"https://img2.baidu.com/it/u=1114729443,1120710416&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500",
					"https://img0.baidu.com/it/u=1345303087,1528317222&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=1082",
					"https://img2.baidu.com/it/u=1893470775,4143435497&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500",
					"https://img0.baidu.com/it/u=1088754973,1390499664&fm=253&fmt=auto&app=138&f=JPEG?w=335&h=500",
				], //所有圖片
				leftList: [], //左邊列圖片
				rightList: [], //右邊列圖片
				leftHeight: 0, //左邊列高度
				rightHeight: 0, //右邊列高度
				columnWidth: 0 //列寬度
			}
		},
		mounted() {
			this.$nextTick(() => {
				uni.createSelectorQuery().in(this).select('.left').boundingClientRect(res => {
					this.columnWidth = res.width
					//方法1
					this.setWaterFallLayout()
					//方法2
					// this.setWaterFallLayout2()
				}).exec()
			})
		},
		methods: {
			//方法1通過img.onload
			async setWaterFallLayout() {
				for (let item of this.imageList) {
					let img = new Image()
					img.src = item
					try {
						let h = await this.getImgHeight(img)
						if (this.leftHeight <= this.rightHeight) {
							this.leftList.push(item)
							this.leftHeight += h
						} else {
							this.rightList.push(item)
							this.rightHeight += h
						}
					} catch (e) {
						console.log(e)
					}

				}

			},
			//獲取圖片高度
			getImgHeight(img) {
				return new Promise((resolve, reject) => {
					img.onload = () => {
						let h = img.height / img.width * this.columnWidth
						resolve(h)
					}
					//加載出錯
					img.onerror = () => {
						reject('error')
					}
				})
			},
			//方法2通過uni.getImageInfo
			async setWaterFallLayout2() {
				for (let item of this.imageList) {
					uni.getImageInfo({
						src: item,
						success: e => {
							if (this.leftHeight <= this.rightHeight) {
								this.leftList.push(item)
								this.leftHeight += e.height
							} else {
								this.rightList.push(item)
								this.rightHeight += e.height
							}

						}
					})
				}
			}

		},

	}
</script>

四、多列實現(xiàn)

多列實現(xiàn)和2列一樣,動態(tài)生成每列圖片數(shù)據(jù)和記錄每列高度

代碼實現(xiàn)

以最簡單的父元素高度比較法(方式2)為例實現(xiàn),圖片高度累加比較法(方式1)自行類比實現(xiàn)

<template>
  <div class="page">
    <div
      class="column"
      ref="column"
      v-for="(item, index) in columnList"
      :key="index"
    >
      <img class="img" v-for="(n, i) in item" :key="i" :src="n" />
    </div>
  </div>
</template>
<style scoped>
.page {
  width: 100%;
  display: flex;
  align-items: flex-start;
  padding: 0 1%;
  box-sizing: border-box;
}

.column {
  flex: 1;
  padding: 0 10px;
  box-sizing: border-box;
  width: 0;
}

.img {
  width: 100%;
  height: auto;
  margin-bottom: 10px;
}
</style>
<script>
export default {
  data() {
    return {
      imgList: [
        "https://img0.baidu.com/it/u=1345303087,1528317222&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=1082",
					"https://img2.baidu.com/it/u=1893470775,4143435497&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500",
					"https://img0.baidu.com/it/u=1088754973,1390499664&fm=253&fmt=auto&app=138&f=JPEG?w=335&h=500",
					"https://img1.baidu.com/it/u=3866290852,3566512524&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500",
					"https://img2.baidu.com/it/u=1114729443,1120710416&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500",
					"https://img0.baidu.com/it/u=1345303087,1528317222&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=1082",
					"https://img2.baidu.com/it/u=1893470775,4143435497&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500",
					"https://img0.baidu.com/it/u=1088754973,1390499664&fm=253&fmt=auto&app=138&f=JPEG?w=335&h=500",
					"https://img0.baidu.com/it/u=1345303087,1528317222&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=1082",
					"https://img2.baidu.com/it/u=1893470775,4143435497&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500",
      ], //所有圖片
      columnList: [], //分配后的每列圖片
      columWidth: 0, //每列寬度
      columnCount: 5, //顯示幾列
    };
  },
  created() {
    //初始化數(shù)據(jù)
    for (let i = 0; i < this.columnCount; i++) {
      this.columnList.push([]);//生成每列圖片數(shù)組
    }
  },
  mounted() {
    this.$nextTick(()=>{
      this.setWaterFallLayout();
    })
  },
  methods: {
   //瀑布布局
    async setWaterFallLayout() {
      for (let item of this.imgList) {
        let columnHeight = this.$refs.column.map((item) => item.clientHeight); //每列高度數(shù)組
        let min = Math.min(...columnHeight); //找出最小高度值
        let index = columnHeight.findIndex((item) => item === min); //找出最小高度列的索引
        this.columnList[index].push(item);//放入圖片
        await this.$nextTick(); //等待渲染完成后重新比較高度
      }
    },
  },
};
</script>

總結(jié)

到此這篇關(guān)于vue+uniapp瀑布流布局多種實現(xiàn)方式的文章就介紹到這了,更多相關(guān)vue uniapp瀑布流布局實現(xiàn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue實現(xiàn)文件上傳讀取及下載功能

    vue實現(xiàn)文件上傳讀取及下載功能

    這篇文章主要為大家詳細介紹了vue實現(xiàn)文件上傳讀取及下載功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • 詳解Vue中的scoped及穿透方法

    詳解Vue中的scoped及穿透方法

    這篇文章主要介紹了Vue中的scoped及穿透方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-04-04
  • vue3+vite項目H5配置適配步驟詳解

    vue3+vite項目H5配置適配步驟詳解

    這篇文章主要為大家介紹了vue3+vite項目H5配置適配步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-10-10
  • Vue2路由動畫效果的實現(xiàn)代碼

    Vue2路由動畫效果的實現(xiàn)代碼

    本篇文章主要介紹了Vue2路由動畫效果的實現(xiàn)代碼,可以根據(jù)不同的路徑去改變動畫的效果,有興趣的可以了解一下
    2017-07-07
  • web項目開發(fā)中2個Token原因解析及示例代碼

    web項目開發(fā)中2個Token原因解析及示例代碼

    這篇文章主要介紹了web項目開發(fā)中會出現(xiàn)2個Token原因的解析以及實現(xiàn)的示例代碼,有需要的同學可以借鑒參考下,希望可以有所幫助
    2021-09-09
  • vue使用路由守衛(wèi)實現(xiàn)菜單的權(quán)限設(shè)置

    vue使用路由守衛(wèi)實現(xiàn)菜單的權(quán)限設(shè)置

    我們使?vue-element-admin前端框架開發(fā)后臺管理系統(tǒng)時,?般都會涉及到菜單的權(quán)限控制問題,下面這篇文章主要給大家介紹了關(guān)于vue使用路由守衛(wèi)實現(xiàn)菜單的權(quán)限設(shè)置的相關(guān)資料,需要的朋友可以參考下
    2023-06-06
  • Vue解析帶html標簽的字符串為dom的實例

    Vue解析帶html標簽的字符串為dom的實例

    今天小編就為大家分享一篇Vue解析帶html標簽的字符串為dom的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • Vue頁面堆棧管理器詳情

    Vue頁面堆棧管理器詳情

    這篇文章主要介紹了Vue頁面堆棧管理器
    2021-10-10
  • vue項目首次打開時加載速度很慢的優(yōu)化過程

    vue項目首次打開時加載速度很慢的優(yōu)化過程

    這篇文章主要介紹了vue項目首次打開時加載速度很慢的優(yōu)化過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • vue3.0 CLI - 2.3 - 組件 home.vue 中學習指令和綁定

    vue3.0 CLI - 2.3 - 組件 home.vue 中學習指令和綁定

    這篇文章主要介紹了vue3.0 CLI - 2.3 - 組件 home.vue 中學習指令和綁定的相關(guān)知識,本文通過實例代碼相結(jié)合的形式給大家介紹的非常詳細 ,需要的朋友可以參考下
    2018-09-09

最新評論