微信小程序开发【大前端】精选

从微信小程序看待FlexBox布局

2017-02-11  本文已影响808人  Lefe

说明

本文是作者Lefe所创,转载请注明出处,如果你在阅读的时候发现问题欢迎一起讨论。本文会不断更新。

正文

开始学习微信小程序时,需要掌握最基本的UI布局,有了UI的布局才是一个开始。下面主要通过一些例子来聊聊FlexBox布局,其实它和ReactNative大同小异。所以学习一门技术,其它的也就不愁了。下面主要通过一些例子来说明FlexBox是如何布局的。

FlexBox概述

1.png 2.png 3.png

一、容器属性介绍

<view class="container">
    <view class="children1"></view>
    <view class="children2"></view>
    <view class="children3"></view>
</view>

me.wxss文件

.container {
    display: flex;
    background-color: lightblue;
}
.children1 {
    width: 100rpx;
    height: 100rpx;
    background-color: red;
}
.children2 {
    width: 100rpx;
    height: 100rpx;
    background-color: yellow;
}
.children3 {
    width: 100rpx;
    height: 100rpx;
    background-color: purple;
}

flex-direction:column,垂直方向布局,从上到下布局

4.png

flex-direction: column-reverse;,垂直方向布局,从下到上布局

column.png

flex-direction: row;,默认,水平方向布局,从左到右布局

.container {
    display: flex;
    flex-direction: row;
    background-color: lightblue;
}
row.png

flex-direction: row-reverse;,水平方向布局,从右到左布局

row-reverse.png
.container {
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    background-color: lightblue;
}
norwrap.png
.container {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    background-color: lightblue;
}
wrap.png
.container {
    display: flex;
    flex-flow: row wrap;
    background-color: lightblue;
}
.container {
    display: flex;
    flex-flow: row wrap;
    justify-content: space-around;
    background-color: lightblue;
}
space-around.png
.container {
    display: flex;
    flex-flow: row wrap;
    justify-content: space-between;
    background-color: lightblue;
}
space-between.png
.flex-container {
    height: 400rpx;
    display: flex;
    flex-direction: row;
    justify-content: space-around;
    align-items: stretch;
    background-color: lightblue;
}
.children1 {
    width: 100rpx;
    min-height: 100rpx;
    background-color: red;
}
stretch.png
.flex-container {
    height: 400rpx;
    display: flex;
    flex-direction: row;
    justify-content: space-around;
    align-items: stretch;
    flex-wrap: wrap;
    align-content: flex-end;
    background-color: lightblue;
}
.children1 {
    width: 600rpx;
    height: 100rpx;
    background-color: red;
}
.children2 {
    width: 600rpx;
    height: 100rpx;
    background-color: yellow;
}
.children3 {
    width: 600rpx;
    height: 100rpx;
    background-color: purple;
}
align-content.png

二、Flex item属性介绍

.children1 {
    width: 100rpx;
    height: 100rpx;
    order: 3;
    background-color: red;
}
.children1 {
    width: 20rpx;
    flex-grow: 1;
    height: 100rpx;
    background-color: red;
}
.children2 {
    width: 100rpx;
    height: 100rpx;
    background-color: yellow;
}
.children3 {
    width: 100rpx;
    flex-grow: 2;
    height: 100rpx;
    background-color: purple;
}
flex-grow.png
.children1 {
    width: 1000rpx;
    flex-shrink: 2;
    height: 100rpx;
    background-color: red;
}
.children2 {
    width: 100rpx;
    height: 100rpx;
    flex-shrink: 0;
    background-color: yellow;
}
.children3 {
    width: 1000rpx;
    flex-shrink: 3;
    height: 100rpx;
    background-color: purple;
}
flex-shrink.png
.children1 {
    height: 100rpx;
    flex-basis: 50%;
    background-color: red;
}

参考:

阮一峰
Flex box guide

===== 我是有底线的 ======
喜欢我的文章,欢迎关注我的新浪微博 Lefe_x,我会不定期的分享一些开发技巧

上一篇 下一篇

猜你喜欢

热点阅读