u-tabs.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <template>
  2. <view class="u-tabs">
  3. <view class="u-tabs__wrapper">
  4. <slot name="left" />
  5. <view class="u-tabs__wrapper__scroll-view-wrapper">
  6. <scroll-view
  7. :scroll-x="scrollable"
  8. :scroll-left="scrollLeft"
  9. scroll-with-animation
  10. class="u-tabs__wrapper__scroll-view"
  11. :show-scrollbar="false"
  12. ref="u-tabs__wrapper__scroll-view"
  13. >
  14. <view
  15. class="u-tabs__wrapper__nav"
  16. ref="u-tabs__wrapper__nav"
  17. :style="[{
  18. flex: scrollable ? 0 : 1
  19. }]"
  20. >
  21. <view
  22. class="u-tabs__wrapper__nav__item"
  23. v-for="(item, index) in list"
  24. :key="index"
  25. @tap="clickHandler(item, index)"
  26. :ref="`u-tabs__wrapper__nav__item-${index}`"
  27. :style="[$u.addStyle(itemStyle)]"
  28. :class="[`u-tabs__wrapper__nav__item-${index}`, item.disabled && 'u-tabs__wrapper__nav__item--disabled']"
  29. >
  30. <text
  31. :class="['ellipsis' && 'u-line-1', item.disabled && 'u-tabs__wrapper__nav__item__text--disabled']"
  32. class="u-tabs__wrapper__nav__item__text"
  33. :style="[textStyle(index)]"
  34. >{{ item.name }}</text>
  35. <u-badge
  36. :show="!!(item.badge && (item.badge.show || item.badge.isDot || item.badge.value))"
  37. :isDot="item.badge && item.badge.isDot || $u.props.badge.isDot"
  38. :value="item.badge && item.badge.value || $u.props.badge.value"
  39. :max="item.badge && item.badge.max || $u.props.badge.max"
  40. :type="item.badge && item.badge.type || $u.props.badge.type"
  41. :showZero="item.badge && item.badge.showZero || $u.props.badge.showZero"
  42. :bgColor="item.badge && item.badge.bgColor || $u.props.badge.bgColor"
  43. :color="item.badge && item.badge.color || $u.props.badge.color"
  44. :shape="item.badge && item.badge.shape || $u.props.badge.shape"
  45. :numberType="item.badge && item.badge.numberType || $u.props.badge.numberType"
  46. :inverted="item.badge && item.badge.inverted || $u.props.badge.inverted"
  47. customStyle="margin-left: 4px;"
  48. ></u-badge>
  49. </view>
  50. <!-- #ifdef APP-NVUE -->
  51. <view
  52. class="u-tabs__wrapper__nav__line"
  53. ref="u-tabs__wrapper__nav__line"
  54. :style="[{
  55. width: $u.addUnit(this.lineWidth),
  56. height: $u.addUnit(this.lineHeight),
  57. backgroundColor: this.lineColor
  58. }]"
  59. >
  60. <!-- #endif -->
  61. <!-- #ifndef APP-NVUE -->
  62. <view
  63. class="u-tabs__wrapper__nav__line"
  64. ref="u-tabs__wrapper__nav__line"
  65. :style="[{
  66. width: $u.addUnit(this.lineWidth),
  67. transform: `translate(${lineOffsetLeft}px)`,
  68. transitionDuration: `${firstTime ? 0 : duration}ms`,
  69. height: $u.addUnit(this.lineHeight),
  70. backgroundColor: this.lineColor
  71. }]"
  72. >
  73. <!-- #endif -->
  74. </view>
  75. </view>
  76. </scroll-view>
  77. </view>
  78. <slot name="right" />
  79. </view>
  80. </view>
  81. </template>
  82. <script>
  83. // #ifdef APP-NVUE
  84. const animation = uni.requireNativePlugin('animation')
  85. const dom = uni.requireNativePlugin('dom')
  86. // #endif
  87. import props from './props.js';
  88. /**
  89. * Tabs 标签
  90. * @description tabs标签组件,在标签多的时候,可以配置为左右滑动,标签少的时候,可以禁止滑动。 该组件的一个特点是配置为滚动模式时,激活的tab会自动移动到组件的中间位置。
  91. * @tutorial https://www.uviewui.com/components/tabs.html
  92. * @property {String | Number} duration 滑块移动一次所需的时间,单位秒(默认 200 )
  93. * @property {String | Number} swierWidth swiper的宽度(默认 '750rpx' )
  94. * @event {Function(index)} change 点击标签时触发 index: 点击了第几个tab,索引从0开始
  95. * @example <u-tabs :list="list" :is-scroll="false" :current="current" @change="change"></u-tabs>
  96. */
  97. export default {
  98. name: 'u-tabs',
  99. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  100. data() {
  101. return {
  102. firstTime: true,
  103. scrollLeft: 0,
  104. scrollViewWidth: 0,
  105. lineOffsetLeft: 0,
  106. tabsRect: {
  107. left: 0
  108. },
  109. current: 0,
  110. moving: false,
  111. }
  112. },
  113. watch: {
  114. current(newValue, oldValue) {
  115. // this.setLineLeft(newValue)
  116. }
  117. },
  118. computed: {
  119. textStyle() {
  120. return index => {
  121. const style = {}
  122. // 取当期是否激活的样式
  123. const customeStyle = index === this.current ? uni.$u.addStyle(this.activeStyle) : uni.$u.addStyle(
  124. this.inactiveStyle)
  125. // 如果当前菜单被禁用,则加上对应颜色,需要在此做处理,是因为nvue下,无法在style样式中通过!import覆盖标签的内联样式
  126. if (this.list[index].disabled) {
  127. style.color = '#c8c9cc'
  128. }
  129. return uni.$u.deepMerge(customeStyle, style)
  130. }
  131. }
  132. },
  133. async mounted() {
  134. this.init()
  135. },
  136. methods: {
  137. setLineLeft() {
  138. const tabItem = this.list[this.current];
  139. if (!tabItem) {
  140. return;
  141. }
  142. // 获取滑块该移动的位置
  143. let lineOffsetLeft = this.list
  144. .slice(0, this.current)
  145. .reduce((total, curr) => total + curr.rect.width, 0);
  146. this.lineOffsetLeft = lineOffsetLeft + (tabItem.rect.width - this.lineWidth) / 2
  147. // #ifdef APP-NVUE
  148. // 第一次移动滑块,无需过渡时间
  149. this.animation(this.lineOffsetLeft, this.firstTime ? 0 : parseInt(this.duration))
  150. // #endif
  151. // 如果是第一次执行此方法,让滑块在初始化时,瞬间滑动到第一个tab item的中间
  152. // 这里需要一个定时器,因为在非nvue下,是直接通过style绑定过渡时间,需要等其过渡完成后,再设置为false(非第一次移动滑块)
  153. if (this.firstTime) {
  154. setTimeout(() => {
  155. this.firstTime = false
  156. }, 10);
  157. }
  158. },
  159. // nvue下设置滑块的位置
  160. animation(x, duration = 0) {
  161. // #ifdef APP-NVUE
  162. const ref = this.$refs['u-tabs__wrapper__nav__line']
  163. animation.transition(ref, {
  164. styles: {
  165. transform: `translateX(${x}px)`
  166. },
  167. duration
  168. })
  169. // #endif
  170. },
  171. // 点击某一个标签
  172. clickHandler(item, index) {
  173. // 如果disabled状态,返回
  174. if (item.disabled) return
  175. this.current = index
  176. this.resize()
  177. this.$emit('click', {...item, index})
  178. },
  179. init() {
  180. uni.$u.sleep().then(() => {
  181. this.resize()
  182. })
  183. },
  184. setScrollLeft() {
  185. // 当前活动tab的布局信息,有tab菜单的width和left(为元素左边界到父元素左边界的距离)等信息
  186. const tabRect = this.list[this.current]
  187. // 累加得到当前item到左边的距离
  188. const offsetLeft = this.list
  189. .slice(0, this.current)
  190. .reduce((total, curr) => {
  191. return total + curr.rect.width
  192. }, 0)
  193. // 此处为屏幕宽度
  194. const windowWidth = uni.$u.sys().windowWidth
  195. // 将活动的tabs-item移动到屏幕正中间,实际上是对scroll-view的移动
  196. let scrollLeft = offsetLeft - (this.tabsRect.width - tabRect.rect.width) / 2 - (windowWidth - this.tabsRect
  197. .right) / 2 + this.tabsRect.left / 2
  198. // 这里做一个限制,限制scrollLeft的最大值为整个scroll-view宽度减去tabs组件的宽度
  199. scrollLeft = Math.min(scrollLeft, this.scrollViewWidth - this.tabsRect.width)
  200. this.scrollLeft = Math.max(0, scrollLeft)
  201. },
  202. // 获取所有标签的尺寸
  203. resize() {
  204. Promise.all([this.getTabsRect(), this.getAllItemRect()]).then(([tabsRect, itemRect = []]) => {
  205. this.tabsRect = tabsRect
  206. this.scrollViewWidth = 0
  207. itemRect.map((item, index) => {
  208. // 计算scroll-view的宽度,这里
  209. this.scrollViewWidth += item.width
  210. // 另外计算每一个item的中心点X轴坐标
  211. this.list[index].rect = item
  212. })
  213. // 获取了tabs的尺寸之后,设置滑块的位置
  214. this.setLineLeft()
  215. this.setScrollLeft()
  216. })
  217. },
  218. // 获取导航菜单的尺寸
  219. getTabsRect() {
  220. return new Promise(resolve => {
  221. this.queryRect('u-tabs__wrapper__scroll-view').then(size => resolve(size))
  222. })
  223. },
  224. // 获取所有标签的尺寸
  225. getAllItemRect() {
  226. return new Promise(resolve => {
  227. const promiseAllArr = this.list.map((item, index) => this.queryRect(
  228. `u-tabs__wrapper__nav__item-${index}`, true))
  229. Promise.all(promiseAllArr).then(sizes => resolve(sizes))
  230. })
  231. },
  232. // 获取各个标签的尺寸
  233. queryRect(el, item) {
  234. // #ifndef APP-NVUE
  235. // $uGetRect为uView自带的节点查询简化方法,详见文档介绍:https://www.uviewui.com/js/getRect.html
  236. // 组件内部一般用this.$uGetRect,对外的为this.$u.getRect,二者功能一致,名称不同
  237. return new Promise(resolve => {
  238. this.$uGetRect(`.${el}`).then(size => {
  239. resolve(size)
  240. })
  241. })
  242. // #endif
  243. // #ifdef APP-NVUE
  244. // nvue下,使用dom模块查询元素高度
  245. // 返回一个promise,让调用此方法的主体能使用then回调
  246. return new Promise(resolve => {
  247. dom.getComponentRect(item ? this.$refs[el][0] : this.$refs[el], res => {
  248. resolve(res.size)
  249. })
  250. })
  251. // #endif
  252. },
  253. },
  254. }
  255. </script>
  256. <style lang="scss">
  257. @import "../../libs/css/components.scss";
  258. .u-tabs {
  259. &__wrapper {
  260. @include flex;
  261. align-items: center;
  262. &__scroll-view-wrapper {
  263. flex: 1;
  264. /* #ifndef APP-NVUE */
  265. overflow: auto hidden;
  266. /* #endif */
  267. }
  268. &__scroll-view {
  269. @include flex;
  270. flex: 1;
  271. }
  272. &__nav {
  273. @include flex;
  274. &__item {
  275. padding: 0 11px;
  276. @include flex;
  277. align-items: center;
  278. justify-content: center;
  279. flex: 1;
  280. &--disabled {
  281. /* #ifndef APP-NVUE */
  282. cursor: not-allowed;
  283. /* #endif */
  284. }
  285. &__text {
  286. font-size: 15px;
  287. color: $u-content-color;
  288. &--disabled {
  289. color: $u-disabled-color !important;
  290. }
  291. }
  292. }
  293. &__line {
  294. height: 3px;
  295. background-color: $u-primary;
  296. width: 30px;
  297. position: absolute;
  298. bottom: 2px;
  299. border-radius: 100px;
  300. transition-property: transform;
  301. transition-duration: 300ms;
  302. }
  303. }
  304. }
  305. }
  306. </style>