当前位置:   article > 正文

uniapp组件之tab选项卡滑动切换_uniapp tab

uniapp tab

  效果如下:​​​​​​​

  代码如下: 

  1. <template>
  2. <view class="content">
  3. <view class="nav">
  4. <!-- 选项卡水平方向滑动,scroll-with-animation是滑动到下一个选项时,有一个延时效果 -->
  5. <scroll-view class="tab-scroll" scroll-x="true" scroll-with-animation :scroll-left="scrollLeft">
  6. <view class="tab-scroll_box">
  7. <!-- 选项卡类别列表 -->
  8. <view class="tab-scroll_item" v-for=" (item,index) in category" :key="index" :class="{'active':isActive==index}" @click="chenked(index)">
  9. {{item.name}}
  10. </view>
  11. </view>
  12. </scroll-view>
  13. </view>
  14. <!-- 选项卡内容轮播滑动显示,current为当前第几个swiper子项 -->
  15. <swiper @change="change" :current="isActive" class="swiper-content" :style="fullHeight">
  16. <swiper-item class="swiperitem-content">
  17. <scroll-view scroll-y style="height: 100%;">
  18. <view class="nav_item" >
  19. 选项卡1页面
  20. </view>
  21. </scroll-view>
  22. </swiper-item>
  23. <swiper-item class="swiperitem-content">
  24. <scroll-view scroll-y style="height: 100%;">
  25. <view class="nav_item" >
  26. 选项卡2页面
  27. </view>
  28. </scroll-view>
  29. </swiper-item>
  30. <swiper-item class="swiperitem-content">
  31. <scroll-view scroll-y style="height: 100%;">
  32. <view class="nav_item" >
  33. 选项卡3页面
  34. </view>
  35. </scroll-view>
  36. </swiper-item>
  37. <swiper-item class="swiperitem-content">
  38. <scroll-view scroll-y style="height: 100%;">
  39. <view class="nav_item" >
  40. 选项卡4页面
  41. </view>
  42. </scroll-view>
  43. </swiper-item>
  44. <swiper-item class="swiperitem-content">
  45. <scroll-view scroll-y style="height: 100%;">
  46. <view class="nav_item" >
  47. 选项卡5页面
  48. </view>
  49. </scroll-view>
  50. </swiper-item>
  51. <swiper-item class="swiperitem-content">
  52. <scroll-view scroll-y style="height: 100%;">
  53. <view class="nav_item" >
  54. 选项卡6页面
  55. </view>
  56. </scroll-view>
  57. </swiper-item>
  58. </swiper>
  59. </view>
  60. </template>
  61. <script>
  62. export default {
  63. watch:{
  64. // swiper与上面选项卡联动
  65. currentindex(newval){
  66. this.isActive = newval;
  67. this.scrollLeft = 0;
  68. // 滑动swiper后,每个选项距离其父元素最左侧的距离
  69. for (let i = 0; i < newval - 1; i++) {
  70. this.scrollLeft += this.category[i].width
  71. };
  72. },
  73. },
  74. data() {
  75. return {
  76. isActive: 0,
  77. index: 0,
  78. currentindex:0,
  79. category:[
  80. {
  81. id:1,
  82. name:'选项卡一',
  83. },
  84. {
  85. id:2,
  86. name:'选项卡二',
  87. },
  88. {
  89. id:3,
  90. name:'选项卡三',
  91. },
  92. {
  93. id:4,
  94. name:'选项卡四',
  95. },
  96. {
  97. id:5,
  98. name:'选项卡五',
  99. },
  100. {
  101. id:6,
  102. name:'选项卡六',
  103. },
  104. ],
  105. contentScrollW: 0, // 导航区宽度
  106. scrollLeft: 0, // 横向滚动条位置
  107. fullHeight:"",
  108. }
  109. },
  110. mounted() {
  111. var that = this;
  112. //获取手机屏幕的高度,让其等于swiper的高度,从而使屏幕充满
  113. uni.getSystemInfo({
  114. success: function (res) {
  115. that.fullHeight ="height:" + res.windowHeight + "px";
  116. }
  117. });
  118. // 获取标题区域宽度,和每个子元素节点的宽度
  119. this.getScrollW()
  120. },
  121. methods: {
  122. // 获取标题区域宽度,和每个子元素节点的宽度以及元素距离左边栏的距离
  123. getScrollW() {
  124. const query = uni.createSelectorQuery().in(this);
  125. query.select('.tab-scroll').boundingClientRect(data => {
  126. // 拿到 scroll-view 组件宽度
  127. this.contentScrollW = data.width
  128. }).exec();
  129. query.selectAll('.tab-scroll_item').boundingClientRect(data => {
  130. let dataLen = data.length;
  131. for (let i = 0; i < dataLen; i++) {
  132. // scroll-view 子元素组件距离左边栏的距离
  133. this.category[i].left = data[i].left;
  134. // scroll-view 子元素组件宽度
  135. this.category[i].width = data[i].width
  136. }
  137. }).exec()
  138. },
  139. // 当前点击子元素靠左留一个选项展示,子元素宽度不相同也可实现
  140. chenked(index) {
  141. this.isActive = index;
  142. this.scrollLeft = 0;
  143. for (let i = 0; i < index - 1; i++) {
  144. this.scrollLeft += this.category[i].width
  145. };
  146. },
  147. // swiper滑动时,获取其索引,也就是第几个
  148. change(e){
  149. const {current} = e.detail;
  150. this.currentindex = current;
  151. },
  152. }
  153. }
  154. </script>
  155. <style lang="scss">
  156. page{
  157. height: 100%;
  158. display: flex;
  159. background-color: #FFFFFF;
  160. }
  161. .content{
  162. display: flex;
  163. flex-direction: column;
  164. width: 100%;
  165. flex: 1;
  166. .nav{
  167. border-top: 1rpx solid #f2f2f2;
  168. background-color: #fceeee;
  169. position: fixed;
  170. z-index: 99;
  171. width: 100%;
  172. align-items: center;
  173. height: 100rpx;
  174. .tab-scroll{
  175. flex: 1;
  176. overflow: hidden;
  177. box-sizing: border-box;
  178. padding-left: 30rpx;
  179. padding-right: 30rpx;
  180. .tab-scroll_box{
  181. display: flex;
  182. align-items: center;
  183. flex-wrap: nowrap;
  184. box-sizing: border-box;
  185. .tab-scroll_item{
  186. line-height: 60rpx;
  187. margin-right: 35rpx;
  188. flex-shrink: 0;
  189. padding-bottom:10px;
  190. display: flex;
  191. justify-content: center;
  192. font-size: 16px;
  193. padding-top: 10px;
  194. }
  195. }
  196. }
  197. }
  198. .swiper-content{
  199. padding-top: 120rpx;
  200. flex: 1;
  201. .swiperitem-content{
  202. background-color: #ffffff;
  203. .nav_item{
  204. background-color: #FFFFFF;
  205. padding:20rpx 40rpx 0rpx 40rpx ;
  206. }
  207. }
  208. }
  209. }
  210. .active {
  211. position: relative;
  212. color: #ff0000;
  213. font-weight: 600;
  214. }
  215. .active::after {
  216. content: "";
  217. position: absolute;
  218. width: 130rpx;
  219. height: 4rpx;
  220. background-color: #ff0000;
  221. left: 0px;
  222. right: 0px;
  223. bottom: 0px;
  224. margin: auto;
  225. }
  226. /* 隐藏滚动条,但依旧具备可以滚动的功能 */
  227. /deep/.uni-scroll-view::-webkit-scrollbar {
  228. display: none
  229. }
  230. </style>

注意:css当中需要加上以下,为了隐藏滚动条,否则会出现下图效果

  1. /* 隐藏滚动条,但依旧具备可以滚动的功能 */
  2. /deep/.uni-scroll-view::-webkit-scrollbar {
  3. display: none
  4. }

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

闽ICP备14008679号