u-subsection.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <template>
  2. <view
  3. class="u-subsection"
  4. ref="u-subsection"
  5. :class="[`u-subsection--${mode}`]"
  6. :style="[$u.addStyle(customStyle), wrapperStyle]"
  7. >
  8. <view
  9. class="u-subsection__bar"
  10. ref="u-subsection__bar"
  11. :style="[barStyle]"
  12. :class="[
  13. mode === 'button' && 'u-subsection--button__bar',
  14. current === 0 &&
  15. mode === 'subsection' &&
  16. 'u-subsection__bar--first',
  17. current > 0 &&
  18. current < this.list.length - 1 &&
  19. mode === 'subsection' &&
  20. 'u-subsection__bar--center',
  21. current === this.list.length - 1 &&
  22. mode === 'subsection' &&
  23. 'u-subsection__bar--last',
  24. ]"
  25. ></view>
  26. <view
  27. class="u-subsection__item"
  28. :class="[
  29. `u-subsection__item--${index}`,
  30. index < list.length - 1 &&
  31. 'u-subsection__item--no-border-right',
  32. index === 0 && 'u-subsection__item--first',
  33. index === list.length - 1 && 'u-subsection__item--last',
  34. ]"
  35. :ref="`u-subsection__item--${index}`"
  36. :style="[itemStyle(index)]"
  37. @tap="clickHandler(index)"
  38. v-for="(item, index) in list"
  39. :key="index"
  40. >
  41. <text
  42. class="u-subsection__item__text"
  43. :style="[textStyle(index)]"
  44. >{{ item }}</text
  45. >
  46. </view>
  47. </view>
  48. </template>
  49. <script>
  50. // #ifdef APP-NVUE
  51. const dom = uni.requireNativePlugin("dom");
  52. const animation = uni.requireNativePlugin("animation");
  53. // #endif
  54. import props from "./props.js";
  55. /**
  56. * Subsection 分段器
  57. * @description 该分段器一般用于用户从几个选项中选择某一个的场景
  58. * @tutorial https://www.uviewui.com/components/subsection.html
  59. * @property {Array} list tab的数据
  60. * @property {String | Number} current 当前活动的tab的index(默认 0 )
  61. * @property {String} activeColor 激活时的颜色(默认 '#3c9cff' )
  62. * @property {String} inactiveColor 未激活时的颜色(默认 '#303133' )
  63. * @property {String} mode 模式选择,mode=button为按钮形式,mode=subsection时为分段模式(默认 'button' )
  64. * @property {String | Number} fontSize 字体大小,单位px(默认 12 )
  65. * @property {Boolean} bold 激活选项的字体是否加粗(默认 true )
  66. * @property {String} bgColor 组件背景颜色,mode为button时有效(默认 '#eeeeef' )
  67. * @property {Object} customStyle 定义需要用到的外部样式
  68. *
  69. * @event {Function} change 分段器选项发生改变时触发 回调 index:选项的index索引值,从0开始
  70. * @example <u-subsection :list="list" :current="curNow" @change="sectionChange"></u-subsection>
  71. */
  72. export default {
  73. name: "u-subsection",
  74. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  75. data() {
  76. return {
  77. // 组件尺寸
  78. itemRect: {
  79. width: 0,
  80. height: 0,
  81. },
  82. };
  83. },
  84. watch: {
  85. list(newValue, oldValue) {
  86. this.init();
  87. },
  88. current: {
  89. immediate: true,
  90. handler(n) {
  91. // #ifdef APP-NVUE
  92. // 在安卓nvue上,如果通过translateX进行位移,到最后一个时,会导致右侧无法绘制圆角
  93. // 故用animation模块进行位移
  94. const ref = this.$refs?.["u-subsection__bar"]?.ref;
  95. // 不存在ref的时候(理解为第一次初始化时,需要渲染dom,进行一定延时再获取ref),这里的100ms是经过测试得出的结果(某些安卓需要延时久一点),勿随意修改
  96. uni.$u.sleep(ref ? 0 : 100).then(() => {
  97. animation.transition(this.$refs["u-subsection__bar"].ref, {
  98. styles: {
  99. transform: `translateX(${
  100. n * this.itemRect.width
  101. }px)`,
  102. transformOrigin: "center center",
  103. },
  104. duration: 300,
  105. });
  106. });
  107. // #endif
  108. },
  109. },
  110. },
  111. computed: {
  112. wrapperStyle() {
  113. const style = {};
  114. // button模式时,设置背景色
  115. if (this.mode === "button") {
  116. style.backgroundColor = this.bgColor;
  117. }
  118. return style;
  119. },
  120. // 滑块的样式
  121. barStyle() {
  122. const style = {};
  123. style.width = `${this.itemRect.width}px`;
  124. style.height = `${this.itemRect.height}px`;
  125. // 通过translateX移动滑块,其移动的距离为索引*item的宽度
  126. // #ifndef APP-NVUE
  127. style.transform = `translateX(${
  128. this.current * this.itemRect.width
  129. }px)`;
  130. // #endif
  131. if (this.mode === "subsection") {
  132. // 在subsection模式下,需要动态设置滑块的圆角,因为移动滑块使用的是translateX,无法通过父元素设置overflow: hidden隐藏滑块的直角
  133. style.backgroundColor = this.activeColor;
  134. }
  135. return style;
  136. },
  137. // 分段器item的样式
  138. itemStyle(index) {
  139. return (index) => {
  140. const style = {};
  141. if (this.mode === "subsection") {
  142. // 设置border的样式
  143. style.borderColor = this.activeColor;
  144. style.borderWidth = "1px";
  145. style.borderStyle = "solid";
  146. }
  147. return style;
  148. };
  149. },
  150. // 分段器文字颜色
  151. textStyle(index) {
  152. return (index) => {
  153. const style = {};
  154. style.fontWeight =
  155. this.bold && this.current === index ? "bold" : "normal";
  156. style.fontSize = uni.$u.addUnit(this.fontSize);
  157. // subsection模式下,激活时默认为白色的文字
  158. if (this.mode === "subsection") {
  159. style.color =
  160. this.current === index ? "#fff" : this.activeColor;
  161. } else {
  162. // button模式下,激活时文字颜色默认为activeColor
  163. style.color =
  164. this.current === index
  165. ? this.activeColor
  166. : this.inactiveColor;
  167. }
  168. return style;
  169. };
  170. },
  171. },
  172. mounted() {
  173. this.init();
  174. },
  175. methods: {
  176. init() {
  177. uni.$u.sleep().then(() => this.getRect());
  178. },
  179. // 获取组件的尺寸
  180. getRect() {
  181. // #ifndef APP-NVUE
  182. this.$uGetRect(".u-subsection__item--0").then((size) => {
  183. this.itemRect = size;
  184. });
  185. // #endif
  186. // #ifdef APP-NVUE
  187. const ref = this.$refs["u-subsection__item--0"][0];
  188. ref &&
  189. dom.getComponentRect(ref, (res) => {
  190. this.itemRect = res.size;
  191. });
  192. // #endif
  193. },
  194. clickHandler(index) {
  195. this.$emit("change", index);
  196. },
  197. },
  198. };
  199. </script>
  200. <style lang="scss" scoped>
  201. @import "../../libs/css/components.scss";
  202. .u-subsection {
  203. @include flex;
  204. position: relative;
  205. overflow: hidden;
  206. /* #ifndef APP-NVUE */
  207. width: 100%;
  208. box-sizing: border-box;
  209. /* #endif */
  210. &--button {
  211. height: 32px;
  212. background-color: rgb(238, 238, 239);
  213. padding: 3px;
  214. border-radius: 3px;
  215. align-items: stretch;
  216. &__bar {
  217. background-color: #ffffff;
  218. border-radius: 3px !important;
  219. }
  220. }
  221. &--subsection {
  222. height: 30px;
  223. }
  224. &__bar {
  225. position: absolute;
  226. /* #ifndef APP-NVUE */
  227. transition-property: transform, color;
  228. transition-duration: 0.3s;
  229. transition-timing-function: ease-in-out;
  230. /* #endif */
  231. &--first {
  232. border-top-left-radius: 3px;
  233. border-bottom-left-radius: 3px;
  234. border-top-right-radius: 0px;
  235. border-bottom-right-radius: 0px;
  236. }
  237. &--center {
  238. border-top-left-radius: 0px;
  239. border-bottom-left-radius: 0px;
  240. border-top-right-radius: 0px;
  241. border-bottom-right-radius: 0px;
  242. }
  243. &--last {
  244. border-top-left-radius: 0px;
  245. border-bottom-left-radius: 0px;
  246. border-top-right-radius: 3px;
  247. border-bottom-right-radius: 3px;
  248. }
  249. }
  250. &__item {
  251. @include flex;
  252. flex: 1;
  253. justify-content: center;
  254. align-items: center;
  255. // vue环境下,需要设置相对定位,因为滑块为绝对定位,item需要在滑块的上面
  256. position: relative;
  257. &--no-border-right {
  258. border-right-width: 0 !important;
  259. }
  260. &--first {
  261. border-top-left-radius: 3px;
  262. border-bottom-left-radius: 3px;
  263. }
  264. &--last {
  265. border-top-right-radius: 3px;
  266. border-bottom-right-radius: 3px;
  267. }
  268. &__text {
  269. font-size: 12px;
  270. line-height: 12px;
  271. @include flex;
  272. align-items: center;
  273. transition-property: color;
  274. transition-duration: 0.3s;
  275. }
  276. }
  277. }
  278. </style>