当前位置:   article > 正文

uni-app 简单顶部tab切换菜单3—可滑动切换菜单_uniapp tab

uniapp tab

 

顶部tab是可滑动,用scroll-view实现,内容部分比较潦草,可以自行更改,话不多说上代码,最后有完整的代码(复制即可用)

HTML 

  1. <template>
  2. <view>
  3. <!-- tab部分 -->
  4. <swiper class="ct_tab">
  5. <swiper-item :class="{ 'ct_active': index == tabCur }" v-for="(item, index) in tabList" :key="index" class="ct_item" @click="clickCtTab(index)">
  6. <text v-text="item.title"></text>
  7. </swiper-item>
  8. </swiper>
  9. <!-- 内容信息 -->
  10. <view v-if="tabCur===0">
  11. <view>全部信息</view>
  12. </view>
  13. <view v-if="tabCur===1">
  14. <view>水果</view>
  15. </view>
  16. <view v-if="tabCur===2">
  17. <view>蔬菜</view>
  18. </view>
  19. <view v-if="tabCur===3">
  20. <view>调味品</view>
  21. </view>
  22. <view v-if="tabCur===4">
  23. <view>肉类</view>
  24. </view>
  25. <view v-if="tabCur===5">
  26. <view>油类</view>
  27. </view>
  28. <view v-if="tabCur===6">
  29. <view>米类</view>
  30. </view>
  31. <view v-if="tabCur===7">
  32. <view>海鲜</view>
  33. </view>
  34. </view>
  35. </template>
JS

 

  1. <script>
  2. export default {
  3. data() {
  4. return {
  5. tabCur:0,
  6. tabList:[
  7. {
  8. title:'全部',
  9. },{
  10. title:'水果',
  11. },{
  12. title:'蔬菜',
  13. },{
  14. title:'调味品',
  15. },{
  16. title:'肉类',
  17. },{
  18. title:'油类',
  19. },{
  20. title:'米类',
  21. },{
  22. title:'海鲜',
  23. }
  24. ]
  25. }
  26. },
  27. methods: {
  28. clickCtTab(ctCur){
  29. this.tabCur = ctCur
  30. console.log('点击了--->'+this.tabCur)
  31. }
  32. }
  33. }
  34. </script>

CSS

  1. <style>
  2. .ct_tab{
  3. width: 698upx;
  4. height: 100rpx;
  5. margin: 0 auto;
  6. padding: 30upx 0;
  7. font-size: 26upx;
  8. font-weight: bold;
  9. color: #c0c8d0;
  10. white-space: nowrap;
  11. display: flex;
  12. overflow: hidden;
  13. }
  14. .ct_item{
  15. width: 150upx;
  16. padding: 30upx 0;
  17. display: inline-block;
  18. }
  19. .ct_item text{
  20. padding: 30upx 0;
  21. }
  22. .ct_active{
  23. color:#007AFF;
  24. }
  25. .ct_active text{
  26. border-bottom: 2px solid #007AFF;
  27. }
  28. swiper{
  29. width:100%;
  30. }
  31. swiper-item{
  32. width: 150upx !important;
  33. }
  34. </style>

完整代码

  1. <template>
  2. <view>
  3. <!-- tab部分 -->
  4. <swiper class="ct_tab">
  5. <swiper-item :class="{ 'ct_active': index == tabCur }" v-for="(item, index) in tabList" :key="index" class="ct_item" @click="clickCtTab(index)">
  6. <text v-text="item.title"></text>
  7. </swiper-item>
  8. </swiper>
  9. <!-- 内容信息 -->
  10. <view v-if="tabCur===0">
  11. <view>全部信息</view>
  12. </view>
  13. <view v-if="tabCur===1">
  14. <view>水果</view>
  15. </view>
  16. <view v-if="tabCur===2">
  17. <view>蔬菜</view>
  18. </view>
  19. <view v-if="tabCur===3">
  20. <view>调味品</view>
  21. </view>
  22. <view v-if="tabCur===4">
  23. <view>肉类</view>
  24. </view>
  25. <view v-if="tabCur===5">
  26. <view>油类</view>
  27. </view>
  28. <view v-if="tabCur===6">
  29. <view>米类</view>
  30. </view>
  31. <view v-if="tabCur===7">
  32. <view>海鲜</view>
  33. </view>
  34. </view>
  35. </template>
  36. <script>
  37. export default {
  38. data() {
  39. return {
  40. tabCur:0,
  41. tabList:[
  42. {
  43. title:'全部',
  44. },{
  45. title:'水果',
  46. },{
  47. title:'蔬菜',
  48. },{
  49. title:'调味品',
  50. },{
  51. title:'肉类',
  52. },{
  53. title:'油类',
  54. },{
  55. title:'米类',
  56. },{
  57. title:'海鲜',
  58. }
  59. ]
  60. }
  61. },
  62. methods: {
  63. clickCtTab(ctCur){
  64. this.tabCur = ctCur
  65. console.log('点击了--->'+this.tabCur)
  66. }
  67. }
  68. }
  69. </script>
  70. <style>
  71. .ct_tab{
  72. width: 698upx;
  73. height: 100rpx;
  74. margin: 0 auto;
  75. padding: 30upx 0;
  76. font-size: 26upx;
  77. font-weight: bold;
  78. color: #c0c8d0;
  79. white-space: nowrap;
  80. display: flex;
  81. overflow: hidden;
  82. }
  83. .ct_item{
  84. width: 150upx;
  85. padding: 30upx 0;
  86. display: inline-block;
  87. }
  88. .ct_item text{
  89. padding: 30upx 0;
  90. }
  91. .ct_active{
  92. color:#007AFF;
  93. }
  94. .ct_active text{
  95. border-bottom: 2px solid #007AFF;
  96. }
  97. swiper{
  98. width:100%;
  99. }
  100. swiper-item{
  101. width: 150upx !important;
  102. }
  103. </style>

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