uniapp 以css方式实现瀑布流

效果图:
在这里插入图片描述
全部代码:

<template>
	<view class="free-panel-title">
		<view class="free-WaterfallFlow">
		  <block>
			<view class="flex-wrap" v-for="(item,index) in imgList" :key="index" v-if="index % 2 != 0">
				<image mode="widthFix" :src="srcurl + item.imgname" :data-src="srcurl + item.imgname" @click="clickimg" ></image>
				<view>评论</view>
				<view>时间</view>
			</view>
		  </block>
		  <block>
			<view class="flex-wrap" v-for="(item,index) in imgList" :key="'2-'+index" v-if="index % 2 == 0">
				<image mode="widthFix" :src="srcurl + item.imgname" :data-src="srcurl + item.imgname" @click="clickimg" ></image>
				<view>评论</view>
				<view>时间</view>
			</view>
		  </block>
		</view>
		<!--返回顶部-->
		<view class="top" :style="{'display':(flag===true? 'block':'none')}">
			<image class="topc" @click="top" src="../../static/top.png" ></image>
		</view>
	</view>
</template>

<script>

	export default {
		data() {
			return {				
				imgList: [],				
				num : 1,
				srcurl:'',
				flag: false
			}
		},
		
		onLoad() {
			let url = uni.getStorageSync('url')  //从缓存读取
			this.srcurl = url + '/imgup/'  //图片的网址,用于页面渲染 :src="srcurl + name"
			this.getInfo('正在加载数据...')	
		},
		
		/* 下拉刷新 */
		onPullDownRefresh() {
			this.num = 1
			this.getInfo('正在加载数据...')
			setTimeout(function () {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		
		/* 上拉触底事件 */
		onReachBottom: function() { 
			if (this.hasMoreData) {
				this.getInfo('加载更多数据')
			} else {
				uni.showToast({
				  title: '没有更多数据',
				  icon:'none'
				})
			}      
		},
		
		methods: {

			// 分页加载数据
			getInfo: function (message) {
				var that = this
				var url = uni.getStorageSync('url') //从缓存读取
				var userid = uni.getStorageSync('userid') //从缓存读取
				uni.showLoading({	//显示 loading 提示框
				  title: message,
				})
				uni.request({
					url: url + '/waterfall',
					data: {					
						pagenum : that.num ,   //赋值并传到后端
						userid : userid
					},
					method: 'GET',
					header: { 'content-type': 'application/json' },
					success: function (res) {  //请求成功
						var imgurlTemp = that.imgList
						var imgurl = res.data
						console.log(imgurl)

						if (imgurl.length >= 0) {							
							if (that.num == 1) {
								imgurlTemp = []
							}
							if (imgurl.length < 10) {  //对应后端接口分页查询的记录条数  
								that.imgList = imgurlTemp.concat(imgurl)
								that.hasMoreData = false
							} else {
								that.imgList = imgurlTemp.concat(imgurl)
								that.hasMoreData = true
								that.num = that.num + 1		    
							}
							setTimeout(function() {
								uni.hideLoading()		//隐藏 loading 提示框
							}, 1000)
						}
					}
				})
			},
			
			// 图片预览
			clickimg(event) {
				var imgurl = event.currentTarget.dataset.src
				var currentUrl = event.currentTarget.dataset.src   //获取点击图片的地址, **对应<template>里面的 :data-src="item.src"					
				uni.previewImage({       
					urls: [imgurl],    //这里是单图 . 需要预览的全部图片地址,这个数组是必须的,要用[] 					
					current: currentUrl, //当前显示图片的地址					
				})  
			},
			
			//回到顶部
			top() { 
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 300
				});
			},
			onPageScroll(e) { //根据距离顶部距离是否显示回到顶部按钮
				if(e.scrollTop>600){ //当距离大于600时显示回到顶部按钮
					this.flag=true
				}else{ //当距离小于600时隐藏回到顶部按钮
					this.flag=false
				}
			}

		}
	}
</script>

<style>
	.free-WaterfallFlow{
		width:96%;
		column-count:2; /* 分隔的列数 */
	}
	.free-WaterfallFlow .flex-wrap{
		display: inline-block;
		width:98%;
		margin-left:3%;
		margin-bottom:3%;		
		padding:2%;
		padding-top:5%;	
		border:0px solid #cc22b0; /* 边框 */
		box-shadow: 0 2px 2px rgba(34, 25, 25, 0.4); /* 框阴影 */
		text-align: center; /* 框内元素居中对齐 */
	}
	.flex-wrap image{
		width:95%;
		margin:0 auto;
	}
	.flex-wrap view:nth-child(2){
		font-size:15px;
		padding:2% 0;
		color:#717171;
	}
	.flex-wrap view:nth-child(3){
		font-size:13px;
		padding:2% 0;
		color:#aaa;
		text-align: right;
	}
	
	/* 回到顶部 */
	.top {
		position: relative;
		display: none; /* 先将元素隐藏 */
	} 
	.topc {
		height: 30px; 
		width: 30px;
		position: fixed;
		right: 0;
		top: 70%;
	}
</style>

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
瀑布流布局是一种常见的网页设计布局,通常用于多列展示图片。该布局的特点是列宽固定,而图片根据自身高度自适应并交错排列。在uniapp实现瀑布流式页面布局有多种方法。其中一种常用的方法是使用flex布局。可以使用flex布局来实现多列的瀑布流效果,具体实现方法如下: 1. 首先,需要在自己的uniapp项目目录下创建一个组件waterfall.vue。 2. 在waterfall.vue组件中,可以使用flex布局来实现瀑布流效果。可以将图片按照固定的列数分成多个列,然后使用flex属性来自动调整图片的宽度和高度,使其自适应并交错排列。 3. 在代码实现方面,可以采用两种方式实现瀑布流布局。一种是通过累加比较图片的高度来确定每一列的位置,另一种是通过比较父元素的高度来确定每一列的位置。 在uniapp实现瀑布流式页面布局可以参考以上的代码实现方法,根据具体需求选择合适的方式实现多列的瀑布流效果。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [vue+uniapp瀑布流布局多种实现方式](https://blog.csdn.net/sd1sd2/article/details/128599997)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [uni-app项目瀑布流布局的实现](https://download.csdn.net/download/weixin_38642864/13680529)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值