微信小程序Flex布局实战——制作色子效果
问题背景
前一篇文章中,介绍了微信小程序开发中的flex布局基本知识,本文将介绍如何基于flex布局实现色子效果。
问题分析
flex布局重点回顾:
1、flex容器结构图:
2、flex-direction属性决定主轴的方向(即项目的排列方向)
.box {
flex-direction: row | row-reverse | column | column-reverse;
}
它可能有4个值。 row(默认值):主轴为水平方向,起点在左端。 row-reverse:主轴为水平方向,起点在右端。 column:主轴为垂直方向,起点在上沿。 column-reverse:主轴为垂直方向,起点在下沿。 3、flex-wrap属性定义,如果一条轴线排不下,如何换行。
.box{
flex-wrap: nowrap | wrap | wrap-reverse;
}
它可能取三个值。 nowrap(默认):不换行。 wrap:换行,第一行在上方。 wrap-reverse:换行,第一行在下方。 4、justify-content属性定义了项目在主轴上的对齐方式。
.box {
justify-content: flex-start | flex-end | center | space-between | space-around;
}
它可能取5个值,具体对齐方式与轴的方向有关。下面假设主轴为从左到右。 flex-start(默认值):左对齐 flex-end:右对齐 center: 居中 space-between:两端对齐,项目之间的间隔都相等。 space-around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。 5、align-items属性定义项目在交叉轴上如何对齐。
.box {
align-items: flex-start | flex-end | center | baseline | stretch;
}
它可能取5个值。具体的对齐方式与交叉轴的方向有关,下面假设交叉轴从上到下。 flex-start:交叉轴的起点对齐。 flex-end:交叉轴的终点对齐。 center:交叉轴的中点对齐。 baseline: 项目的第一行文字的基线对齐。 stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。 6、align-content属性定义了多根轴线的对齐方式。如果项目只有一根轴线,该属性不起作用。
.box {
align-content: flex-start | flex-end | center | space-between | space-around | stretch;
}
flex-start:与交叉轴的起点对齐。 flex-end:与交叉轴的终点对齐。 center:与交叉轴的中点对齐。 space-between:与交叉轴两端对齐,轴线之间的间隔平均分布。 space-around:每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。 stretch(默认值):轴线占满整个交叉轴。
问题解决
话不多说,直接上代码。 (1)index.wxss文件,代码如下:
.outLayer{
margin:50rpx;
height: 1800rpx;
display:flex;
flex-direction: row;
flex-wrap: wrap;
align-content: flex-start;
background: gray;
}
.item1{
background: rgb(231, 121, 121);
width: 300rpx;
height: 300rpx;
display: flex;
justify-content: center;
align-items: center;
}
.subItem1{
background: black;
width: 100rpx;
height: 100rpx;
border-radius: 50%;
}
.item2{
background: rgb(231, 220, 121);
width: 300rpx;
height: 300rpx;
display: flex;
flex-direction: column;
justify-content: space-between;
align-items: center;
}
.subItem2{
background: black;
width: 100rpx;
height: 100rpx;
border-radius: 50%;
}
.item3{
background: rgb(121, 156, 231);
width: 300rpx;
height: 300rpx;
display: flex;
flex-direction: column;
}
.subItem3{
background: black;
width: 100rpx;
height: 100rpx;
border-radius: 50%;
align-self: center;
}
.subItem31{
background: black;
width: 100rpx;
height: 100rpx;
border-radius: 50%;
align-self: flex-end;
}
.item4{
background: rgb(121, 256, 231);
width: 300rpx;
height: 300rpx;
display: flex;
flex-direction: row;
justify-content: space-around;
}
.subItem4{
background: rgb(121, 256, 231);
width: 100rpx;
height: 300rpx;
display: flex;
flex-direction: column;
justify-content: space-between;
align-items: center;
}
.item5{
background: red;
width: 300rpx;
height: 300rpx;
display: flex;
flex-direction: row;
justify-content: space-around;
}
.subItem5{
background: rgb(231, 220, 121);
width: 100rpx;
height: 300rpx;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.subItem51{
background: rgb(231, 220, 121);
width: 300rpx;
height: 300rpx;
display: flex;
flex-direction: column;
justify-content: center;
}
.subItem52{
background: black;
width: 100rpx;
height: 100rpx;
border-radius: 50%;
}
.subItem53{
background: rgb(231, 220, 121);
width: 300rpx;
height: 300rpx;
display: flex;
flex-direction: column;
justify-content: space-between;
align-items: flex-end;
}
.item6{
background: white;
width: 300rpx;
height: 300rpx;
display: flex;
flex-direction: row;
justify-content: space-around;
}
.subItem6{
background: white;
width: 100rpx;
height: 300rpx;
display: flex;
flex-direction: column;
justify-content: space-between;
align-items: flex-end;
}
(2)index.wxml文件,代码如下:
<view class="outLayer">
<view class="item1">
<view class="subItem1">
</view>
</view>
<view class="item2">
<view class="subItem2">
</view>
<view class="subItem2">
</view>
</view>
<view class="item3">
<view class="subItem31">
</view>
<view class="subItem3">
</view>
<view class="subItem2">
</view>
</view>
<view class="item4">
<view class="subItem4">
<view class="subItem2">
</view>
<view class="subItem2">
</view>
</view>
<view class="subItem4">
<view class="subItem2">
</view>
<view class="subItem2">
</view>
</view>
</view>
<view class="item5">
<view class="subItem5">
<view class="subItem2">
</view>
<view class="subItem2">
</view>
</view>
<view class="subItem51">
<view class="subItem52">
</view>
</view>
<view class="subItem53">
<view class="subItem2">
</view>
<view class="subItem2">
</view>
</view>
</view>
<view class="item6">
<View class="subItem6">
<View class="subItem2"></View>
<View class="subItem2"></View>
<View class="subItem2"></View>
</View>
<View class="subItem6">
<View class="subItem2"></View>
<View class="subItem2"></View>
<View class="subItem2"></View>
</View>
</view>
</view>
运行结果如下:
问题总结
本文将介绍了微信小程序开发过程使用flex布局实现色子效果,有兴趣的同学可以进一步深入研究。