u-subsection.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <template>
  2. <view class="u-subsection" :style="[subsectionStyle]">
  3. <view class="u-item u-line-1" :style="[itemStyle(index)]" @tap="click(index)"
  4. :class="[noBorderRight(index), 'u-item-' + index]" v-for="(item, index) in listInfo" :key="index">
  5. <view :style="[textStyle(index)]" class="u-item-text u-line-1">{{ item.name }}</view>
  6. <u-badge v-if="item.num > 0" :count="item.num" :offset="offset" size="mini"></u-badge>
  7. </view>
  8. <view class="u-item-bg" :style="[itemBarStyle]"></view>
  9. </view>
  10. </template>
  11. <script>
  12. /**
  13. * subsection 分段器
  14. * @description 该分段器一般用于用户从几个选项中选择某一个的场景
  15. * @tutorial https://www.uviewui.com/components/subsection.html
  16. * @property {Array} list 选项的数组,形式见上方"基本使用"
  17. * @property {String Number} current 初始化时默认选中的选项索引值(默认0)
  18. * @property {String} active-color 激活时的颜色,mode为subsection时固定为白色(默认#303133)
  19. * @property {String} inactive-color 未激活时字体的颜色,mode为subsection时无效(默认#606266)
  20. * @property {String} mode 模式选择,见官网"模式选择"说明(默认button)
  21. * @property {String Number} font-size 字体大小,单位rpx(默认28)
  22. * @property {String Number} height 组件高度,单位rpx(默认70)
  23. * @property {Boolean} animation 是否开启动画效果,见上方说明(默认true)
  24. * @property {Boolean} bold 激活选项的字体是否加粗(默认true)
  25. * @property {String} bg-color 组件背景颜色,mode为button时有效(默认#eeeeef)
  26. * @property {String} button-color 按钮背景颜色,mode为button时有效(默认#ffffff)
  27. * @event {Function} change 分段器选项发生改变时触发
  28. * @example <u-subsection active-color="#ff9900"></u-subsection>
  29. */
  30. export default {
  31. name: "u-subsection",
  32. emits: ["change", "update:modelValue", "input"],
  33. props: {
  34. // tab的数据
  35. list: {
  36. type: Array,
  37. default () {
  38. return [];
  39. }
  40. },
  41. value: {
  42. type: [String, Number],
  43. default: 0
  44. },
  45. modelValue: {
  46. type: [String, Number],
  47. default: 0
  48. },
  49. // 当前活动的tab的index
  50. current: {
  51. type: [Number, String],
  52. default: 0
  53. },
  54. // 激活的颜色
  55. activeColor: {
  56. type: String,
  57. default: '#303133'
  58. },
  59. // 未激活的颜色
  60. inactiveColor: {
  61. type: String,
  62. default: '#606266'
  63. },
  64. // 模式选择,mode=button为按钮形式,mode=subsection时为分段模式
  65. mode: {
  66. type: String,
  67. default: 'button'
  68. },
  69. // 字体大小,单位rpx
  70. fontSize: {
  71. type: [Number, String],
  72. default: 28
  73. },
  74. // 是否开启动画效果
  75. animation: {
  76. type: Boolean,
  77. default: true
  78. },
  79. // 组件的高度,单位rpx
  80. height: {
  81. type: [Number, String],
  82. default: 70
  83. },
  84. // 激活tab的字体是否加粗
  85. bold: {
  86. type: Boolean,
  87. default: true
  88. },
  89. // mode=button时,组件背景颜色
  90. bgColor: {
  91. type: String,
  92. default: '#eeeeef'
  93. },
  94. // mode = button时,滑块背景颜色
  95. buttonColor: {
  96. type: String,
  97. default: '#ffffff'
  98. },
  99. // 在切换分段器的时候,是否让设备震一下
  100. vibrateShort: {
  101. type: Boolean,
  102. default: false
  103. },
  104. offset: {
  105. type: Array,
  106. default: function() {
  107. return [0, 0]
  108. }
  109. },
  110. },
  111. data() {
  112. return {
  113. itemBgStyle: {
  114. width: 0,
  115. left: 0,
  116. backgroundColor: '#ffffff',
  117. height: '100%',
  118. transition: ''
  119. },
  120. currentIndex: this.current,
  121. buttonPadding: 3, // mode = button 时,组件的内边距
  122. borderRadius: 5, // 圆角值
  123. firstTimeVibrateShort: true, // 组件初始化时,会触发current变化,此时不应震动
  124. listInfo: []
  125. };
  126. },
  127. watch: {
  128. valueCom: {
  129. immediate: true,
  130. handler(nVal) {
  131. if (!nVal) nVal = 0;
  132. this.currentIndex = nVal;
  133. this.changeSectionStatus(nVal);
  134. }
  135. },
  136. current: {
  137. immediate: true,
  138. handler(nVal) {
  139. if (!nVal) nVal = this.valueCom || 0;
  140. this.currentIndex = nVal;
  141. this.changeSectionStatus(nVal);
  142. }
  143. },
  144. list: {
  145. deep: true,
  146. handler(nVal = []) {
  147. this.listInfoFn();
  148. setTimeout(() => {
  149. this.getTabsInfo();
  150. }, 10);
  151. }
  152. },
  153. },
  154. computed: {
  155. valueCom() {
  156. // #ifndef VUE3
  157. return this.value;
  158. // #endif
  159. // #ifdef VUE3
  160. return this.modelValue;
  161. // #endif
  162. },
  163. // 设置mode=subsection时,滑块特有的样式
  164. noBorderRight() {
  165. return index => {
  166. if (this.mode != 'subsection') return;
  167. let classs = '';
  168. // 不显示右边的边框
  169. if (index < this.list.length - 1) classs += ' u-none-border-right';
  170. // 显示整个组件的左右边圆角
  171. if (index == 0) classs += ' u-item-first';
  172. if (index == this.list.length - 1) classs += ' u-item-last';
  173. return classs;
  174. };
  175. },
  176. // 文字的样式
  177. textStyle() {
  178. return index => {
  179. let style = {};
  180. // 设置字体颜色
  181. if (this.mode == 'subsection') {
  182. if (index == this.currentIndex) {
  183. style.color = '#ffffff';
  184. } else {
  185. style.color = this.activeColor;
  186. }
  187. } else {
  188. if (index == this.currentIndex) {
  189. style.color = this.activeColor;
  190. } else {
  191. style.color = this.inactiveColor;
  192. }
  193. }
  194. // 字体加粗
  195. if (index == this.currentIndex && this.bold) style.fontWeight = 'bold';
  196. // 文字大小
  197. style.fontSize = this.fontSize + 'rpx';
  198. return style;
  199. };
  200. },
  201. // 每个分段器item的样式
  202. itemStyle() {
  203. return index => {
  204. let style = {};
  205. if (this.mode == 'subsection') {
  206. // 设置border的样式
  207. style.borderColor = this.activeColor;
  208. style.borderWidth = '1px';
  209. style.borderStyle = 'solid';
  210. }
  211. return style;
  212. };
  213. },
  214. // mode=button时,外层view的样式
  215. subsectionStyle() {
  216. let style = {};
  217. // #ifndef APP-HARMONY
  218. style.height = uni.upx2px(this.height) + 'px';
  219. // #endif
  220. // #ifdef APP-HARMONY
  221. style.height = this.height / 2 + 'px';
  222. // #endif
  223. if (this.mode == 'button') {
  224. style.backgroundColor = this.bgColor;
  225. style.padding = `${this.buttonPadding}px`;
  226. style.borderRadius = `${this.borderRadius}px`;
  227. }
  228. return style;
  229. },
  230. // 滑块的样式
  231. itemBarStyle() {
  232. let style = {};
  233. style.backgroundColor = this.activeColor;
  234. style.zIndex = 1;
  235. if (this.mode == 'button') {
  236. style.backgroundColor = this.buttonColor;
  237. style.borderRadius = `${this.borderRadius}px`;
  238. style.bottom = `${this.buttonPadding}px`;
  239. // #ifndef APP-HARMONY
  240. style.height = uni.upx2px(this.height) - this.buttonPadding * 2 + 'px';
  241. // #endif
  242. // #ifdef APP-HARMONY
  243. style.height = (this.height / 2) - this.buttonPadding * 2 + 'px';
  244. // #endif
  245. style.zIndex = 0;
  246. }
  247. return Object.assign(this.itemBgStyle, style);
  248. }
  249. },
  250. mounted() {
  251. this.listInfoFn();
  252. setTimeout(() => {
  253. this.getTabsInfo();
  254. }, 10);
  255. },
  256. methods: {
  257. listInfoFn() {
  258. let {
  259. list = []
  260. } = this;
  261. this.listInfo = this.list.map((val, index) => {
  262. if (typeof val != 'object') {
  263. let obj = {
  264. width: 0,
  265. name: val,
  266. };
  267. return obj;
  268. } else {
  269. return val;
  270. }
  271. });
  272. return this.listInfo;
  273. },
  274. // 改变滑块的样式
  275. changeSectionStatus(nVal) {
  276. if (this.mode == 'subsection') {
  277. // 根据滑块在最左边和最右边时,显示左边和右边的圆角
  278. if (nVal == this.list.length - 1) {
  279. this.itemBgStyle.borderRadius = `0 ${this.buttonPadding}px ${this.buttonPadding}px 0`;
  280. }
  281. if (nVal == 0) {
  282. this.itemBgStyle.borderRadius = `${this.buttonPadding}px 0 0 ${this.buttonPadding}px`;
  283. }
  284. if (nVal > 0 && nVal < this.list.length - 1) {
  285. this.itemBgStyle.borderRadius = '0';
  286. }
  287. }
  288. // 更新滑块的位置
  289. setTimeout(() => {
  290. this.itemBgLeft();
  291. }, 10);
  292. if (this.vibrateShort && !this.firstTimeVibrateShort) {
  293. // 使手机产生短促震动,微信小程序有效,APP(HX 2.6.8)和H5无效
  294. // #ifndef H5
  295. uni.vibrateShort();
  296. // #endif
  297. }
  298. // 第一次过后,设置firstTimeVibrateShort为false,让其下一次可以震动(如果允许震动的话)
  299. this.firstTimeVibrateShort = false;
  300. },
  301. click(index) {
  302. // 不允许点击当前激活选项
  303. if (index == this.currentIndex) return;
  304. this.currentIndex = index;
  305. this.changeSectionStatus(index);
  306. this.$emit('change', Number(index));
  307. this.$emit("input", Number(index));
  308. this.$emit("update:modelValue", Number(index));
  309. },
  310. // 获取各个tab的节点信息
  311. getTabsInfo() {
  312. let view = uni.createSelectorQuery().in(this);
  313. for (let i = 0; i < this.list.length; i++) {
  314. view.select('.u-item-' + i).boundingClientRect();
  315. }
  316. view.exec(res => {
  317. if (!res.length) {
  318. setTimeout(() => {
  319. this.getTabsInfo();
  320. return;
  321. }, 10);
  322. }
  323. // 将分段器每个item的宽度,放入listInfo数组
  324. res.map((val, index) => {
  325. this.listInfo[index].width = val.width;
  326. });
  327. // 初始化滑块的宽度
  328. if (this.mode == 'subsection') {
  329. this.itemBgStyle.width = this.listInfo[0].width + 'px';
  330. } else if (this.mode == 'button') {
  331. this.itemBgStyle.width = this.listInfo[0].width + 'px';
  332. }
  333. // 初始化滑块的位置
  334. this.itemBgLeft();
  335. });
  336. },
  337. itemBgLeft() {
  338. // 根据是否开启动画效果,
  339. if (this.animation) {
  340. this.itemBgStyle.transition = 'all 0.35s';
  341. } else {
  342. this.itemBgStyle.transition = 'all 0s';
  343. }
  344. let left = 0;
  345. // 计算当前活跃item到组件左边的距离
  346. this.listInfo.map((val, index) => {
  347. if (index < this.currentIndex) left += val.width;
  348. });
  349. // 根据mode不同模式,计算滑块需要移动的距离
  350. if (this.mode == 'subsection') {
  351. this.itemBgStyle.left = left + 'px';
  352. } else if (this.mode == 'button') {
  353. this.itemBgStyle.left = left + this.buttonPadding + 'px';
  354. }
  355. }
  356. }
  357. };
  358. </script>
  359. <style lang="scss" scoped>
  360. @import "../../libs/css/style.components.scss";
  361. .u-subsection {
  362. @include vue-flex;
  363. align-items: center;
  364. overflow: hidden;
  365. position: relative;
  366. }
  367. .u-item {
  368. flex: 1;
  369. text-align: center;
  370. font-size: 26rpx;
  371. height: 100%;
  372. @include vue-flex;
  373. align-items: center;
  374. justify-content: center;
  375. color: $u-main-color;
  376. padding: 0 6rpx;
  377. position: relative;
  378. }
  379. .u-item-bg {
  380. background-color: $u-type-primary;
  381. position: absolute;
  382. z-index: -1;
  383. }
  384. .u-none-border-right {
  385. border-right: none !important;
  386. }
  387. .u-item-first {
  388. border-top-left-radius: 8rpx;
  389. border-bottom-left-radius: 8rpx;
  390. }
  391. .u-item-last {
  392. border-top-right-radius: 8rpx;
  393. border-bottom-right-radius: 8rpx;
  394. }
  395. .u-item-text {
  396. transition: all 0.35s;
  397. color: $u-main-color;
  398. @include vue-flex;
  399. align-items: center;
  400. position: relative;
  401. z-index: 3;
  402. }
  403. </style>