uniapp实现瀑布流

<template>
	<view class="container">
		<view class="left" ref="leftColumn">
			<view class="posive" v-for="(item, index) in leftList" :key="index">
				<image :src="item.src" alt="" mode="widthFix" @click="previewImg(item)" />
			</view>
		</view>
		<view class="right" ref="rightColumn">
			<view class="posive" v-for="(item, index) in rightList" :key="index">
				<image :src="item.src" alt="" mode="widthFix" @click="previewImg(item)" />
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 图片列表
				list: [{
						src: "https://inews.gtimg.com/om_bt/OjPq2cnMN_-ivDKjxpCZ2kk_ab8YC5VMnL-0pQ21fUvd4AA/1000"
					},
					{
						src: "https://inews.gtimg.com/om_bt/OjPq2cnMN_-ivDKjxpCZ2kk_ab8YC5VMnL-0pQ21fUvd4AA/1000"
					},
					{
						src: "https://inews.gtimg.com/om_bt/OjPq2cnMN_-ivDKjxpCZ2kk_ab8YC5VMnL-0pQ21fUvd4AA/1000"
					},
					{
						src: "https://inews.gtimg.com/om_bt/OjPq2cnMN_-ivDKjxpCZ2kk_ab8YC5VMnL-0pQ21fUvd4AA/1000"
					},
					{
						src: "https://inews.gtimg.com/om_bt/OjPq2cnMN_-ivDKjxpCZ2kk_ab8YC5VMnL-0pQ21fUvd4AA/1000"
					},
					{
						src: "https://inews.gtimg.com/om_bt/OjPq2cnMN_-ivDKjxpCZ2kk_ab8YC5VMnL-0pQ21fUvd4AA/1000"
					},
					{
						src: "https://inews.gtimg.com/om_bt/OjPq2cnMN_-ivDKjxpCZ2kk_ab8YC5VMnL-0pQ21fUvd4AA/1000"
					},
					{
						src: "https://inews.gtimg.com/om_bt/OjPq2cnMN_-ivDKjxpCZ2kk_ab8YC5VMnL-0pQ21fUvd4AA/1000"
					},
					{
						src: "https://inews.gtimg.com/om_bt/OjPq2cnMN_-ivDKjxpCZ2kk_ab8YC5VMnL-0pQ21fUvd4AA/1000"
					},
					{
						src: "https://inews.gtimg.com/om_bt/OjPq2cnMN_-ivDKjxpCZ2kk_ab8YC5VMnL-0pQ21fUvd4AA/1000"
					},



					// 添加更多图片对象
				],
				// 初始化左右盒子
				leftList: [],
				rightList: [],
				// 初始化左右盒子高度
				leftH: 0,
				rightH: 0
			};
		},
		onLoad() {
			this.doList();
		},
		methods: {
			doList() {
				const that = this;
				this.list.forEach(res => {
					uni.getImageInfo({
						src: res.src,
						success: image => {
							let showH = (50 * image.height) / image.width;
							if (that.leftH <= that.rightH) {
								that.leftList.push({
									...res,
									height: showH
								});
								that.leftH += showH;
							} else {
								that.rightList.push({
									...res,
									height: showH
								});
								that.rightH += showH;
							}
						}
					});
				});
			}
		}
	};
</script>

<style>
	.container {
		padding: 0 24rpx;
		font-size: 14rpx;
		line-height: 24rpx;
		display: flex;
		justify-content: space-between;

		.right,
		.left {
			display: inline-block;
			width: 340rpx;
			vertical-align: top;
		}

		.left image,
		.right image {
			border-radius: 16rpx;
			width: 100%;
			margin-bottom: 24rpx;
		}
	}
</style>

效果

相关推荐

  1. uniapp实现瀑布

    2024-03-20 05:12:08       48 阅读
  2. uniapp 封装瀑布组件

    2024-03-20 05:12:08       26 阅读
  3. CSS实现瀑布

    2024-03-20 05:12:08       69 阅读
  4. jQuery实现瀑布

    2024-03-20 05:12:08       36 阅读
  5. css实现瀑布布局

    2024-03-20 05:12:08       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-20 05:12:08       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-20 05:12:08       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-20 05:12:08       82 阅读
  4. Python语言-面向对象

    2024-03-20 05:12:08       91 阅读

热门阅读

  1. FFmpeg-- c++实现:音频流aac和视频流h264封装

    2024-03-20 05:12:08       42 阅读
  2. 预防GPT-3和其他复杂语言模型中的“幻觉”

    2024-03-20 05:12:08       40 阅读
  3. [python] ETL 工作流程 Prefect

    2024-03-20 05:12:08       61 阅读
  4. 计算机网络1.1网络定义及发展

    2024-03-20 05:12:08       32 阅读
  5. 12350安全生产举报热线系统解决方案

    2024-03-20 05:12:08       35 阅读
  6. 字节-安全研究实习生(二面)

    2024-03-20 05:12:08       31 阅读
  7. 掌握C#中的GUI多线程技巧:WinForms和WPF实例详解

    2024-03-20 05:12:08       37 阅读
  8. OpenAI的ChatGPT应对大学会计考试

    2024-03-20 05:12:08       48 阅读