[前端] 微信小程序实现tab点击切换

2385 0
王子 2022-10-21 15:33:51 | 显示全部楼层 |阅读模式
本文实例为大家分享了微信小程序无滑动效果的tab点击切换的具体代码,供大家参考,具体内容如下

  1. <!--pages/dingdan/dingdan.wxml-->
  2. <view class="body">
  3. <view class="swiper-tab">
  4.   <view wx:for="{{tabList}}" wx:key="index" catchtap="change" class="{{page==index?'selected-menu':'unselect-menu'}}" data-pageid="{{index}}">{{item.title}}({{item.num}})
  5.    <hr class="{{page==index?'selected-line':'unselect-line'}}" />
  6.   </view>
  7. </view>
  8. <view class="view-Content">
  9.   <view wx:for="{{tabList}}" wx:key="index" class="{{page==index?'show tabCon':'hidden tabCon'}}">
  10.    <view class="content">
  11.     <text>暂无订单{{index}}</text>
  12.    </view>
  13.   </view>
  14. </view>
  15. </view>
复制代码
  1. /* pages/dingdan/dingdan.wxss */
  2. page {
  3. width: 100%;
  4. height: 100%;
  5. overflow: hidden;
  6. /* background: pink; */
  7. }
  8. .body {
  9. height: 100%;
  10. /* background: hotpink; */
  11. display: flex;
  12. flex-direction: column;
  13. }
  14. /* tab栏 */
  15. .swiper-tab {
  16. width: 100%;
  17. height: 80rpx;
  18. text-align: center;
  19. display: flex;
  20. justify-content: space-between;
  21. background: white;
  22. }
  23. .selected-menu {
  24. display: flex;
  25. flex-direction: column;
  26. align-items: center;
  27. color: #ff5050;
  28. background: #fff;
  29. font-size: 32rpx;
  30. font-weight: bold;
  31. font-family: ingFang SC;
  32. font-weight: 400;
  33. width: 33%;
  34. height: 60rpx;
  35. line-height: 60rpx;
  36. opacity: 1;
  37. /* border-bottom: 2px solid #ff5050; */
  38. position: relative;
  39. }
  40. .unselect-menu {
  41. display: flex;
  42. flex-direction: column;
  43. align-items: center;
  44. font-size: 16px;
  45. font-family: PingFang SC;
  46. font-weight: 400;
  47. color: #4f4f50;
  48. width: 33%;
  49. height: 60rpx;
  50. line-height: 60rpx;
  51. background: #fff;
  52. opacity: 1;
  53. position: relative;
  54. /* border-radius: 34rpx; */
  55. }
  56. .selected-line {
  57. background: #ff5050;
  58. height: 4rpx;
  59. width: 90rpx;
  60. position: absolute;
  61. /* margin-top: 10rpx; */
  62. bottom: -18rpx;
  63. width: 60rpx;
  64. }
  65. /* 内容 */
  66. .view-Content {
  67. flex: 1;
  68. overflow-y: auto;
  69. background-color: rgb(236, 236, 236);
  70. }
  71. .tabCon {
  72. height: 100%;
  73. }
  74. /* 展示隐藏 */
  75. .show {
  76. display: block;
  77. }
  78. .hidden {
  79. display: none;
  80. }
复制代码
  1. Page({
  2. /**
  3. * 页面的初始数据
  4. */
  5. data: {
  6.   page: 0, // page:当前页--
  7.   tabList: [{
  8.    title: '菜单1',
  9.    num: 0
  10.   }, {
  11.    title: '菜单2',
  12.    num: 0
  13.   }, {
  14.    title: '菜单3',
  15.    num: 0
  16.   }],
  17. },
  18. // 切换tab
  19. change: function (event) {
  20.   console.log('切换时会调用', event);
  21.   var a = event.currentTarget.dataset.pageid
  22.   this.setData({
  23.    page: a,
  24.   })
  25. },
  26. /**
  27. * 生命周期函数--监听页面加载
  28. */
  29. onLoad: function (options) {
  30. },
  31. /**
  32. * 生命周期函数--监听页面初次渲染完成
  33. */
  34. onReady: function () {
  35. },
  36. /**
  37. * 生命周期函数--监听页面显示
  38. */
  39. onShow: function () {
  40. },
  41. /**
  42. * 生命周期函数--监听页面隐藏
  43. */
  44. onHide: function () {
  45. },
  46. /**
  47. * 生命周期函数--监听页面卸载
  48. */
  49. onUnload: function () {
  50. },
  51. /**
  52. * 页面相关事件处理函数--监听用户下拉动作
  53. */
  54. onPullDownRefresh: function () {
  55. },
  56. /**
  57. * 页面上拉触底事件的处理函数
  58. */
  59. onReachBottom: function () {
  60. },
  61. /**
  62. * 用户点击右上角分享
  63. */
  64. onShareAppMessage: function () {
  65. },
  66. })
复制代码
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持中国红客联盟。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行