image.png

    Tabs组件:

    1. <slot/>
    2. <scroll-view class="scroll-view-x" scroll-x >
    3. <view class="scroll-view-item" wx:for="{{tabList}}" wx:key="name">
    4. <view class="{{currentTab === index ? 'on' : ''}}" bind:tap="_switchTab" data-current="{{item.page}}">{{item.name}}</view>
    5. </view>
    6. </scroll-view>
    1. // components/tabs/tabs.js
    2. Component({
    3. /**
    4. * 组件的属性列表
    5. */
    6. properties: {
    7. tabList: {
    8. type: Array,
    9. value: []
    10. },
    11. currentTab: {
    12. type: Number,
    13. value: 0,
    14. observer: function(newVal, oldValue, data){
    15. // 该方法当此数据被setData的时候被调用
    16. console.log(newVal, oldValue, data);
    17. }
    18. }
    19. },
    20. /**
    21. * 组件的初始数据
    22. */
    23. data: {
    24. },
    25. /**
    26. * 组件的方法列表
    27. */
    28. methods: {
    29. _switchTab: function(e){
    30. console.log("_switchTab",e);
    31. this.triggerEvent("changeTab", {
    32. currentNum: e.currentTarget.dataset.current
    33. });
    34. }
    35. }
    36. })
    1. .scroll-view-x {
    2. background-color: #fff;
    3. white-space: nowrap;
    4. /* position: fixed; */
    5. z-index: 10;
    6. top: 0px;
    7. }
    8. .scroll-view-x .scroll-view-item {
    9. display: inline-block;
    10. margin: 0 35rpx;
    11. line-height: 33px;
    12. cursor: pointer;
    13. }
    14. .on{
    15. border-bottom: 2px solid red;
    16. color: red;
    17. }
    1. {
    2. "component": true,
    3. "usingComponents": {}
    4. }

    在页面中使用:

    1. <tabs currentTab="{{currentTab}}" tabList="{{statusType}}" bind:changeTab="switchTab">
    2. <swiper current="{{currentTab}}" duration="300" bindchange="bindChange" style="height:{{windowHeight-35}}px;">
    3. <swiper-item wx:for="{{list}}" wx:key="*this">
    4. <view>{{item}}</view>
    5. </swiper-item>
    6. </swiper>
    7. </tabs>
    1. // pages/componentPage/componentPage.js
    2. Page({
    3. /**
    4. * 页面的初始数据
    5. */
    6. data: {
    7. statusType: [
    8. {
    9. name: "待付款",
    10. page: 0
    11. },
    12. {
    13. name: "待发货",
    14. page: 1
    15. },
    16. {
    17. name: "待收货",
    18. page: 2
    19. },
    20. {
    21. name: "待评价",
    22. page: 3
    23. },
    24. {
    25. name: "已完成",
    26. page: 4
    27. }
    28. ],
    29. currentTab: 0,
    30. list: ["page1", "Page2", "Page3", "Page4", "Page5"],
    31. windowHeight: ''
    32. },
    33. /**
    34. * 生命周期函数--监听页面加载
    35. */
    36. onLoad: function (options) {
    37. this.setData({
    38. windowHeight: wx.getSystemInfoSync().windowHeight
    39. });
    40. },
    41. switchTab: function(event){
    42. console.log("page",event.detail.currentNum);
    43. if (this.data.currentTab === event.detail.currentNum) {
    44. return;
    45. }
    46. this.setData({
    47. currentTab: event.detail.currentNum
    48. });
    49. },
    50. bindChange: function(event){
    51. this.setData({
    52. currentTab: event.detail.current
    53. });
    54. },
    55. /**
    56. * 生命周期函数--监听页面初次渲染完成
    57. */
    58. onReady: function () {
    59. },
    60. /**
    61. * 生命周期函数--监听页面显示
    62. */
    63. onShow: function () {
    64. },
    65. /**
    66. * 生命周期函数--监听页面隐藏
    67. */
    68. onHide: function () {
    69. },
    70. /**
    71. * 生命周期函数--监听页面卸载
    72. */
    73. onUnload: function () {
    74. },
    75. /**
    76. * 页面相关事件处理函数--监听用户下拉动作
    77. */
    78. onPullDownRefresh: function () {
    79. },
    80. /**
    81. * 页面上拉触底事件的处理函数
    82. */
    83. onReachBottom: function () {
    84. },
    85. /**
    86. * 用户点击右上角分享
    87. */
    88. onShareAppMessage: function () {
    89. }
    90. })
    1. {
    2. "usingComponents": { "tabs": "/components/tabs/tabs"}
    3. }