微信小程序 自定义GIF tabbar 底部导航栏

1.新建tabbar组件

2. wxml组件代码

<view class="tabbar">
    <view class="tabbar-item" wx:for="{{ tabs }}" wx:key="{{index}}" bindtap="switchTab" data-index="{{index}}">
        <image class="tabbar-item-icon" style="width: 50px;height: 40px;" mode="aspectFit" src="{{item.selected ? item.iconPath : item.selectedIconPath }}" class="{{item.selected ? 'selected' : ''}}"></image>
        <view wx:if="{{item.text != ''}}" class="tabbar-item-text" class="{{item.selected ? 'selected' : ''}}">{{item.text}}</view>
    </view>
</view>

注意:因为GIF展示会有缓存影响,需要发GIF放到服务器上 并且在后缀加上随机数或者时间戳进行展示

3.js 代码

// components/tabbar/tabbar.js
Component({
    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        tabs: [
            {
                iconPath: 'https://football.web1995.top/images/0-1.gif',
                selectedIconPath: 'https://football.web1995.top/images/0-2.gif',
                text: '',
                selected: true,
                pagePath: '/pages/home/index'
            },
            {
                iconPath: 'https://football.web1995.top/images/1-1.gif',
                selectedIconPath: 'https://football.web1995.top/images/1-2.gif',
                text: '',
                selected: false,
                pagePath: '/pages/message/index'
            },
            {
                iconPath: 'https://football.web1995.top/images/2-1.gif',
                selectedIconPath: 'https://football.web1995.top/images/2-2.gif',
                text: '',
                selected: false,
                pagePath: '/pages/user/index'
            },
            {
                iconPath: 'https://football.web1995.top/images/3-1.gif',
                selectedIconPath: 'https://football.web1995.top/images/3-2.gif',
                text: '',
                selected: false,
                pagePath: '/pages/user/index'
            },
            {
                iconPath: 'https://football.web1995.top/images/4-1.gif',
                selectedIconPath: 'https://football.web1995.top/images/4-2.gif',
                text: '',
                selected: false,
                pagePath: '/pages/user/index'
            }
        ],
        oldTabs: [
            {
                iconPath: 'https://football.web1995.top/images/0-1.gif',
                selectedIconPath: 'https://football.web1995.top/images/0-2.gif',
                text: '',
                selected: true,
                pagePath: '/pages/home/index'
            },
            {
                iconPath: 'https://football.web1995.top/images/1-1.gif',
                selectedIconPath: 'https://football.web1995.top/images/1-2.gif',
                text: '',
                selected: false,
                pagePath: '/pages/message/index'
            },
            {
                iconPath: 'https://football.web1995.top/images/2-1.gif',
                selectedIconPath: 'https://football.web1995.top/images/2-2.gif',
                text: '',
                selected: false,
                pagePath: '/pages/user/index'
            },
            {
                iconPath: 'https://football.web1995.top/images/3-1.gif',
                selectedIconPath: 'https://football.web1995.top/images/3-2.gif',
                text: '',
                selected: false,
                pagePath: '/pages/user/index'
            },
            {
                iconPath: 'https://football.web1995.top/images/4-1.gif',
                selectedIconPath: 'https://football.web1995.top/images/4-2.gif',
                text: '',
                selected: false,
                pagePath: '/pages/user/index'
            }
        ],
    },

    /**
     * 组件的方法列表
     */
    methods: {
        switchTab(e) {
            const { index } = e.currentTarget.dataset;
            const { tabs, oldTabs } = this.data;
            tabs.forEach((item, idx) => {
                if (index === idx) {
                    item.iconPath = oldTabs[idx].iconPath + '?time=' + Math.random();
                    item.selectedIconPath = oldTabs[idx].selectedIconPath + '?time=' + Math.random();
                    item.selected = true
                } else {
                    item.selected = false
                }
            })

            this.setData({
                tabs
            })
        }
    }
})

有开发问题可以咨询我喔  wx: ai_002 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UniApp 是一个使用 Vue.js 开发跨平台应用的前端框架,它支持将代码一次编写,即可发布到多个平台,包括微信小程序。在 UniApp 中,自定义底部 tabbar 非常简单,只需要在 pages.json 中指定 tabBar 配置,然后在页面中使用自己的自定义 tabbar 组件即可。 以下是自定义底部 tabbar 的步骤: 1. 在 pages.json 文件中设置 tabBar 配置,如下所示: ```javascript { "tabBar": { "custom": true, // 使用自定义tabBar "color": "#7A7E83", "selectedColor": "#3cc51f", "backgroundColor": "#ffffff", "borderStyle": "white", "list": [ { "pagePath": "pages/index/index", "text": "首页", "iconPath": "/static/tabbar/home.png", "selectedIconPath": "/static/tabbar/home-active.png" }, { "pagePath": "pages/user/user", "text": "我的", "iconPath": "/static/tabbar/user.png", "selectedIconPath": "/static/tabbar/user-active.png" } ] } } ``` 2. 在页面中引入自己的自定义 tabbar 组件,并使用该组件替换默认的 tabBar,如下所示: ```javascript <template> <view> <router-view /> <my-tab-bar :list="list" :active.sync="active" /> </view> </template> <script> import MyTabBar from '@/components/MyTabBar.vue' export default { components: { MyTabBar }, data () { return { active: 0, list: [ { text: '首页', icon: '/static/tabbar/home.png', selectedIcon: '/static/tabbar/home-active.png', path: '/pages/index/index' }, { text: '我的', icon: '/static/tabbar/user.png', selectedIcon: '/static/tabbar/user-active.png', path: '/pages/user/user' } ] } } } </script> ``` 以上是自定义底部 tabbar 的简单介绍,如果您需要更详细的内容,请查看 UniApp 的官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值