uniapp使用Vue的Flex布局

Vue Flex布局

技术概述:该技术主要用于在开发网页、app、小程序时适配不同的屏幕大小。因为我一开始没有用Flex布局,大小宽度写死,导致在真机测试时屏幕内容就偏移了原本的位置。该技术的难点在于测量组件的大小,估算出百分比。

技术详述:

就拿我们小组的app中的个人中心页面来说
微信图片 20240603144733
从上到下,可以将页面切割成一个一个的长方体view,说明整体看属于垂直布局,flex-direction=vertical,对于第一个矩形框,有两个按钮,是水平布局的,且水平位于长方形框的两侧,则可以设置justify-content为space-between。其中justify-content是主轴的方向,align-items是垂直于主轴的轴的方向。主轴就是flex-direction选择的方向,如图中vertical就是从上往下。以下是静态布局的代码:

<template>
	<view class="content">
		<view class="person-inform">
			<view class="" style="width: 100% ;height:14%;"></view>
			<view class="setter-area" style="width:100%;height: 10%; display: flex;flex-direction:row;justify-content: space-between;align-items: center;">
				<view class="modify-btn" @click="modifyInform" style="display: flex;align-items: center;justify-content: center;">
					编辑资料
				</view>
				<view class="setter" @tap="startSetter" style="width: 15%;height: 100%; display: flex;justify-content: center;align-items: center;">
					<image style="width: 50rpx; height: 50rpx;" src="../../static/setter.png"></image>
				</view>
			</view>
			<view class="" style="width: 100%;height: 31%;"></view>
			<view class="base-inform">
				<view class="" style="width: 30rpx;"></view>
				<view class="head-picture" style="width: 80px;height: 80px;border-radius: 50%;display: flex;align-items: center;justify-content: center;">
					<image style="width: 80px;height: 80px;border-radius: 50%;" :src="headPictureOrDefault" mode=""></image>
				</view>
				<view class="" style="width: 3%;height: 100%;"></view>
				<view class="simpleInform">
					<view class="nickname-address">
						<view class="nickname">
							<view class="">
								{{userInfo.nickname}}
							</view>
							<view class="gender">
								<image style="width: 40rpx; height: 40rpx;" :src="genderImage" mode=""></image>
							</view>
						</view>
						<view class="address">
							ip属地:{{userInfo.address}}
						</view>
					</view>
	
				</view>
			</view>
			<view class="concern-fan">
				<view class="" style="width: 5%;"></view>
				<view @click="onClickFocus" style="width: auto;font-size: 1.5em;font-weight: bold;">{{socialInfo.followsNum}}</view> 
				<view class="" style="margin-left: 20rpx;">
					<view class="" style="width:100% ;height: 90%;" @click="onClickFocus">
						关注
					</view>
				</view> 
				<view @click="onClickFans" style="width: auto;font-size: 1.5em;font-weight: bold;margin-left: 20rpx;" >{{socialInfo.fansNum}}</view>
				<view class="" style="margin-left: 20rpx;">
					<view class="" style="width: 100%;height: 90%;" @click="onClickFans">
						粉丝
					</view>
				</view>
			</view>
			<view class="signature" style="padding-left: 20rpx;">
				个性签名:<text>{{userInfo.signature}}</text>
			</view>
			<view class="state-row" style="width: 100%;height: 10%;display: flex;flex-direction: row;background-color: white;align-items: flex-start;">
				<view class="state" style="margin-left: 15rpx; ">
					<image style="width:40rpx;height: 40rpx;" src="../../static/dongtai.png" mode=""></image>
					<text style="font-weight: bold;">动态</text>
				</view>
			</view>
		</view>
		<view class="" style="width: 100%;height:1%;background-color: white"></view>
		<view class="person-history">
			<scroll-view v-if="totalPost.length>0" class="scroll-area" style="width: 100%;height: 100%;" scroll-y="true" show-scrollbar="false">
			    <view class="" v-for="(item,index) in totalPost" :key="index">
			    	<personalupdate :postContent="item.content" :headPicture="userInfo.avatarUrl" :postDetail="item" :nickName="userInfo.nickname" @deletePostSuccess="removePost"></personalupdate>
			    </view>
			</scroll-view>
			<view class="" v-if="totalPost.length==0" style="width: 100%;height: 100%;display: flex;flex-direction: column;justify-content: center;align-items: center;">
				<image src="../../static/null.png" style="width: 400rpx;height: 200rpx;" mode="widthFix"></image>
				<view class="" style="font: 30rpx;color:black">
				   快发帖让更多人了解你吧
				</view>
			</view>
		</view>
	</view>
</template>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		background-color: #fef157;
		width: 100vw;
        height: 100vh;	
	}
	.content .person-inform{
		display: flex;
		flex-direction: column;
		width: 100%;
		height: 45%;
		background:#fef157;
	}
   
	
	.content .person-inform .setter-area{
		width: 100%;
		height: 10%;
	}
	 .content .person-inform .setter{
		width: 10%;
	}
	.content .person-inform .modify-btn{
		width:20%;
		height: 75%;
		margin-left:10rpx;
		border:1px solid black ;
		background-color: black;
		color: white;
		border-radius: 20%;
		display: flex;
		justify-items: center;
		align-items: center;
		font-weight: bold;
		font-size:30rpx;
	}
	.content .person-inform .base-inform{
		width: 100%;
		height: 40%;
		display: flex;
		flex-direction: row;
	}
	.content .person-inform .base-inform .head-picture{
	
	}
	.content .person-inform .base-inform .simpleInform{
		width: auto;
		height: 100%;
		display: flex;
		flex-direction: row;
		align-items: center;
	}
	.content .person-inform .base-inform .simpleInform .nickname-address{
		display: flex;
		flex-direction: column;
		align-items: flex-start;
	    width: auto;
		position: relative;
	}
	.content .person-inform .base-inform .simpleInform .nickname-address .nickname{
		font-size: 1.5em;
		color: #999;
		font-weight: bold;
		display: flex;
	}
	.content .person-inform .base-inform .simpleInform .nickname-address .address{
		font-size: 13px;
		color: #999;
	}
	.content .person-inform .base-inform .simpleInform .nickname-address .gender{
		display: flex;
		align-items: end;
	}
	.content .person-inform .concern-fan{
		width: 100%;
		height:10% ;
		background-color: aqua;
		display: flex;
		flex-direction:row;
		font-size: 1em;
		background-color: white;
		border-radius: 50rpx 50rpx 0 0;
		flex-direction: row;
	}
	.content .person-inform .concern-fan text{
		width: 5%;
		font-size: 1.5em;
		font-weight: bold;
		text-align: center;
	}
	.content .person-inform .concern-fan view{
		width: 10%;
		height: 100%;
		display: flex;
		justify-content: center;
		align-items: center;
	}
	.content .person-inform .signature{
		width: 100%;
		height: 8%;
		background: white;
		font-size: 0.8em;
		color: gray;
		display: flex;
		justify-content: flex-start;
	}
	.content .person-inform .state-row .state{
		
		background: white;
		height: 95%;
		width: 20%;
		border-bottom: 2px solid black;
	}
	.content .person-history{
		background-color: white;
		width: 100%;
		height: 54%;
		display: flex;
		align-items: center;
		flex-direction: column;
	}
	
</style>

在编写过程中,我遇到如果一个view里面包裹一个image,应该如何让image处于view的中心的问题,之前用的是让view使用垂直布局,像三明治一样,在里面加两个view,让image夹在中间。后面发现,其实只要让view处于flex布局,再让justify-content和align-items都为center即可

总结:flex布局可以使页面具有一定的灵活性,是个非常重要的布局知识

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
前端使用uniapp相册时,可以通过响应式布局来确保在不同设备上的良好显示效果。下面是一个示例代码,展示了如何使用uniapp和响应式布局来实现相册的布局: ```vue <template> <view class="gallery"> <view class="gallery-item" v-for="(image, index) in images" :key="index"> <image :src="image.url" :mode="mode"></image> </view> </view> </template> <script> export default { data() { return { images: [ { url: 'path/to/image1.jpg' }, { url: 'path/to/image2.jpg' }, { url: 'path/to/image3.jpg' }, // 添加更多图片对象 ], mode: 'aspectFill' // 图片裁剪模式,可以根据需要调整 } } } </script> <style> .gallery { display: flex; flex-wrap: wrap; justify-content: space-between; } .gallery-item { width: calc(33.33% - 10px); /* 每行显示3个图片,可根据需要调整 */ margin-bottom: 10px; } .image { width: 100%; } </style> ``` 上述代码中,使用了`flex`布局来实现相册的响应式布局。通过设置`.gallery`为`display:flex`,`.gallery-item`为`width:calc(33.33% - 10px)`,可以将每行显示3个图片,并在它们之间添加一定的间距。你可以根据需要调整这些样式来适应你的项目需求。 每个图片使用`<image>`标签来显示,通过`:src`绑定图片的URL,`:mode`属性可以设置图片的裁剪模式,根据需要进行调整。 这是一个简单的示例,你可以根据自己的实际需求进行进一步的样式和布局调整。希望对你有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值