微信小程序丝滑的tab栏

简介: 微信小程序丝滑的tab栏

微信小程序丝滑的tab栏

image.png

可以左右滑动


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>
目录
相关文章
|
JSON 小程序 JavaScript
微信小程序|Tab标签页
微信小程序|Tab标签页
298 0
|
2月前
|
小程序 JavaScript 前端开发
【微信小程序-原生开发】实用教程06-轮播图、分类页签 tab 、成员列表(含Tdesign升级,切换调试基础库,设置全局样式,配置组件按需注入,添加图片素材,wx:for,生命周期 onLoad)
【微信小程序-原生开发】实用教程06-轮播图、分类页签 tab 、成员列表(含Tdesign升级,切换调试基础库,设置全局样式,配置组件按需注入,添加图片素材,wx:for,生命周期 onLoad)
67 0
|
10月前
|
小程序 JavaScript
微信小程序模仿分类导航实现多个tab 页
微信小程序模仿分类导航实现多个tab 页
48 0
|
10月前
|
小程序 JavaScript
小程序点击轮播图跳转到tab导航界面
小程序点击轮播图跳转到tab导航界面
89 0
|
10月前
|
小程序 JavaScript
微信小程序实现tab页面切换功能
微信小程序实现tab页面切换功能
68 0
|
小程序 索引
【微信小程序】页面tab栏与页面内容联动
我们可以将整个页面考虑成一个轮播图,tab栏就如同轮播图的那小点点,可以控制网那个页面点击,有了思路就非常的好实现了,这时候就需要阅读官网,了解那个属性可以跟轮播图绑定,从而实现点击滚动,滑动也可以使tab栏随之滚动。
419 1
【微信小程序】页面tab栏与页面内容联动
|
小程序 索引
【微信小程序】实现页面tab切换效果
通过以下的方法实现tab与页面内容联动。
400 0
【微信小程序】实现页面tab切换效果
|
28天前
|
小程序 JavaScript Java
微信小程序的后端开发需要使用什么语言?
【8月更文挑战第22天】微信小程序的后端开发需要使用什么语言?
255 65
|
21天前
|
小程序 JavaScript
Taro@3.x+Vue@3.x+TS开发微信小程序,使用轮播图
本文介绍了使用 Taro 和 Vue 创建轮播组件的两种方法:一是通过 `&lt;swiper&gt;` 实现,二是利用 Nut UI 的 `&lt;nut-swiper&gt;` 组件实现。
Taro@3.x+Vue@3.x+TS开发微信小程序,使用轮播图
|
7天前
|
存储 移动开发 监控
微信支付开发避坑指南
【9月更文挑战第11天】在进行微信支付开发时,需遵循官方文档,确保权限和参数配置正确。开发中应注重安全,验证用户输入,合理安排接口调用顺序,并处理异常。上线后需实时监控支付状态,定期检查配置,关注安全更新,确保系统稳定运行。

热门文章

最新文章