当前位置:   article > 正文

uniapp 实现tab栏切换时内容自动滚动到相对应的位置,页面滚动时tab栏自动变化_使用u-tabs实现滚动

使用u-tabs实现滚动

业务需求如下

代码如下

  1. <template>
  2. <div class="slidertab">
  3. <!-- 滑动的tab -->
  4. <div class="u_tabs">
  5. <!-- -->
  6. <div
  7. class="tab_item"
  8. v-for="(item, index) in list1"
  9. :key="index"
  10. @click="changeScrollTop(item, index)"
  11. :class="{ active: currentActive === index }"
  12. >
  13. {{ item.name }}
  14. </div>
  15. </div>
  16. <!-- 内容滑动的时候动态更改 -->
  17. <div class="u_contetns">
  18. <div class="items" v-for="(item, index) in list1" :key="index">
  19. <div :class="'item' + index" style="height:200px">
  20. <h2>{{ item.val }}</h2>
  21. </div>
  22. </div>
  23. </div>
  24. </div>
  25. </template>
  26. <script>
  27. export default {
  28. data () {
  29. return {
  30. list1: [
  31. {
  32. name: '关注',
  33. val: '关注'
  34. },
  35. {
  36. name: '推荐',
  37. val: '推荐'
  38. },
  39. {
  40. name: '电影',
  41. val: '电影'
  42. },
  43. {
  44. name: '科技',
  45. val: '科技'
  46. },
  47. {
  48. name: '音乐',
  49. val: '音乐'
  50. },
  51. {
  52. name: '美食',
  53. val: '美食'
  54. },
  55. {
  56. name: '文化',
  57. val: '文化'
  58. },
  59. {
  60. name: '财经',
  61. val: '财经'
  62. },
  63. {
  64. name: '手工',
  65. val: '手工'
  66. }
  67. ],
  68. preActive: 0,
  69. currentActive: 0,
  70. // 为了解决点击滑动的bug,点击的时候,滑动里面不处理任何逻辑
  71. isClick: false
  72. }
  73. },
  74. onPageScroll (obj) {
  75. const { scrollTop } = obj
  76. this.scrollTochang(scrollTop)
  77. },
  78. mounted () {
  79. this.$nextTick(() => {
  80. // 此时把每个dom的距离拿出来
  81. this.list1.forEach((item, index) => {
  82. uni
  83. .createSelectorQuery()
  84. .select('.u_contetns')
  85. .boundingClientRect(data => {
  86. //目标节点、也可以是最外层的父级节点
  87. uni
  88. .createSelectorQuery()
  89. .select(`.item${index}`)
  90. .boundingClientRect(res => {
  91. //最外层盒子节点
  92. item.scrollTop = res.top - data.top
  93. })
  94. .exec()
  95. })
  96. .exec()
  97. })
  98. })
  99. },
  100. methods: {
  101. changeScrollTop (item, index) {
  102. this.isClick = true
  103. const that = this
  104. const { scrollTop } = item
  105. this.currentActive = index
  106. uni.pageScrollTo({
  107. duration: 80 * Math.abs(this.currentActive - this.preActive),
  108. scrollTop,
  109. complete () {
  110. that.isClick = false
  111. }
  112. })
  113. this.preActive = this.currentActive
  114. },
  115. scrollTochang (scrollTop) {
  116. if (this.isClick) return
  117. const idx = this.list1.findIndex(item => scrollTop <= item.scrollTop)
  118. if (this.currentActive === idx) return
  119. this.currentActive = idx
  120. this.preActive = this.currentActive
  121. }
  122. },
  123. options: { styleIsolation: 'shared' }
  124. }
  125. </script>
  126. <style lang="scss" scoped>
  127. .slidertab {
  128. background: #f5f9fd;
  129. border-radius: 16px 16px 0px 0px;
  130. padding: 0 32px;
  131. .u_tabs {
  132. position: sticky;
  133. top: 0;
  134. z-index: 10;
  135. display: flex;
  136. background-color: #fff;
  137. .tab_item {
  138. flex: 1;
  139. background-color: #fff;
  140. line-height: 100px;
  141. &.active {
  142. background-color: rgb(55, 185, 169);
  143. }
  144. }
  145. }
  146. }
  147. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/103168
推荐阅读
相关标签
  

闽ICP备14008679号