• 总结tab栏切换实现的方法,以及增加滚动实现tab栏切换的效果


    目录

    前言

    第一部分 高亮效果

    效果图

     代码

    第二个部分

    scroll-view + swiper   滑动加点击切换


    前言

    继昨天写了篇简单的tab栏切换,后来,我又想起,其实这个tab栏切换,可以换为两部分,第一部分就是上面的 鼠标的高亮效果,第二部分就是 在第一部分的基础上进行操作的,第二部分也算是写死不动的 

    第一部分 高亮效果

    效果图

     代码

    1. <view class="navTitle" v-for="(item,index) in navList" :key="index">
    2. <view :class="{'active':current === index}" @click="checked(index)">
    3. {{item.title}}
    4. </view>
    5. </view>
    6. data
    7. return {
    8. current: 0,
    9. navList: [{
    10. index: 0,
    11. title: '进行中'
    12. }, {
    13. index: 1,
    14. title: "未支付"
    15. }, {
    16. index: 2,
    17. title: "已完成"
    18. }]
    19. }
    20. methods: {
    21. checked(index) {
    22. this.current = index
    23. },
    24. }

    第二个部分

    就是判断当它点击的下标为哪个,就显示哪个里面的内容

    1. <view class="nav_item" v-if="current==0">
    2. 0
    3. </view>
    4. <view class="nav_item" v-if="current==1">
    5. 1
    6. </view>
    7. <view class="nav_item" v-if="current==2">
    8. 2
    9. </view>

    但我今天就想分享下,tab栏是以滚动的方式(数据多的时候使用)实现切换的

    其实也很简单

    就是在 实现高亮效果的外面加一层 scroll-view 就OK了

    上面都说了,第二部分是通用的,那代码 我想 就不需要 我展示吧

    1. <scroll-view class="scroll-view_H" scroll-x="true" @scroll="scroll">
    2. <view class="scroll-view-item_H" v-for="(tab,index) in tabBars" :key="tab.id"
    3. :id="tab.id"
    4. :class="navIndex==index ? 'activite' : ''"
    5. @click="checked(index)">{{tab.name}}</view>
    6. </scroll-view>
    7. 别忘了 定义 navList 这里面要有很多数据才行
    8. methods: {
    9. checked(index) {
    10. this.current = index
    11. },
    12. scroll(e) {
    13. console.log(e)
    14. this.old.scrollTop = e.detail.scrollTop
    15. },
    16. }
    17. <style scoped>
    18. .activite {
    19. color: #ff0000;
    20. }
    21. .content {
    22. height: 300px;
    23. }
    24. .scroll-view_H {
    25. white-space: nowrap;
    26. width: 100%;
    27. color: #000000;
    28. }
    29. .scroll-view-item_H {
    30. display: inline-block;
    31. width: 20%;
    32. height: 50rpx;
    33. line-height: 50rpx;
    34. text-align: center;
    35. padding: 10px 0;
    36. }
    37. </style>

    效果图

     取消滚动后的效果

     还有一种

    scroll-view + swiper     滑动加 点击切换

    1. <template>
    2. <view>
    3. <!--顶部导航栏-->
    4. <scroll-view class="scroll-view_H" scroll-x="true" @scroll="scroll">
    5. <view class="scroll-view-item_H" v-for="(tab,index) in tabBars" :key="tab.id" :id="tab.id"
    6. :class="navIndex==index ? 'activite' : ''" @tap="checkIndex(index)">
    7. {{tab.name}}
    8. </view>
    9. </scroll-view>
    10. <!-- 内容 -->
    11. <swiper :current="navIndex" @change="tabChange" class="content">
    12. <swiper-item class="swiper_item ">
    13. 1
    14. </swiper-item>
    15. <swiper-item class="swiper_item ">
    16. 2
    17. </swiper-item>
    18. <swiper-item class="swiper_item ">
    19. 3
    20. </swiper-item>
    21. <swiper-item class="swiper_item ">
    22. 4
    23. </swiper-item>
    24. <swiper-item class="swiper_item ">
    25. 5
    26. </swiper-item>
    27. </swiper>
    28. </view>
    29. </template>
    30. 这个需要在 data return里 定义 old: {
    31. scrollTop: 0
    32. }
    33. methods: {
    34. checkIndex(index) {
    35. this.navIndex = index;
    36. console.log(index)
    37. },
    38. scroll(e) {
    39. console.log(e)
    40. this.old.scrollTop = e.detail.scrollTop
    41. },
    42. //滑动切换swiper
    43. tabChange(e) {
    44. const navIndex = e.detail.current
    45. this.navIndex = navIndex
    46. },
    47. }
    48. }
    49. </script>
    50. <style scoped>
    51. .activite {
    52. color: #04c9c3;
    53. }
    54. .content {
    55. height: 700px;
    56. background-color: #04C9C3;
    57. color: #fff;
    58. }
    59. .scroll-view_H {
    60. white-space: nowrap;
    61. width: 100%;
    62. color: #CCCCCC;
    63. }
    64. .scroll-view-item_H {
    65. display: inline-block;
    66. width: 20%;
    67. height: 50rpx;
    68. line-height: 50rpx;
    69. text-align: center;
    70. padding: 10px 0;
    71. font-size: 32rpx;
    72. }
    73. </style>

  • 相关阅读:
    疯狂星期四的营销策略是什么?如何复制?
    这几道SQL面试题秒杀大部分的0年工作经验的毕业生
    关于Google身份验证器、基于时间的一次性密码 (TOTP)算法的初步了解
    C++类与对象 (上)
    PTA 1040 有几个PAT
    第五届“传智杯”全国大学生计算机大赛(练习赛)水题题解
    SQLSERVER查看数据库日志方法和语句示例,已亲测。
    使用共享 MVI 架构实现高效的 Kotlin Multiplatform Mobile (KMM) 开发
    linux 启动springboot项目脚本
    正则表达式语法与应用案例
  • 原文地址:https://blog.csdn.net/LJM51200/article/details/128040319