u-subsection.vue 9.8 KB

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