uniapp微信小程序导航栏切换动画

22 篇文章 0 订阅
8 篇文章 0 订阅

        开发的时候做导航栏切换。很正常的事情。但有时候为了我们的产品更加的完美会去要求我们前端在做导航栏切换的时候的动画效果。
        如下:

        具体效果  大家可以自己复制代码之后看

        因为我做的是一个组件并且是用uniapp做的所有大家在自己的uniapp里面创建一个vue文件然后粘贴进去就好了。
如果自己做的是微信小程序原生开发或者是H5 或者做后台管理的

 要用  大家复制一下根据自己需求去修改就好了

<template>
	<view class="team-pact">
		<view :style="{color:pitchon==item?'#444':'#999'}" class="segmented-item" :key="index"
			v-for="(item,index) in titleList" :id="'iconBox'+index">
			<view class="cont-box">
				<text @click="itemClick(item,index)" :class="pitchon==item?'subcolor':''">{{item}}</text>
			</view>
		</view>
		<text :style="[{left:left}]" class="subscript"></text>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				pitchon: '',
				left: '',
			}
		},

		// 父组件传递的参数
		// titleList: ['导航1', '导航2','导航3'],
		// pitchon:'导航1'

		// props: ["title", "back"],
		props: {
			pitchon: { //默认选中的内容 
				type: String,
				default: '',
			},
			titleList: { //标题列表
				type: Array,
				default: []
			},
			color: { // 标题和返回按钮颜色(默认白色)
				type: String,
				default: '#fff'
			},
			//建议使用background  因为使用backgroundColor,会导致不识别渐变颜色
			background: { // 背景颜色(不传值默认透明)
				type: String,
				default: 'transparent'
			},

		},

		created() {

			let index
			this.titleList.map((item, ind) => {
				if (item == this.pitchon) {
					index = ind
				}
			})


			this.move(index)

		},
		methods: {
			// 左侧返回按钮调用
			itemClick(item, index) {
				console.log(item, index);


				this.move(index)

				this.$emit("itemClick", { item, index })
				// this.left = left * 2
			},


			move(index) {
				let _this = this

				const query = uni.createSelectorQuery().in(this)
				query.select(`#iconBox${index}`).boundingClientRect(data => {
					console.log(data.left)
					console.log(data)

					//  移动距离 = 当前盒子距离屏幕左边距离  +  (当前盒子宽度 - 滑动条宽度 )除2 (注意这里是px)
					_this.left = data.left + (data.width - 20) / 2 + 'px'


					console.log(_this.left);
				}).exec()
			},


		}
	}
</script>
<style lang="less">
	.team-pact {
		position: fixed;
		top: 0;
		width: 100%;
		height: 78rpx;
		display: flex;
		justify-content: space-evenly;
		background-color: #fff;
		position: fixed;
		white-space: nowrap;
		z-index: 9999;
		left: 0;
		color: #444444;

		::-webkit-scrollbar {
			width: 0;
			height: 0;
			color: transparent;
		}

		.iconImg-box {
			display: inline-block;
			margin-left: 10rpx;
			width: 70rpx;

			.iconImg {
				width: 20rpx;
				height: 14rpx;
			}
		}



		.subscript {
			display: block;
			width: 40rpx;
			height: 8rpx;
			
			background: #5CDBD3;
			border-radius: 7rpx;
			position: absolute;
			bottom: 0rpx;
			transition: all .2s linear;

		}

		.subcolor {
			color: #5CDBD3;
		}

		.segmented-item {
			// width: 50%;



			.cont-box {
				/* margin-top: 20rpx; */
				height: 58rpx;
				margin: auto;
				padding: 20rpx 0 0 0;
				font-size: 32rpx;
				color: #111111;
				display: flex;
				justify-content: space-between;
				flex-direction: column;
			}

		}
	}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在uniapp中自定义微信小程序导航,你可以按照以下步骤进行操作: 1. 在 `App.vue` 中添加 `onLaunch` 方法,用于获取设备信息: ``` onLaunch: function() { uni.getSystemInfo({ success: function(res) { uni.$systemInfo = res; } }); } ``` 2. 在 `pages.json` 中添加 `navigationStyle` 属性,用于设置导航样式为自定义: ``` { "pages": [ { "path": "pages/home/home", "style": { "navigationStyle": "custom" } } ] } ``` 3. 在需要自定义导航的页面中,添加一个自定义导航组件: ``` <template> <view class="custom-navigation-bar"> <view class="custom-navigation-bar-left" @click="back"> <image src="/static/arrow_left.png" mode="widthFix" /> </view> <view class="custom-navigation-bar-title">{{title}}</view> </view> </template> <script> export default { props: { title: { type: String, default: '' } }, methods: { back() { uni.navigateBack({ delta: 1 }); } } }; </script> <style scoped> .custom-navigation-bar { position: fixed; top: 0; left: 0; width: 100%; height: uni.$systemInfo.statusBarHeight + 44px; background-color: #fff; display: flex; align-items: center; justify-content: space-between; padding: 0 16px; box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1); z-index: 999; } .custom-navigation-bar-left { width: 20px; height: 20px; display: flex; align-items: center; justify-content: center; } .custom-navigation-bar-title { font-size: 18px; font-weight: bold; color: #333; text-align: center; } </style> ``` 4. 在需要自定义导航的页面中,引入自定义导航组件,并将页面标题传入组件中: ``` <template> <view> <custom-navigation-bar title="自定义导航"></custom-navigation-bar> <view class="content">页面内容</view> </view> </template> <script> import CustomNavigationBar from '@/components/CustomNavigationBar'; export default { components: { CustomNavigationBar } }; </script> <style scoped> .content { margin-top: uni.$systemInfo.statusBarHeight + 44px; padding: 16px; } </style> ``` 这样,你就可以在uniapp微信小程序中自定义导航了。需要注意的是,在自定义导航组件中,需要通过 `uni.$systemInfo.statusBarHeight` 获取状态高度,并根据状态高度调整自定义导航的高度和位置。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值