• 总结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>

  • 相关阅读:
    Android开发-Mac Android开发环境搭建(Android Studio Mac环境详细安装教程,适合新手)...
    微信怎么开直播卖货你知道吗?
    【Mongo|1】MongoDB常用命令详细介绍
    自定义镜像运行Nginx及Java服务并基于NAS实现动静分离
    java-net-php-python-jspm广西壮家缘食府餐饮计算机毕业设计程序
    Java开发 - Redis常见问题场景及解决办法一览
    传智教育 | Java教程:feature分支开发到一半时切换到bugfix分支,如何暂存数据?
    百度搜索首届技术创新挑战赛 丰厚奖励等你拿!
    金仓数据库 KingbaseGIS 使用手册(8.12. 栅格运算符、8.13. 栅格和栅格波段空间关系函数)
    nginx安装和使用
  • 原文地址:https://blog.csdn.net/LJM51200/article/details/128040319