wxc-flex
MinUI 小程序组件 - flex
Install
$ min install @minui/wxc-flex
Demos
常见布局 - 水平垂直居中
<template>
<wxc-flex class="wrap" main="center" cross="center">
<view class="item"></view>
<view class="item"></view>
</wxc-flex>
<view class="layout-code">
<wxc-flex <view class="layout-code__point">main="center" cross="center"</view>>...</wxc-flex>
</view>
</template>
<script>
export default {
config: {
usingComponents: {
'wxc-flex': '@minui/wxc-flex'
}
},
data: {},
methods: {}
}
</script>
<style>
.wrap {
display: block;
height: 300rpx;
padding: 10rpx;
background: #696969;
}
.item {
min-width: 100rpx;
min-height: 100rpx;
}
.item:nth-of-type(odd) {
background: #afdde3
}
.item:nth-of-type(2n) {
background: #f397b2
}
.layout-code {
padding: 15rpx;
background: #222222;
color: #FFFFFF;
font-size: 20rpx;
font-family: Monaco;
}
.layout-code__point {
display: inline-block;
padding: 0 5rpx;
background: #444444;
}
</style>
交叉轴排列
项目在交叉轴上如何对齐
<template>
<wxc-flex class="wrap" cross="{{cross}}">
<view class="item">栏 1</view>
<view class="item">栏 2</view>
<view class="item">栏 3</view>
</wxc-flex>
<view class="layout-code">
<wxc-flex <view class="layout-code__point">cross="{{cross}}"</view>>...</wxc-flex>
</view>
<view class="setting-button" bindtap="setCrossToStretch">拉伸铺满(默认)</view>
<view class="setting-button" bindtap="setCrossToStart">启点排列</view>
<view class="setting-button" bindtap="setCrossToEnd">终点排列</view>
<view class="setting-button" bindtap="setCrossToCenter">居中对齐</view>
<view class="setting-button" bindtap="setCrossToBaseline">基线对齐</view>
</template>
<script>
export default {
config: {
usingComponents: {
'wxc-flex': '@minui/wxc-flex'
}
},
data: {
cross: 'stretch'
},
/** note: 在 wxp 文件或者页面文件中请去掉 methods 包装 */
methods: {
setCrossToStretch: function () {
this.setData({
cross: 'stretch'
})
},
setCrossToStart: function () {
this.setData({
cross: 'start'
})
},
setCrossToEnd: function () {
this.setData({
cross: 'end'
})
},
setCrossToCenter: function () {
this.setData({
cross: 'center'
})
},
setCrossToBaseline: function () {
this.setData({
cross: 'baseline'
})
}
}
}
</script>
<style>
.wrap {
display: block;
height: 300rpx;
padding: 10rpx;
background: #696969;
}
.item {
min-width: 100rpx;
min-height: 100rpx;
font-size: 22rpx;
text-align: center;
}
.item:nth-of-type(odd) {
line-height: 30rpx;
background: #afdde3
}
.item:nth-of-type(2n) {
line-height: 50rpx;
background: #f397b2
}
.setting-button {
display: inline-block;
line-height: 50rpx;
margin: 20rpx 10rpx 0 0;
padding: 2rpx 20rpx;
border-radius: 6rpx;
background: #31b0d5;
color: #ffffff;
font-size: 18rpx;
text-align: center;
}
.layout-code {
padding: 15rpx;
background: #222222;
color: #FFFFFF;
font-size: 20rpx;
font-family: Monaco;
}
.layout-code__point {
display: inline-block;
padding: 0 5rpx;
background: #444444;
}
</style>
主轴方向
项目的排列方向
<template>
<wxc-flex class="wrap" dir="{{dir}}">
<view class="item">栏 1</view>
<view class="item">栏 2</view>
<view class="item">栏 3</view>
</wxc-flex>
<view class="layout-code">
<wxc-flex <view class="layout-code__point">dir="{{dir}}"</view>>...</wxc-flex>
</view>
<view class="setting-button" bindtap="setDirToLeft">水平正序(默认)</view>
<view class="setting-button" bindtap="setDirToRight">水平倒序</view>
<view class="setting-button" bindtap="setDirToTop">垂直正序</view>
<view class="setting-button" bindtap="setDirToBottom">垂直倒序</view>
</template>
<script>
export default {
config: {
usingComponents: {
'wxc-flex': '@minui/wxc-flex'
}
},
data: {
dir: 'left'
},
/** note: 在 wxp 文件或者页面文件中请去掉 methods 包装 */
methods: {
setDirToLeft: function () {
this.setData({
dir: 'left'
})
},
setDirToRight: function () {
this.setData({
dir: 'right'
})
},
setDirToTop: function () {
this.setData({
dir: 'top'
})
},
setDirToBottom: function () {
this.setData({
dir: 'bottom'
})
}
}
}
</script>
<style>
.wrap {
display: block;
padding: 10rpx;
background: #696969;
}
.item {
min-width: 100rpx;
min-height: 100rpx;
font-size: 22rpx;
text-align: center;
}
.item:nth-of-type(odd) {
line-height: 30rpx;
background: #afdde3
}
.item:nth-of-type(2n) {
line-height: 50rpx;
background: #f397b2
}
.setting-button {
display: inline-block;
line-height: 50rpx;
margin: 20rpx 10rpx 0 0;
padding: 2rpx 20rpx;
border-radius: 6rpx;
background: #31b0d5;
color: #ffffff;
font-size: 18rpx;
text-align: center;
}
.layout-code {
padding: 15rpx;
background: #222222;
color: #FFFFFF;
font-size: 20rpx;
font-family: Monaco;
}
.layout-code__point {
display: inline-block;
padding: 0 5rpx;
background: #444444;
}
</style>
常见布局 - 定宽+弹性宽度
<template>
<wxc-flex class="wrap">
<view class="item side">固定宽度</view>
<view class="item main">弹性宽度</view>
</wxc-flex>
</template>
<script>
export default {
config: {
usingComponents: {
'wxc-flex': '@minui/wxc-flex'
}
},
data: {},
methods: {}
}
</script>
<style>
.wrap {
display: block;
height: 300rpx;
background: #696969;
}
.item {
font-size: 20rpx;
line-height: 300rpx;
text-align: center;
}
.side {
width: 200rpx;
background: #afdde3;
flex-grow: 0;
}
.main {
background: #f397b2;
flex-grow: 1;
}
</style>
常见布局 - 列表
<template>
<wxc-flex class="wrap" cross="top" wrap="wrap">
<view class="item"></view>
<view class="item"></view>
<view class="item"></view>
<view class="item"></view>
<view class="item"></view>
</wxc-flex>
</template>
<script>
export default {
config: {
usingComponents: {
'wxc-flex': '@minui/wxc-flex'
}
},
data: {},
methods: {}
}
</script>
<style>
.wrap {
display: block;
padding: 2%;
background: #696969;
}
.item {
width: 47%;
height: 348rpx;
margin: 1.5%;
flex-grow: 0;
}
.item:nth-of-type(odd) {
background: #afdde3
}
.item:nth-of-type(2n) {
background: #f397b2
}
</style>
主轴排列
项目在主轴上的对齐方式
<template>
<wxc-flex class="wrap" main="{{main}}">
<view class="item">栏 1</view>
<view class="item">栏 2</view>
<view class="item">栏 3</view>
</wxc-flex>
<view class="layout-code">
<wxc-flex <view class="layout-code__point">main="{{main}}"</view>>...</wxc-flex>
</view>
<view class="setting-button" bindtap="setMainToStart">启点排列(默认)</view>
<view class="setting-button" bindtap="setMainToEnd">终点排列</view>
<view class="setting-button" bindtap="setMainToBetween">两端对齐</view>
<view class="setting-button" bindtap="setMainToCenter">居中对齐</view>
<view class="setting-button" bindtap="setMainToAround">等间分布</view>
</template>
<script>
export default {
config: {
usingComponents: {
'wxc-flex': '@minui/wxc-flex'
}
},
data: {
main: 'left'
},
/** note: 在 wxp 文件或者页面文件中请去掉 methods 包装 */
methods: {
setMainToStart: function () {
this.setData({
main: 'start'
})
},
setMainToEnd: function () {
this.setData({
main: 'end'
})
},
setMainToBetween: function () {
this.setData({
main: 'between'
})
},
setMainToCenter: function () {
this.setData({
main: 'center'
})
},
setMainToAround: function () {
this.setData({
main: 'around'
})
}
}
}
</script>
<style>
.wrap {
display: block;
padding: 10rpx;
background: #696969;
}
.item {
min-width: 100rpx;
min-height: 100rpx;
font-size: 22rpx;
text-align: center;
}
.item:nth-of-type(odd) {
line-height: 30rpx;
background: #afdde3
}
.item:nth-of-type(2n) {
line-height: 50rpx;
background: #f397b2
}
.setting-button {
display: inline-block;
line-height: 50rpx;
margin: 20rpx 10rpx 0 0;
padding: 2rpx 20rpx;
border-radius: 6rpx;
background: #31b0d5;
color: #ffffff;
font-size: 18rpx;
text-align: center;
}
.layout-code {
padding: 15rpx;
background: #222222;
color: #FFFFFF;
font-size: 20rpx;
font-family: Monaco;
}
.layout-code__point {
display: inline-block;
padding: 0 5rpx;
background: #444444;
}
</style>
换行选项
如果一条轴线排不下,如何换行
<template>
<wxc-flex class="wrap" wrap="{{wrap}}">
<view class="item">栏 1</view>
<view class="item">栏 2</view>
<view class="item">栏 3</view>
<view class="item">栏 4</view>
<view class="item">栏 5</view>
<view class="item">栏 6</view>
<view class="item">栏 7</view>
<view class="item">栏 8</view>
<view class="item">栏 9</view>
<view class="item">栏 10</view>
<view class="item">栏 11</view>
<view class="item">栏 12</view>
</wxc-flex>
<view class="layout-code">
<wxc-flex <view class="layout-code__point">wrap="{{wrap}}"</view>>...</wxc-flex>
</view>
<view class="setting-button" bindtap="setWrapToNowrap">不换行(默认)</view>
<view class="setting-button" bindtap="setWrapToWrap">正序换行</view>
<view class="setting-button" bindtap="setWrapToReverse">倒序换行</view>
</template>
<script>
export default {
config: {
usingComponents: {
'wxc-flex': '@minui/wxc-flex'
}
},
data: {
wrap: 'nowrap'
},
/** note: 在 wxp 文件或者页面文件中请去掉 methods 包装 */
methods: {
setWrapToNowrap: function () {
this.setData({
wrap: 'nowrap'
})
},
setWrapToWrap: function () {
this.setData({
wrap: 'wrap'
})
},
setWrapToReverse: function () {
this.setData({
wrap: 'reverse'
})
}
}
}
</script>
<style>
.wrap {
display: block;
padding: 10rpx;
background: #696969;
}
.item {
min-width: 20%;
min-height: 100rpx;
font-size: 22rpx;
text-align: center;
}
.item:nth-of-type(odd) {
line-height: 30rpx;
background: #afdde3
}
.item:nth-of-type(2n) {
line-height: 50rpx;
background: #f397b2
}
.setting-button {
display: inline-block;
line-height: 50rpx;
margin: 20rpx 10rpx 0 0;
padding: 2rpx 20rpx;
border-radius: 6rpx;
background: #31b0d5;
color: #ffffff;
font-size: 18rpx;
text-align: center;
}
.layout-code {
padding: 15rpx;
background: #222222;
color: #FFFFFF;
font-size: 20rpx;
font-family: Monaco;
}
.layout-code__point {
display: inline-block;
padding: 0 5rpx;
background: #444444;
}
</style>
API
Flex【props】
名称 | 描述 |
---|---|
dir | 主轴方向。取值范围: - left:水平正序(默认) - right:水平倒序 - top:垂直正序 - bottom:垂直倒序 |
main | 主轴对齐方式。取值范围: - start:启点排列(默认) - end:终点排列 - between:两端对齐 - center:居中对齐 - around:等间分布 |
cross | 交叉轴对齐方式。取值范围: - stretch:拉伸铺满(默认) - start:启点排列 - end:终点排列 - center:居中对齐 - baseline:基线对齐 |
wrap | 换行设置。取值范围: - nowrap:不换行(默认) - wrap:正序换行 - reverse:倒序换行 |
bind:click | 点击事件。组件带 slot 时,在组件中使用事件点击到 slot 会报错,绑定事件请用该自定义事件。 |
Link
地址 | |
---|---|
flex 组件文档 https://meili.github.io/min/docs/minui/index.html#flex | |
flex 组件源码 https://github.com/meili/minui/tree/master/packages/wxc-flex | |
MinUI 组件库 https://github.com/meili/minui |
Preview
ChangeLog
v1.0.3(2018.6.5)
- 增加点击事件。
v1.0.2(2017.11.02)
- update .npmignore
v1.0.1(2017.10.24)
- 初始版本