uniapp底部导航封装

1、封装底部导航栏

<template>
    <view class="all">
        <view :class="{active : types!==index}" v-for='(item,index) in tabberlist' :key="index"
            @click="color(index);jump(item.path)">
            <text>{{item.title}}</text>
        </view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                tabberlist: [{
                        title: '首页',
                        path: "/pages/index/index"
                    },
                    {
                        title: '我的',
                        path: '/pages/pageTwo/pageTwo'
                    }
                ],
                types: 0,
            }
        },
    methods: {
        color(index) {
            this.types = index;
            console.log(this.types);
        },
        jump(path) {
            uni.navigateTo({
                url : path
            })
        }
    }
}
</script>

<style>
    .all {
        width: 100%;
        height: 50px;
        line-height: 50px;
        border: 1px solid #eeeeee;
        display: flex;
        position: fixed;
        bottom: 0;
        justify-content: space-around;
    }

    .active {
        color: #ac29c8;
    }
</style>

2、在页面里引入

<template>
	<view class="content">
		<image class="logo" src="/static/logo.png"></image>
		<view class="text-area">
			<text class="title">{{title}}</text>
		</view>
	<TabBar></TabBar>
	</view>
</template>
<script>
	import TabBar from '../../components/tabBar.vue'
	export default {
		components: {
			TabBar
		},
		data() {
			return {
				title: 'Hello'
			}
		},
		onLoad() {
		},
		methods: {

		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>

3、效果图

 注意:需要在pages.json里面取消默认的导航栏。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用uniapp的自定义导航栏组件来实现自适应底部导航栏。以下是一个简单的示例代码: 1. 创建一个名为`CustomTabBar`的自定义组件: ```vue <template> <view class="custom-tab-bar"> <view v-for="(item, index) in tabList" :key="index" class="tab-item" @click="switchTab(index)"> <image :src="selectedTab === index ? item.selectedIcon : item.icon" class="tab-icon"></image> <text :class="selectedTab === index ? 'active' : ''">{{ item.text }}</text> </view> </view> </template> <script> export default { props: { tabList: { type: Array, default: () => [] }, selectedTab: { type: Number, default: 0 } }, methods: { switchTab(index) { this.$emit('switchTab', index); } } } </script> <style scoped> .custom-tab-bar { position: fixed; bottom: 0; left: 0; right: 0; height: 50px; background-color: #F5F5F5; display: flex; justify-content: space-around; align-items: center; } .tab-item { display: flex; flex-direction: column; align-items: center; } .tab-icon { width: 24px; height: 24px; } .active { color: #007AFF; } </style> ``` 2. 在页面中使用自定义导航栏组件: ```vue <template> <view> <!-- 页面内容 --> <view class="content"> <!-- 页面内容 --> </view> <!-- 自定义底部导航栏 --> <custom-tab-bar :tabList="tabList" :selectedTab="selectedTab" @switchTab="switchTab"></custom-tab-bar> </view> </template> <script> import CustomTabBar from '@/components/CustomTabBar.vue'; // 导入自定义导航栏组件 export default { components: { CustomTabBar }, data() { return { tabList: [ { text: '首页', icon: '/static/tabbar/home.png', selectedIcon: '/static/tabbar/home-active.png' }, { text: '分类', icon: '/static/tabbar/category.png', selectedIcon: '/static/tabbar/category-active.png' }, { text: '我的', icon: '/static/tabbar/mine.png', selectedIcon: '/static/tabbar/mine-active.png' } ], selectedTab: 0 }; }, methods: { switchTab(index) { this.selectedTab = index; } } } </script> ``` 此示例代码中,`CustomTabBar`组件用于展示底部导航栏,通过传递`tabList`和`selectedTab`属性来渲染导航栏的选项卡和当前选中的选项卡。点击选项卡时,触发`switchTab`事件来更新选中的选项卡。 您可以根据自己的需求修改样式和图标路径,以实现您想要的自适应底部导航栏效果。希望对您有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值