uniapp vue flex 布局快速上手

318 阅读3分钟

vue flex 布局快速上手

* 无flex代码示例

image.png

完整代码

<template>
	<view>
		<view class="box">
			<view class="box-item">1</view>
			<view class="box-item">2</view>
			<view class="box-item">3</view>
			<view class="box-item2">4</view>
			<view class="box-item2">5</view>
			<view class="box-item2">6</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				
			}
		},
		methods: {
			
		}
	}
</script>

<style>
	.box{
		height: 500upx;
		width: 100%;
		border: 5upx solid #cccccc;
		background-color: white;
	}
	.box-item {
		color: white;
		height: 130upx;
		width: 130upx;
		line-height: 130upx;
		font-size: 30upx;
		font-weight: bold;
		text-align: center;
	}
	.box-item2 {
		color: white;
		height: 260upx;
		width: 260upx;
		line-height: 260upx;
		font-size: 30upx;
		font-weight: bold;
		text-align: center;
	}
	
	.box-item:nth-of-type(odd){
		background: green;
	}
	.box-item:nth-of-type(even){
		background: blue;
	}
	
	.box-item2:nth-of-type(odd){
		background: purple;
	}
	.box-item2:nth-of-type(even){
		background: orange;
	}

</style>


* box 加 display: flex; 效果

注意:这个时候 所有的item 会填充到一行。box-item 的height没变 ,width 按比例压缩,

.box{
		height: 500upx;
		width: 100%;
		border: 5upx solid #cccccc;
		background-color: white;
		display: flex;
	}

image.png

完整代码如下

<template>
	<view>
		<view class="box">
			<view class="box-item">1</view>
			<view class="box-item">2</view>
			<view class="box-item">3</view>
			<view class="box-item2">4</view>
			<view class="box-item2">5</view>
			<view class="box-item2">6</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				
			}
		},
		methods: {
			
		}
	}
</script>

<style>
	.box{
		height: 500upx;
		width: 100%;
		border: 5upx solid #cccccc;
		background-color: white;
		display: flex;
	}
	.box-item {
		color: white;
		height: 130upx;
		width: 130upx;
		line-height: 130upx;
		font-size: 30upx;
		font-weight: bold;
		text-align: center;
	}
	.box-item2 {
		color: white;
		height: 260upx;
		width: 260upx;
		line-height: 260upx;
		font-size: 30upx;
		font-weight: bold;
		text-align: center;
	}
	
	.box-item:nth-of-type(odd){
		background: green;
	}
	.box-item:nth-of-type(even){
		background: blue;
	}
	
	.box-item2:nth-of-type(odd){
		background: purple;
	}
	.box-item2:nth-of-type(even){
		background: orange;
	}

</style>


* flex-wrap: wrap; 换行

image.png

主要代码

.box{
		height: 500upx;
		width: 100%;
		border: 5upx solid #cccccc;
		background-color: white;
		display: flex;
		flex-wrap: wrap;
	}

完整代码

<template>
	<view>
		<view class="box">
			<view class="box-item">1</view>
			<view class="box-item">2</view>
			<view class="box-item">3</view>
			<view class="box-item2">4</view>
			<view class="box-item2">5</view>
			<view class="box-item2">6</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				
			}
		},
		methods: {
			
		}
	}
</script>

<style>
	.box{
		height: 500upx;
		width: 100%;
		border: 5upx solid #cccccc;
		background-color: white;
		display: flex;
		flex-wrap: wrap;
	}
	.box-item {
		color: white;
		height: 130upx;
		width: 130upx;
		line-height: 130upx;
		font-size: 30upx;
		font-weight: bold;
		text-align: center;
	}
	.box-item2 {
		color: white;
		height: 260upx;
		width: 260upx;
		line-height: 260upx;
		font-size: 30upx;
		font-weight: bold;
		text-align: center;
	}
	
	.box-item:nth-of-type(odd){
		background: green;
	}
	.box-item:nth-of-type(even){
		background: blue;
	}
	
	.box-item2:nth-of-type(odd){
		background: purple;
	}
	.box-item2:nth-of-type(even){
		background: orange;
	}

</style>

* flex-wrap: wrap-reverse; 反向换行

.box{
		height: 500upx;
		width: 100%;
		border: 5upx solid #cccccc;
		background-color: white;
		display: flex;
		flex-wrap: wrap-reverse;
	}

image.png

*justify-content: center; 水平方向居中

image.png

示例代码

.box{
		height: 500upx;
		width: 100%;
		border: 5upx solid #cccccc;
		background-color: white;
		display: flex;
		/* flex-wrap: wrap-reverse; */
		justify-content: center;
	}

*justify-content: space-between; 水平方向两段对齐

image.png

示例代码

.box{
		height: 500upx;
		width: 100%;
		border: 5upx solid #cccccc;
		background-color: white;
		display: flex;
		/* flex-wrap: wrap-reverse; */
		justify-content: space-between;
	}

* justify-content: flex-start; 水平方向靠左 (默认值)

image.png 示例代码

.box{
		height: 500upx;
		width: 100%;
		border: 5upx solid #cccccc;
		background-color: white;
		display: flex;
		/* flex-wrap: wrap-reverse; */
		justify-content: flex-start;
	}

*justify-content: flex-end; 水平方向靠右

image.png

* display: flex; align-items: center;垂直居中

image.png

示例代码

.box{
		height: 500upx;
		width: 100%;
		border: 5upx solid #cccccc;
		background-color: white;
		line-height: 500upx;
		 display: flex; 
		 align-items: center;
		/* flex-wrap: wrap; */
		/* justify-content: flex-start; */
	}

* display: flex; align-items: stretch; 水平方向拉伸

要去掉 box-item 里的height

image.png 主要代码

.box{
		height: 500upx;
		width: 100%;
		border: 5upx solid #cccccc;
		background-color: white;
		line-height: 500upx;
		display: flex; 
		align-items: stretch;
		/* flex-wrap: wrap; */
		/* justify-content: flex-start; */
	}
	.box-item {
		color: white;
		/* height: 130upx; */
		width: 130upx;
		/* line-height: 130upx; */
		font-size: 30upx;
		font-weight: bold;
		text-align: center;
	}

完整代码

<template>
	<view>
		<view class="box">
			<view class="box-item">1</view>
			<view class="box-item">2</view>
			<!-- <view class="box-item">3</view>
			<view class="box-item2">4</view>
			<view class="box-item2">5</view>
			<view class="box-item2">6</view> -->
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				
			}
		},
		methods: {
			
		}
	}
</script>

<style>
	.box{
		height: 500upx;
		width: 100%;
		border: 5upx solid #cccccc;
		background-color: white;
		line-height: 500upx;
		display: flex; 
		align-items: stretch;
		/* flex-wrap: wrap; */
		/* justify-content: flex-start; */
	}
	.box-item {
		color: white;
		/* height: 130upx; */
		width: 130upx;
		/* line-height: 130upx; */
		font-size: 30upx;
		font-weight: bold;
		text-align: center;
	}
	
	.box-item:nth-of-type(odd){
		background: green;
	}
	.box-item:nth-of-type(even){
		background: blue;
	}

</style>

* 如果想让子元素内的字水平垂直居中,则只需要在子元素设置line-height:子元素高度

image.png

示例代码

.box-item {
		color: white;
		height: 130upx;
		width: 130upx;
		font-size: 30upx;
		font-weight: bold;
		
		line-height: 130upx;
		display: flex;
		justify-content: center;
		text-align: center;
	}

页面代码

<template>
	<view>
		<view class="box">
			<view class="box-item">
				1
			</view>
			<view class="box-item">2</view>
			<!-- <view class="box-item">3</view>
			<view class="box-item2">4</view>
			<view class="box-item2">5</view>
			<view class="box-item2">6</view> -->
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {

			}
		},
		methods: {

		}
	}
</script>

<style>
	.box {
		height: 500upx;
		width: 100%;
		border: 5upx solid #cccccc;
		background-color: white;
		display: flex;
	}

	.box-item {
		color: white;
		height: 130upx;
		width: 130upx;
		font-size: 30upx;
		font-weight: bold;
		
		line-height: 130upx;
		display: flex;
		justify-content: center;
		text-align: center;
	}

	.inter-item {
		display: flex;
		justify-content: center;
		text-align: center;
		color: white;
		width: 100%;
		height: 100%;
		background-color: red;

		font-size: 30upx;
		font-weight: bold;
	}

	.box-item:nth-of-type(odd) {
		background: green;
	}

	.box-item:nth-of-type(even) {
		background: blue;
	}
</style>
* display: flex; align-items: flex-end; 垂直方向靠下

image.png

主要代码

.box {
		height: 500upx;
		width: 100%;
		border: 5upx solid #cccccc;
		background-color: white;
		display: flex;
		align-items: flex-end;
	}
* 设置flex-direction: column; 设置flex 方向

image.png

页面代码

<template>
	<view>
		<view class="box">
			<view class="box-item">
				1
			</view>
			<view class="box-item">2</view>
			<!-- <view class="box-item">3</view>
			<view class="box-item2">4</view>
			<view class="box-item2">5</view>
			<view class="box-item2">6</view> -->
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {

			}
		},
		methods: {

		}
	}
</script>

<style>å
	.box {
		height: 500upx;
		width: 100%;
		border: 5upx solid #cccccc;
		background-color: white;
		display: flex;
		flex-direction: column;
		/* align-items: center;
		justify-content: center; */
	}

	.box-item {
		color: white;
		height: 130upx;
		width: 130upx;
		font-size: 30upx;
		font-weight: bold;
		line-height: 130upx;
	}

	.box-item:nth-of-type(odd) {
		background: green;
	}

	.box-item:nth-of-type(even) {
		background: blue;
	}
</style>

* 设置flex-direction: column; 设置flex 方向,从上到下居中

image.png

主要代码

flex-direction: column;
		align-items: center;
		justify-content: center;

页面代码

<template>
	<view>
		<view class="box">
			<view class="box-item">
				1
			</view>
			<view class="box-item">2</view>
			<!-- <view class="box-item">3</view>
			<view class="box-item2">4</view>
			<view class="box-item2">5</view>
			<view class="box-item2">6</view> -->
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {

			}
		},
		methods: {

		}
	}
</script>

<style>
	.box {
		height: 500upx;
		width: 100%;
		border: 5upx solid #cccccc;
		background-color: white;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.box-item {
		color: white;
		height: 130upx;
		width: 130upx;
		font-size: 30upx;
		font-weight: bold;
		line-height: 130upx;
	}

	.box-item:nth-of-type(odd) {
		background: green;
	}

	.box-item:nth-of-type(even) {
		background: blue;
	}
</style>
* 垂直方向居中对齐 display: flex;flex-direction: column;justify-content: center;

image.png 主要代码

.box {
		height: 500upx;
		width: 100%;
		border: 5upx solid #cccccc;
		background-color: white;
		display: flex;
		flex-direction: column;
		/* align-items: center; */
		justify-content: center;
	}

页面代码

<template>
	<view>
		<view class="box">
			<view class="box-item">
				1
			</view>
			<view class="box-item">2</view>
			<!-- <view class="box-item">3</view>
			<view class="box-item2">4</view>
			<view class="box-item2">5</view>
			<view class="box-item2">6</view> -->
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {

			}
		},
		methods: {

		}
	}
</script>

<style>
	.box {
		height: 500upx;
		width: 100%;
		border: 5upx solid #cccccc;
		background-color: white;
		display: flex;
		flex-direction: column;
		/* align-items: center; */
		justify-content: center;
	}

	.box-item {
		color: white;
		height: 130upx;
		width: 130upx;
		font-size: 30upx;
		font-weight: bold;
		line-height: 130upx;
	}

	.box-item:nth-of-type(odd) {
		background: green;
	}

	.box-item:nth-of-type(even) {
		background: blue;
	}
</style>
* flex-shrink: 0; 不被压缩

image.png

.box-item:first-of-type{
		flex-shrink: 0;
	}

页面代码

<template>
	<view>
		<view class="box">
			<view class="box-item">1</view>
			<view class="box-item">2</view>
			<view class="box-item">3</view>
			<view class="box-item2">4</view>
			<view class="box-item2">5</view>
			<view class="box-item2">6</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				
			}
		},
		methods: {
			
		}
	}
</script>

<style>
	.box{
		height: 500upx;
		width: 100%;
		border: 5upx solid #cccccc;
		background-color: white;
		line-height: 500upx;
		display: flex; 
	}
	.box-item {
		color: white;
		height: 130upx;
		width: 130upx;
		line-height: 130upx;
		font-size: 30upx;
		font-weight: bold;
		text-align: center;
	}
	.box-item2 {
		color: white;
		height: 260upx;
		width: 260upx;
		line-height: 260upx;
		font-size: 30upx;
		font-weight: bold;
		text-align: center;
	}
	.box-item:first-of-type{
		flex-shrink: 0;
	}
	.box-item:nth-of-type(odd){
		background: green;
	}
	.box-item:nth-of-type(even){
		background: blue;
	}
	
	.box-item2:nth-of-type(odd){
		background: purple;
	}
	.box-item2:nth-of-type(even){
		background: orange;
	}

</style>

* flex: 1; 平均 占比

image.png

主要代码

.box-item {
		color: white;
		height: 130upx;
		/* width: 130upx; */
		flex: 1;
		line-height: 130upx;
		font-size: 30upx;
		font-weight: bold;
		text-align: center;
	}

页面代码

<template>
	<view>
		<view class="box">
			<view class="box-item">1</view>
			<view class="box-item">2</view>
			<view class="box-item">3</view>
			<!-- <view class="box-item2">4</view>
			<view class="box-item2">5</view>
			<view class="box-item2">6</view> -->
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				
			}
		},
		methods: {
			
		}
	}
</script>

<style>
	.box{
		height: 500upx;
		width: 100%;
		border: 5upx solid #cccccc;
		background-color: white;
		line-height: 500upx;
		display: flex; 
	}
	.box-item {
		color: white;
		height: 130upx;
		/* width: 130upx; */
		flex: 1;
		line-height: 130upx;
		font-size: 30upx;
		font-weight: bold;
		text-align: center;
	}
	.box-item2 {
		color: white;
		height: 260upx;
		width: 260upx;
		line-height: 260upx;
		font-size: 30upx;
		font-weight: bold;
		text-align: center;
	}
	
	.box-item:nth-of-type(odd){
		background: green;
	}
	.box-item:nth-of-type(even){
		background: blue;
	}
	
	.box-item2:nth-of-type(odd){
		background: purple;
	}
	.box-item2:nth-of-type(even){
		background: orange;
	}

</style>


*flex 占比

image.png

	.box-item:nth-of-type(1){
		flex: 1;
	}
	.box-item:nth-of-type(2){
		flex: 2;
	}
	.box-item:nth-of-type(3){
		flex: 1;
	}

* align-self: flex-end; 置某个元素 的位置

image.png

.box-item:nth-of-type(1){
		flex: 1;
	}
	.box-item:nth-of-type(2){
		flex: 2;
		align-self: flex-end;
	}
	.box-item:nth-of-type(3){
		flex: 1;
	}