u-subsection.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. <template>
  2. <view class="u-subsection" :style="[subsectionStyle]">
  3. <view class="u-item u-line-1" :style="[itemStyle(index)]" @tap="click(index)" :class="[noBorderRight(index), 'u-item-' + index]"
  4. 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. // #ifdef VUE2
  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. style.height = uni.upx2px(this.height) + 'px';
  218. if (this.mode == 'button') {
  219. style.backgroundColor = this.bgColor;
  220. style.padding = `${this.buttonPadding}px`;
  221. style.borderRadius = `${this.borderRadius}px`;
  222. }
  223. return style;
  224. },
  225. // 滑块的样式
  226. itemBarStyle() {
  227. let style = {};
  228. style.backgroundColor = this.activeColor;
  229. style.zIndex = 1;
  230. if (this.mode == 'button') {
  231. style.backgroundColor = this.buttonColor;
  232. style.borderRadius = `${this.borderRadius}px`;
  233. style.bottom = `${this.buttonPadding}px`;
  234. style.height = uni.upx2px(this.height) - this.buttonPadding * 2 + 'px';
  235. style.zIndex = 0;
  236. }
  237. return Object.assign(this.itemBgStyle, style);
  238. }
  239. },
  240. mounted() {
  241. this.listInfoFn();
  242. setTimeout(() => {
  243. this.getTabsInfo();
  244. }, 10);
  245. },
  246. methods: {
  247. listInfoFn(){
  248. let { list =[] } = this;
  249. this.listInfo = this.list.map((val, index) => {
  250. if (typeof val != 'object') {
  251. let obj = {
  252. width: 0,
  253. name: val,
  254. };
  255. return obj;
  256. } else {
  257. return val;
  258. }
  259. });
  260. return this.listInfo;
  261. },
  262. // 改变滑块的样式
  263. changeSectionStatus(nVal) {
  264. if (this.mode == 'subsection') {
  265. // 根据滑块在最左边和最右边时,显示左边和右边的圆角
  266. if (nVal == this.list.length - 1) {
  267. this.itemBgStyle.borderRadius = `0 ${this.buttonPadding}px ${this.buttonPadding}px 0`;
  268. }
  269. if (nVal == 0) {
  270. this.itemBgStyle.borderRadius = `${this.buttonPadding}px 0 0 ${this.buttonPadding}px`;
  271. }
  272. if (nVal > 0 && nVal < this.list.length - 1) {
  273. this.itemBgStyle.borderRadius = '0';
  274. }
  275. }
  276. // 更新滑块的位置
  277. setTimeout(() => {
  278. this.itemBgLeft();
  279. }, 10);
  280. if (this.vibrateShort && !this.firstTimeVibrateShort) {
  281. // 使手机产生短促震动,微信小程序有效,APP(HX 2.6.8)和H5无效
  282. // #ifndef H5
  283. uni.vibrateShort();
  284. // #endif
  285. }
  286. // 第一次过后,设置firstTimeVibrateShort为false,让其下一次可以震动(如果允许震动的话)
  287. this.firstTimeVibrateShort = false;
  288. },
  289. click(index) {
  290. // 不允许点击当前激活选项
  291. if (index == this.currentIndex) return;
  292. this.currentIndex = index;
  293. this.changeSectionStatus(index);
  294. this.$emit('change', Number(index));
  295. this.$emit("input", Number(index));
  296. this.$emit("update:modelValue", Number(index));
  297. },
  298. // 获取各个tab的节点信息
  299. getTabsInfo() {
  300. let view = uni.createSelectorQuery().in(this);
  301. for (let i = 0; i < this.list.length; i++) {
  302. view.select('.u-item-' + i).boundingClientRect();
  303. }
  304. view.exec(res => {
  305. if (!res.length) {
  306. setTimeout(() => {
  307. this.getTabsInfo();
  308. return;
  309. }, 10);
  310. }
  311. // 将分段器每个item的宽度,放入listInfo数组
  312. res.map((val, index) => {
  313. this.listInfo[index].width = val.width;
  314. });
  315. // 初始化滑块的宽度
  316. if (this.mode == 'subsection') {
  317. this.itemBgStyle.width = this.listInfo[0].width + 'px';
  318. } else if (this.mode == 'button') {
  319. this.itemBgStyle.width = this.listInfo[0].width + 'px';
  320. }
  321. // 初始化滑块的位置
  322. this.itemBgLeft();
  323. });
  324. },
  325. itemBgLeft() {
  326. // 根据是否开启动画效果,
  327. if (this.animation) {
  328. this.itemBgStyle.transition = 'all 0.35s';
  329. } else {
  330. this.itemBgStyle.transition = 'all 0s';
  331. }
  332. let left = 0;
  333. // 计算当前活跃item到组件左边的距离
  334. this.listInfo.map((val, index) => {
  335. if (index < this.currentIndex) left += val.width;
  336. });
  337. // 根据mode不同模式,计算滑块需要移动的距离
  338. if (this.mode == 'subsection') {
  339. this.itemBgStyle.left = left + 'px';
  340. } else if (this.mode == 'button') {
  341. this.itemBgStyle.left = left + this.buttonPadding + 'px';
  342. }
  343. }
  344. }
  345. };
  346. </script>
  347. <style lang="scss" scoped>
  348. @import "../../libs/css/style.components.scss";
  349. .u-subsection {
  350. @include vue-flex;
  351. align-items: center;
  352. overflow: hidden;
  353. position: relative;
  354. }
  355. .u-item {
  356. flex: 1;
  357. text-align: center;
  358. font-size: 26rpx;
  359. height: 100%;
  360. @include vue-flex;
  361. align-items: center;
  362. justify-content: center;
  363. color: $u-main-color;
  364. padding: 0 6rpx;
  365. position: relative;
  366. }
  367. .u-item-bg {
  368. background-color: $u-type-primary;
  369. position: absolute;
  370. z-index: -1;
  371. }
  372. .u-none-border-right {
  373. border-right: none !important;
  374. }
  375. .u-item-first {
  376. border-top-left-radius: 8rpx;
  377. border-bottom-left-radius: 8rpx;
  378. }
  379. .u-item-last {
  380. border-top-right-radius: 8rpx;
  381. border-bottom-right-radius: 8rpx;
  382. }
  383. .u-item-text {
  384. transition: all 0.35s;
  385. color: $u-main-color;
  386. @include vue-flex;
  387. align-items: center;
  388. position: relative;
  389. z-index: 3;
  390. }
  391. </style>