wxc-flex

MinUI 小程序组件 - flex

Install

  1. $ min install @minui/wxc-flex

Demos

常见布局 - 水平垂直居中

flex flex - 图1

  1. <template>
  2. <wxc-flex class="wrap" main="center" cross="center">
  3. <view class="item"></view>
  4. <view class="item"></view>
  5. </wxc-flex>
  6. <view class="layout-code">
  7. <wxc-flex <view class="layout-code__point">main="center" cross="center"</view>>...</wxc-flex>
  8. </view>
  9. </template>
  10. <script>
  11. export default {
  12. config: {
  13. usingComponents: {
  14. 'wxc-flex': '@minui/wxc-flex'
  15. }
  16. },
  17. data: {},
  18. methods: {}
  19. }
  20. </script>
  21. <style>
  22. .wrap {
  23. display: block;
  24. height: 300rpx;
  25. padding: 10rpx;
  26. background: #696969;
  27. }
  28. .item {
  29. min-width: 100rpx;
  30. min-height: 100rpx;
  31. }
  32. .item:nth-of-type(odd) {
  33. background: #afdde3
  34. }
  35. .item:nth-of-type(2n) {
  36. background: #f397b2
  37. }
  38. .layout-code {
  39. padding: 15rpx;
  40. background: #222222;
  41. color: #FFFFFF;
  42. font-size: 20rpx;
  43. font-family: Monaco;
  44. }
  45. .layout-code__point {
  46. display: inline-block;
  47. padding: 0 5rpx;
  48. background: #444444;
  49. }
  50. </style>

交叉轴排列

项目在交叉轴上如何对齐

flex flex - 图2

  1. <template>
  2. <wxc-flex class="wrap" cross="{{cross}}">
  3. <view class="item">栏 1</view>
  4. <view class="item">栏 2</view>
  5. <view class="item">栏 3</view>
  6. </wxc-flex>
  7. <view class="layout-code">
  8. <wxc-flex <view class="layout-code__point">cross="{{cross}}"</view>>...</wxc-flex>
  9. </view>
  10. <view class="setting-button" bindtap="setCrossToStretch">拉伸铺满(默认)</view>
  11. <view class="setting-button" bindtap="setCrossToStart">启点排列</view>
  12. <view class="setting-button" bindtap="setCrossToEnd">终点排列</view>
  13. <view class="setting-button" bindtap="setCrossToCenter">居中对齐</view>
  14. <view class="setting-button" bindtap="setCrossToBaseline">基线对齐</view>
  15. </template>
  16. <script>
  17. export default {
  18. config: {
  19. usingComponents: {
  20. 'wxc-flex': '@minui/wxc-flex'
  21. }
  22. },
  23. data: {
  24. cross: 'stretch'
  25. },
  26. /** note: 在 wxp 文件或者页面文件中请去掉 methods 包装 */
  27. methods: {
  28. setCrossToStretch: function () {
  29. this.setData({
  30. cross: 'stretch'
  31. })
  32. },
  33. setCrossToStart: function () {
  34. this.setData({
  35. cross: 'start'
  36. })
  37. },
  38. setCrossToEnd: function () {
  39. this.setData({
  40. cross: 'end'
  41. })
  42. },
  43. setCrossToCenter: function () {
  44. this.setData({
  45. cross: 'center'
  46. })
  47. },
  48. setCrossToBaseline: function () {
  49. this.setData({
  50. cross: 'baseline'
  51. })
  52. }
  53. }
  54. }
  55. </script>
  56. <style>
  57. .wrap {
  58. display: block;
  59. height: 300rpx;
  60. padding: 10rpx;
  61. background: #696969;
  62. }
  63. .item {
  64. min-width: 100rpx;
  65. min-height: 100rpx;
  66. font-size: 22rpx;
  67. text-align: center;
  68. }
  69. .item:nth-of-type(odd) {
  70. line-height: 30rpx;
  71. background: #afdde3
  72. }
  73. .item:nth-of-type(2n) {
  74. line-height: 50rpx;
  75. background: #f397b2
  76. }
  77. .setting-button {
  78. display: inline-block;
  79. line-height: 50rpx;
  80. margin: 20rpx 10rpx 0 0;
  81. padding: 2rpx 20rpx;
  82. border-radius: 6rpx;
  83. background: #31b0d5;
  84. color: #ffffff;
  85. font-size: 18rpx;
  86. text-align: center;
  87. }
  88. .layout-code {
  89. padding: 15rpx;
  90. background: #222222;
  91. color: #FFFFFF;
  92. font-size: 20rpx;
  93. font-family: Monaco;
  94. }
  95. .layout-code__point {
  96. display: inline-block;
  97. padding: 0 5rpx;
  98. background: #444444;
  99. }
  100. </style>

主轴方向

项目的排列方向

flex flex - 图3

  1. <template>
  2. <wxc-flex class="wrap" dir="{{dir}}">
  3. <view class="item">栏 1</view>
  4. <view class="item">栏 2</view>
  5. <view class="item">栏 3</view>
  6. </wxc-flex>
  7. <view class="layout-code">
  8. <wxc-flex <view class="layout-code__point">dir="{{dir}}"</view>>...</wxc-flex>
  9. </view>
  10. <view class="setting-button" bindtap="setDirToLeft">水平正序(默认)</view>
  11. <view class="setting-button" bindtap="setDirToRight">水平倒序</view>
  12. <view class="setting-button" bindtap="setDirToTop">垂直正序</view>
  13. <view class="setting-button" bindtap="setDirToBottom">垂直倒序</view>
  14. </template>
  15. <script>
  16. export default {
  17. config: {
  18. usingComponents: {
  19. 'wxc-flex': '@minui/wxc-flex'
  20. }
  21. },
  22. data: {
  23. dir: 'left'
  24. },
  25. /** note: 在 wxp 文件或者页面文件中请去掉 methods 包装 */
  26. methods: {
  27. setDirToLeft: function () {
  28. this.setData({
  29. dir: 'left'
  30. })
  31. },
  32. setDirToRight: function () {
  33. this.setData({
  34. dir: 'right'
  35. })
  36. },
  37. setDirToTop: function () {
  38. this.setData({
  39. dir: 'top'
  40. })
  41. },
  42. setDirToBottom: function () {
  43. this.setData({
  44. dir: 'bottom'
  45. })
  46. }
  47. }
  48. }
  49. </script>
  50. <style>
  51. .wrap {
  52. display: block;
  53. padding: 10rpx;
  54. background: #696969;
  55. }
  56. .item {
  57. min-width: 100rpx;
  58. min-height: 100rpx;
  59. font-size: 22rpx;
  60. text-align: center;
  61. }
  62. .item:nth-of-type(odd) {
  63. line-height: 30rpx;
  64. background: #afdde3
  65. }
  66. .item:nth-of-type(2n) {
  67. line-height: 50rpx;
  68. background: #f397b2
  69. }
  70. .setting-button {
  71. display: inline-block;
  72. line-height: 50rpx;
  73. margin: 20rpx 10rpx 0 0;
  74. padding: 2rpx 20rpx;
  75. border-radius: 6rpx;
  76. background: #31b0d5;
  77. color: #ffffff;
  78. font-size: 18rpx;
  79. text-align: center;
  80. }
  81. .layout-code {
  82. padding: 15rpx;
  83. background: #222222;
  84. color: #FFFFFF;
  85. font-size: 20rpx;
  86. font-family: Monaco;
  87. }
  88. .layout-code__point {
  89. display: inline-block;
  90. padding: 0 5rpx;
  91. background: #444444;
  92. }
  93. </style>

常见布局 - 定宽+弹性宽度

flex flex - 图4

  1. <template>
  2. <wxc-flex class="wrap">
  3. <view class="item side">固定宽度</view>
  4. <view class="item main">弹性宽度</view>
  5. </wxc-flex>
  6. </template>
  7. <script>
  8. export default {
  9. config: {
  10. usingComponents: {
  11. 'wxc-flex': '@minui/wxc-flex'
  12. }
  13. },
  14. data: {},
  15. methods: {}
  16. }
  17. </script>
  18. <style>
  19. .wrap {
  20. display: block;
  21. height: 300rpx;
  22. background: #696969;
  23. }
  24. .item {
  25. font-size: 20rpx;
  26. line-height: 300rpx;
  27. text-align: center;
  28. }
  29. .side {
  30. width: 200rpx;
  31. background: #afdde3;
  32. flex-grow: 0;
  33. }
  34. .main {
  35. background: #f397b2;
  36. flex-grow: 1;
  37. }
  38. </style>

常见布局 - 列表

flex flex - 图5

  1. <template>
  2. <wxc-flex class="wrap" cross="top" wrap="wrap">
  3. <view class="item"></view>
  4. <view class="item"></view>
  5. <view class="item"></view>
  6. <view class="item"></view>
  7. <view class="item"></view>
  8. </wxc-flex>
  9. </template>
  10. <script>
  11. export default {
  12. config: {
  13. usingComponents: {
  14. 'wxc-flex': '@minui/wxc-flex'
  15. }
  16. },
  17. data: {},
  18. methods: {}
  19. }
  20. </script>
  21. <style>
  22. .wrap {
  23. display: block;
  24. padding: 2%;
  25. background: #696969;
  26. }
  27. .item {
  28. width: 47%;
  29. height: 348rpx;
  30. margin: 1.5%;
  31. flex-grow: 0;
  32. }
  33. .item:nth-of-type(odd) {
  34. background: #afdde3
  35. }
  36. .item:nth-of-type(2n) {
  37. background: #f397b2
  38. }
  39. </style>

主轴排列

项目在主轴上的对齐方式

flex flex - 图6

<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>

换行选项

如果一条轴线排不下,如何换行

flex flex - 图7

<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 会报错,绑定事件请用该自定义事件。
地址
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

flex

ChangeLog

v1.0.3(2018.6.5)

  • 增加点击事件。

v1.0.2(2017.11.02)

  • update .npmignore

v1.0.1(2017.10.24)

  • 初始版本