u-subsection.vue 9.6 KB

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