0
点赞
收藏
分享

微信扫一扫

微信小程序丝滑的tab栏

舍予兄 2022-04-03 阅读 82

 

可以左右滑动

index.js

 data: {
        motto: 'Hello World',
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo'), navData: [
            {
                text: '孕产妇'
            },
            {
                text: '婴幼儿'
            },
            {
                text: '中老年人'
            },
            {
           
            {
                text: '传染病'
            },
            {
                text: '精神病'
            },
            {
                text: '职业病'
            },
            {
                text: '老年病'
            },
            {
                text: '疾病'
            }
        ],
        currentTab: 0,
        navScrollLeft: 0,
        windowWidth: wx.getSystemInfoSync().windowWidth,  list: [{ title: " 啊实打实大所大所多", img: "最新!新增确诊病例59例,其中本土50例" },
        { title: " 阿萨大师大大", img: "" },
        ]
    },


 //事件处理函数
    onLoad: function () {
        var that = this;
        //  高度自适应 wx.getSystemInfo({
            success: function (res) {
                var clientHeight = res.windowHeight,
                    clientWidth = res.windowWidth, rpxR = 750 / clientWidth;
                var calc = clientHeight * rpxR - 180;
                console.log(calc)  that.setData({
                    winHeight: calc
                });
            }
        });  },
    switchNav(event) {
        var cur = event.currentTarget.dataset.current;
        //每个tab选项宽度占1/5
        var singleNavWidth = this.data.windowWidth / 5;
        //tab选项居中                            
        this.setData({
            navScrollLeft: (cur - 2) * singleNavWidth
        })
        if (this.data.currentTab == cur) {
            return false;
        } else {
            this.setData({
                currentTab: cur
            })
        }
    }, switchTab(event) {
        console.log(event);
        var cur = event.detail.current; var singleNavWidth = this.data.windowWidth / 5;
        console.log(cur, "----", singleNavWidth, "----", (cur - 2) * singleNavWidth);

        this.setData({  currentTab: cur,
            navScrollLeft: (cur - 2) * singleNavWidth
        });
        this.checkCor();
    }, //判断当前滚动超过一屏时,设置tab标题滚动条。
    checkCor: function () {
        if (this.data.currentTab > 4) {
            this.setData({   scrollLeft: 300
            })
        } else {
            this.setData({  scrollLeft: 0
            })
        }
    },

index.wxcss

page {
    width: 100%;
    height: 100%;
    background-color: #F2F2F2;
}
.container {
    width: 100%;
    height: 100%;
}
.listPar {
    position: relative;
    height: 96rpx;
}

.more {
    position: absolute;
    width: 120rpx;
    height: 96rpx;
    line-height: 96rpx;
    z-index: 100;
    left: 84%;
    /* background-color: linear-gradient( white 90%, 30% ,#ffffff88 10%); */
    background: linear-gradient(to left top, white 20%, rgba(255, 255, 255, 0.8) 80%);
    /* background-color: fuchsia; */
    text-align
}

.moreImg {
    width: 32rpx;
    height: 32rpx;
}

.nav {
    padding-right: 85rpx;
    position: absolute;
    height: 96rpx;
    width: 100%;
    box-sizing: border-box;
    overflow: hidden;
    line-height: 96rpx;
    background: white;
    font-size: 16px;
    white-space: nowrap;
     top: 0;
    left: 0;
    z-index: 99;
}

.nav-item {
    margin: 0rpx 34rpx;
    display: inline-block;
    text-align: center;
    line-height: 86rpx;
    font-size: 28rpx;
    font-weight: 400;
    color: #999999;
}

.nav-item.active {
    font-size: 32rpx;
    font-weight: 500;
    color: #316CEC;
}
.nav-line {
    margin: 0rpx 20rpx;
    height: 0px;
}

.nav-line.active {
    height: 4rpx;
    background: #316CEC;
    border-radius: 8px;
}
.nav-item.active:after {
    content: "";
    display: block;
    position: absolute;
    bottom: 0;
    left: 5rpx;
    border-radius: 16rpx;
}
.tab-box {
    background: #F2F2F2;
    height: 100%;
    box-sizing: border-box;
}
.tab-content {
    overflow-y: scroll;
}
.listBg {
    margin: 24rpx 32rpx 0rpx 32rpx;
    background-color: white;
    display: flex;
    flex-direction: column;
}
.itemImg {
    height: 320rpx;
    width: 100%;
}

.itemTitle {
    margin: 24rpx;
    font-size: 28rpx;
    font-weight: 500;
    line-height: 48rpx;
    color: #333333;
}

index.wxml

<view class="container"> 
  <view class="listPar">
  <scroll-view scroll-x="true" class="nav" scroll-left="{{navScrollLeft}}" scroll-with-animation="{{true}}">
              <block wx:for="{{navData}}" wx:for-index="idx" wx:for-item="navItem" wx:key="idx" bindtap="switchNav">
                 <view class="nav-item {{currentTab == idx ?'active':''}}" data-current="{{idx}}" bindtap="switchNav">
                       <view>{{navItem.text}}</view>
                    <view class="nav-line {{currentTab == idx ?'active':''}}"></view>
                </view>
            </block>
        </scroll-view>
   <navigator class="more" bindtap="moreTab" url="./more">
            <image class="moreImg" src="../../../image/icon8.png"></image>
        </navigator>
 </view> 
   <!-- 页面内容 -->
    <swiper class="tab-box" current="{{currentTab}}" duration="200" bindchange="switchTab">
           <swiper-item wx:for="{{navData}}" wx:for-item="navItem" wx:for-index="idx" wx:key="idx" class="tab-content">
     <navigator class="listBg" wx:for="{{list}}" wx:key="index" url="./detailed">
                <image wx:if="{{item.img}}" class="itemImg" src="../../../image/icon17.png"></image>
                      <text class="itemTitle">{{item.title}}</text>
            </navigator>
   </swiper-item>
    </swiper>
</view>
举报

相关推荐

0 条评论