uni-row.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <template>
  2. <view :class="[ 'uni-row', typeClass , justifyClass, alignClass, ]" :style="{
  3. marginLeft:`${Number(marginValue)}rpx`,
  4. marginRight:`${Number(marginValue)}rpx`,
  5. }">
  6. <slot></slot>
  7. </view>
  8. </template>
  9. <script>
  10. const ComponentClass = 'uni-row';
  11. const modifierSeparator = '--';
  12. /**
  13. * Row 布局-行
  14. * @description 流式栅格系统,随着屏幕或视口分为 24 份,可以迅速简便地创建布局。
  15. * @tutorial https://ext.dcloud.net.cn/plugin?id=3958
  16. *
  17. * @property {gutter} type = Number 栅格间隔
  18. * @property {justify} type = String flex 布局下的水平排列方式
  19. * 可选 start/end/center/space-around/space-between start
  20. * 默认值 start
  21. * @property {align} type = String flex 布局下的垂直排列方式
  22. * 可选 top/middle/bottom
  23. * 默认值 top
  24. * @property {width} type = String|Number nvue下需要自行配置宽度用于计算
  25. * 默认值 750
  26. */
  27. export default {
  28. name: 'uniRow',
  29. componentName: 'uniRow',
  30. // #ifdef MP-WEIXIN
  31. options: {
  32. virtualHost: true // 在微信小程序中将组件节点渲染为虚拟节点,更加接近Vue组件的表现,可使用flex布局
  33. },
  34. // #endif
  35. props: {
  36. type: String,
  37. gutter: Number,
  38. justify: {
  39. type: String,
  40. default: 'start'
  41. },
  42. align: {
  43. type: String,
  44. default: 'top'
  45. },
  46. // nvue如果使用span等属性,需要配置宽度
  47. width: {
  48. type: [String, Number],
  49. default: 750
  50. }
  51. },
  52. created() {
  53. // #ifdef APP-NVUE
  54. this.type = 'flex';
  55. // #endif
  56. },
  57. computed: {
  58. marginValue() {
  59. // #ifndef APP-NVUE
  60. if (this.gutter) {
  61. return -(this.gutter / 2);
  62. }
  63. // #endif
  64. return 0;
  65. },
  66. typeClass() {
  67. return this.type === 'flex' ? `${ComponentClass + modifierSeparator}flex` : '';
  68. },
  69. justifyClass() {
  70. return this.justify !== 'start' ? `${ComponentClass + modifierSeparator}flex-justify-${this.justify}` : ''
  71. },
  72. alignClass() {
  73. return this.align !== 'top' ? `${ComponentClass + modifierSeparator}flex-align-${this.align}` : ''
  74. }
  75. }
  76. };
  77. </script>
  78. <style lang="scss">
  79. $layout-namespace: ".uni-";
  80. $row:$layout-namespace+"row";
  81. $modifier-separator: "--";
  82. @mixin utils-clearfix {
  83. $selector: &;
  84. @at-root {
  85. /* #ifndef APP-NVUE */
  86. #{$selector}::before,
  87. #{$selector}::after {
  88. display: table;
  89. content: "";
  90. }
  91. #{$selector}::after {
  92. clear: both;
  93. }
  94. /* #endif */
  95. }
  96. }
  97. @mixin utils-flex ($direction: row) {
  98. /* #ifndef APP-NVUE */
  99. display: flex;
  100. /* #endif */
  101. flex-direction: $direction;
  102. }
  103. @mixin set-flex($state) {
  104. @at-root &-#{$state} {
  105. @content
  106. }
  107. }
  108. #{$row} {
  109. position: relative;
  110. flex-direction: row;
  111. /* #ifdef APP-NVUE */
  112. flex: 1;
  113. /* #endif */
  114. /* #ifndef APP-NVUE */
  115. box-sizing: border-box;
  116. /* #endif */
  117. // 非nvue使用float布局
  118. @include utils-clearfix;
  119. // 在QQ、字节、百度小程序平台,编译后使用shadow dom,不可使用flex布局,使用float
  120. @at-root {
  121. /* #ifndef MP-QQ || MP-TOUTIAO || MP-BAIDU */
  122. &#{$modifier-separator}flex {
  123. @include utils-flex;
  124. flex-wrap: wrap;
  125. flex: 1;
  126. &:before,
  127. &:after {
  128. /* #ifndef APP-NVUE */
  129. display: none;
  130. /* #endif */
  131. }
  132. @include set-flex(justify-center) {
  133. justify-content: center;
  134. }
  135. @include set-flex(justify-end) {
  136. justify-content: flex-end;
  137. }
  138. @include set-flex(justify-space-between) {
  139. justify-content: space-between;
  140. }
  141. @include set-flex(justify-space-around) {
  142. justify-content: space-around;
  143. }
  144. @include set-flex(align-middle) {
  145. align-items: center;
  146. }
  147. @include set-flex(align-bottom) {
  148. align-items: flex-end;
  149. }
  150. }
  151. /* #endif */
  152. }
  153. }
  154. // 字节、QQ配置后不生效
  155. // 此处用法无法使用scoped
  156. /* #ifdef MP-WEIXIN || MP-TOUTIAO || MP-QQ */
  157. :host {
  158. display: block;
  159. }
  160. /* #endif */
  161. </style>