tn-subsection.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. <template>
  2. <view
  3. class="tn-subsection-class tn-subsection"
  4. :class="[subsectionBackgroundColorClass]"
  5. :style="[subsectionStyle]"
  6. >
  7. <!-- 滑块 -->
  8. <block v-for="(item, index) in listInfo" :key="index">
  9. <view
  10. class="tn-subsection__item tn-text-ellipsis"
  11. :class="[
  12. 'section-item-' + index,
  13. noBorderRight(index)
  14. ]"
  15. :style="[itemStyle(index)]"
  16. @tap="click(index)"
  17. >
  18. <view class="tn-subsection__item--text tn-text-ellipsis" :style="[textStyle(index)]">
  19. {{ item.name }}
  20. </view>
  21. </view>
  22. </block>
  23. <!-- 背景 -->
  24. <view
  25. class="tn-subsection__bg"
  26. :class="[itemBarClass]"
  27. :style="[itemBarStyle]"
  28. ></view>
  29. </view>
  30. </template>
  31. <script>
  32. import componentsColorMixin from '../../libs/mixin/components_color.js'
  33. export default {
  34. mixins: [componentsColorMixin],
  35. name: 'tn-subsection',
  36. props: {
  37. // 模式选择
  38. // button 按钮模式 subsection 分段模式
  39. mode: {
  40. type: String,
  41. default: 'subsection'
  42. },
  43. // 组件高度
  44. height: {
  45. type: Number,
  46. default: 60
  47. },
  48. // tab的数据
  49. list: {
  50. type: Array,
  51. default() {
  52. return []
  53. }
  54. },
  55. // 当前活动tab的index
  56. current: {
  57. type: [Number, String],
  58. default: 0
  59. },
  60. // 激活时的字体颜色
  61. activeColor: {
  62. type: String,
  63. default: '#FFFFFF'
  64. },
  65. // 未激活时的字体颜色
  66. inactiveColor: {
  67. type: String,
  68. default: '#AAAAAA'
  69. },
  70. // 激活tab的字体是否加粗
  71. bold: {
  72. type: Boolean,
  73. default: false
  74. },
  75. backgroundColor: {
  76. type: String,
  77. default: '#F4F4F4'
  78. },
  79. // 滑块的颜色
  80. buttonColor: {
  81. type: String,
  82. default: '#01BEFF'
  83. },
  84. // 当mode为button时生效,圆角的值,单位rpx
  85. borderRadius: {
  86. type: Number,
  87. default: 10
  88. },
  89. // 是否开启动画
  90. animation: {
  91. type: Boolean,
  92. default: true
  93. },
  94. // 动画类型
  95. // cubic-bezier -> 贝塞尔曲线
  96. animationType: {
  97. type: String,
  98. default: ''
  99. },
  100. // 滑动滑块的是否,是否触发震动
  101. vibrateShort: {
  102. type: Boolean,
  103. default: false
  104. }
  105. },
  106. data() {
  107. return {
  108. // 列表数据
  109. listInfo: [],
  110. // 子元素的背景样式
  111. itemBgStyle: {
  112. width: 0,
  113. left: 0,
  114. backgroundColor: '#ffffff',
  115. height: '100%'
  116. },
  117. // 当前选中的滑块
  118. currentIndex: this.current,
  119. buttonPadding: 3,
  120. // 组件初始化的是否current变换不应该震动
  121. firstVibrateShort: true
  122. }
  123. },
  124. watch: {
  125. current: {
  126. handler(val) {
  127. this.currentIndex = val
  128. this.changeSectionStatus(val)
  129. },
  130. immediate: true
  131. }
  132. },
  133. created() {
  134. // 将list的数据,传入listInfo数组
  135. // 接受直接数组形式,或者数组元素为对象的形式,如:['开启', '关闭'],或者[{name: '开启'}, {name: '关闭'}]
  136. this.listInfo = this.list.map((item, index) => {
  137. if (typeof item !== 'object') {
  138. let obj = {
  139. width: 0,
  140. name: item
  141. }
  142. return obj
  143. } else {
  144. item.width = 0
  145. return obj
  146. }
  147. })
  148. },
  149. computed: {
  150. // 设置mode=subsection时,滑块没有样式
  151. noBorderRight() {
  152. return index => {
  153. if (this.mode !== 'subsection') return
  154. let clazz = ''
  155. // 不显示右边的边距
  156. if (index < this.list.length - 1) clazz += ' tn-subsection__item--none-border-right'
  157. // 显示整个组件的左右边圆角
  158. if (index === 0) clazz += ' tn-subsection__item--first'
  159. if (index === this.list.length - 1) clazz += ' tn-subsection__item--last'
  160. return clazz
  161. }
  162. },
  163. // 文字的样式
  164. textStyle() {
  165. return index => {
  166. let style = {}
  167. // 设置字体颜色
  168. if (index === this.currentIndex) {
  169. style.color = this.activeColor
  170. } else {
  171. style.color = this.inactiveColor
  172. }
  173. // 字体加粗
  174. if (index === this.currentIndex && this.bold) style.fontWeight = 'bold'
  175. // 文字大小
  176. style.fontSize = (this.fontSize || 26) + this.fontUnit
  177. return style
  178. }
  179. },
  180. // 每个分段器item的样式
  181. itemStyle() {
  182. return index => {
  183. let style = {}
  184. if (this.fontSizeStyle) {
  185. style.fontSize = this.fontSizeStyle
  186. }
  187. if (this.mode === 'subsection') {
  188. // 设置border的样式
  189. style.borderColor = this.buttonColor
  190. style.borderWidth = '1rpx'
  191. style.borderStyle = 'solid'
  192. }
  193. return style
  194. }
  195. },
  196. // mode = button时,设置外层view的样式
  197. subsectionStyle() {
  198. let style = {}
  199. style.height = this.height + 'rpx'
  200. if (this.mode === 'button') {
  201. style.backgroundColor = this.backgroundColorStyle
  202. style.padding = `${this.buttonPadding}px`
  203. style.borderRadius = `${this.borderRadius}rpx`
  204. }
  205. return style
  206. },
  207. // mode = button时,设置外层view的背景class
  208. subsectionBackgroundColorClass() {
  209. let clazz = ''
  210. if (this.mode === 'button' && this.backgroundColorClass) {
  211. clazz = this.backgroundColorClass
  212. }
  213. return clazz
  214. },
  215. itemBarClass() {
  216. let clazz = ''
  217. const buttonBgClass = this.$t.color.getBackgroundColorInternalClass(this.buttonColor)
  218. if (this.animation) {
  219. clazz += ' tn-subsection__bg__animation'
  220. if (this.animationType) {
  221. clazz += ` tn-subsection__bg__animation--${this.animationType}`
  222. }
  223. }
  224. if (buttonBgClass) {
  225. clazz += ` ${buttonBgClass}`
  226. }
  227. return clazz
  228. },
  229. // 滑块样式
  230. itemBarStyle() {
  231. let style = {}
  232. const buttonBgStyle = this.$t.color.getBackgroundColorStyle(this.buttonColor)
  233. if (buttonBgStyle) {
  234. style.backgroundColor = this.buttonColor
  235. }
  236. style.zIndex = 1
  237. if (this.mode === 'button') {
  238. style.borderRadius = `${this.borderRadius}rpx`
  239. style.bottom = `${this.buttonPadding}px`
  240. style.height = (this.height - (this.buttonPadding * 4)) + 'rpx'
  241. style.zIndex = 0
  242. }
  243. return Object.assign(this.itemBgStyle, style)
  244. }
  245. },
  246. mounted() {
  247. // 等待加载组件完成
  248. setTimeout(() => {
  249. this.getTabsInfo()
  250. }, 10)
  251. },
  252. methods: {
  253. // 改变滑块样式
  254. changeSectionStatus(val) {
  255. if (this.mode === 'subsection') {
  256. // 根据滑块在最左和最右时,显示对应的圆角
  257. if (val === this.list.length - 1) {
  258. this.itemBgStyle.borderRadius = `0 ${this.buttonPadding}px ${this.buttonPadding}px 0`
  259. }
  260. if (val === 0) {
  261. this.itemBgStyle.borderRadius = `${this.buttonPadding}px 0 0 ${this.buttonPadding}px`
  262. }
  263. if (val > 0 && val < this.list.length - 1) {
  264. this.itemBgStyle.borderRadius = '0'
  265. }
  266. }
  267. // 更新滑块的位置
  268. setTimeout(() => {
  269. this.itemBgLeft()
  270. }, 10)
  271. if (this.vibrateShort && !this.firstVibrateShort) {
  272. // 使手机产生短促震动,微信小程序有效,APP(HX 2.6.8)和H5无效
  273. // #ifndef H5
  274. uni.vibrateShort();
  275. // #endif
  276. }
  277. this.firstVibrateShort = false
  278. },
  279. // 获取各个tab的节点信息
  280. getTabsInfo() {
  281. let view = uni.createSelectorQuery().in(this)
  282. for (let i = 0; i < this.list.length; i++) {
  283. view.select('.section-item-' + i).boundingClientRect()
  284. }
  285. view.exec(res => {
  286. // 如果没有获取到,则重新获取
  287. if (!res.length) {
  288. setTimeout(() => {
  289. this.getTabsInfo()
  290. return
  291. }, 10)
  292. }
  293. // 将每个分段器的宽度放入listInfo中
  294. res.map((item, index) => {
  295. this.listInfo[index].width = item.width
  296. })
  297. // 初始化滑块的宽度
  298. if (this.mode === 'subsection') {
  299. this.itemBgStyle.width = this.listInfo[0].width + 'px'
  300. } else if (this.mode === 'button') {
  301. this.itemBgStyle.width = this.listInfo[0].width + 'px'
  302. }
  303. // 初始化滑块的位置
  304. this.itemBgLeft()
  305. })
  306. },
  307. // 设置滑块的位置
  308. itemBgLeft() {
  309. let left = 0
  310. // 计算当前活跃item到组件左边的距离
  311. this.listInfo.map((item, index) => {
  312. if (index < this.currentIndex) left += item.width
  313. })
  314. // 根据不同的模式,计算滑块的位置
  315. if (this.mode === 'subsection') {
  316. this.itemBgStyle.left = left + 'px'
  317. } else if (this.mode === 'button') {
  318. this.itemBgStyle.left = left + this.buttonPadding + 'px'
  319. }
  320. },
  321. // 点击事件
  322. click(index) {
  323. // 不允许点击当前激活的选项
  324. if (index === this.currentIndex) return
  325. this.currentIndex = index
  326. this.changeSectionStatus(index)
  327. this.$emit('change', {
  328. index: Number(index),
  329. name: this.listInfo[index]['name']
  330. })
  331. }
  332. }
  333. }
  334. </script>
  335. <style lang="scss" scoped>
  336. .tn-subsection {
  337. /* #ifndef APP-PLUS */
  338. display: flex;
  339. flex-direction: row;
  340. /* #endif */
  341. align-items: center;
  342. overflow: hidden;
  343. position: relative;
  344. &__item {
  345. /* #ifndef APP-PLUS */
  346. display: flex;
  347. flex-direction: row;
  348. /* #endif */
  349. flex: 1;
  350. text-align: center;
  351. font-size: 26rpx;
  352. height: 100%;
  353. align-items: center;
  354. justify-content: center;
  355. color: #FFFFFF;
  356. padding: 0 6rpx;
  357. &--text {
  358. transition: all 0.3s;
  359. color: #FFFFFF;
  360. /* #ifndef APP-PLUS */
  361. display: flex;
  362. flex-direction: row;
  363. /* #endif */
  364. align-items: center;
  365. position: relative;
  366. z-index: 3;
  367. }
  368. &--first {
  369. border-top-left-radius: 8rpx;
  370. border-bottom-left-radius: 8rpx;
  371. }
  372. &--last {
  373. border-top-right-radius: 8rpx;
  374. border-bottom-right-radius: 8rpx;
  375. }
  376. &--none-border-right {
  377. border-right: none !important;
  378. }
  379. }
  380. &__bg {
  381. background-color: $tn-main-color;
  382. position: absolute;
  383. z-index: -1;
  384. transition-property: all;
  385. transition-duration: 0s;
  386. transition-timing-function: linear;
  387. &__animation {
  388. transition-duration: 0.25s !important;
  389. &--cubic-bezier {
  390. transition-timing-function: cubic-bezier(0.68, -0.55, 0.265, 1.55) !important;
  391. }
  392. }
  393. }
  394. }
  395. </style>