综合办公系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

uv-tabs.vue 13KB

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