UNIAPP----导航栏滑动实现淡隐淡出效果

再uniapp里面,仿写淘宝的详情页滑动淡隐淡出效果。

核心思想:通过onPageScroll方法拿到滑动距离,动态修改opacity属性,rgba不可用,区别见我之前博客------这里是opacity和rgba的区别

代码完整,复制直接用,略微臃肿,因为我截取项目里面的,有一张小图片你们是加载不出来的,可以删除。

没有组件,纯css和js动态修改。不是最简单的方法,注释已打,仅供学习参考。

HTML:

<template>
	<view>
			<view class="baikuang">
				
			</view>
			<view class="head" @tap="toTop" :style="bgcolor">
				<view class="back">
					<view class="left"><image src="/static/back_green.svg" mode=""></image></view>
				</view>
				<table></table>
				<view class="title">
					<view class="" :style="color1">
						<text @click="sp" :style="fw1" >商品</text>
					</view>
					<view class="" :style="color2">
						<text @click="xq" :style="fw2">详情</text>
					</view>
					<view class="" :style="color3">
						<text @click="tj" :style="fw3">推荐</text>
					</view>
				</view>
			</view>
	
		<view class="" style="margin-top: 88rpx;">
			
			<view class="" style="width: 200rpx;height: 6vh;background-color: #55ffff;">
				<text style="color: fff;">↑↑↑↑↑↑↑↑↑↑↑↑↑</text>
			</view>
			<view class="" style="width: 200rpx;height: 6vh;background-color: #aaff7f;">
				<text style="color: fff;">↑↑↑↑↑↑↑↑↑↑↑↑↑</text>
			</view>
			<view class="" style="width: 200rpx;height: 6vh;background-color: #55ffff;">
				<text style="color: fff;">↑↑↑↑↑↑↑↑↑↑↑↑↑</text>
			</view>
			<view class="" style="width: 200rpx;height: 6vh;background-color: #aaff7f;">
				<text style="color: fff;">↑↑↑↑↑↑↑↑↑↑↑↑↑</text>
			</view>
			<view class="" style="width: 200rpx;height: 6vh;background-color: #55ffff;">
				<text style="color: fff;">↑↑↑↑↑↑↑↑↑↑↑↑↑</text>
			</view>
			<view class="" style="width: 200rpx;height: 6vh;background-color: #aaff7f;">
				<text style="color: fff;">↑↑↑↑↑↑↑↑↑↑↑↑↑</text>
			</view>
			<view class="" style="width: 200rpx;height: 6vh;background-color: #55ffff;">
				<text style="color: fff;">↑↑↑↑↑↑↑↑↑↑↑↑↑</text>
			</view>
			<view class="" style="width: 200rpx;height: 6vh;background-color: #aaff7f;">
				<text style="color: fff;">↑↑↑↑↑↑↑↑↑↑↑↑↑</text>
			</view>
			<view class="" style="width: 200rpx;height: 6vh;background-color: #55ffff;">
				<text style="color: fff;">↑↑↑↑↑↑↑↑↑↑↑↑↑</text>
			</view>
			<view class="" style="width: 200rpx;height: 6vh;background-color: #aaff7f;">
				<text style="color: fff;">↑↑↑↑↑↑↑↑↑↑↑↑↑</text>
			</view>
			<view class="" style="width: 200rpx;height: 6vh;background-color: #55ffff;">
				<text style="color: fff;">↑↑↑↑↑↑↑↑↑↑↑↑↑</text>
			</view>
			<view class="" style="width: 200rpx;height: 6vh;background-color: #aaff7f;">
				<text style="color: fff;">↑↑↑↑↑↑↑↑↑↑↑↑↑</text>
			</view>
			<view class="" style="width: 200rpx;height: 6vh;background-color: #55ffff;">
				<text style="color: fff;">↑↑↑↑↑↑↑↑↑↑↑↑↑</text>
			</view>
			<view class="" style="width: 200rpx;height: 6vh;background-color: #aaff7f;">
				<text style="color: fff;">↑↑↑↑↑↑↑↑↑↑↑↑↑</text>
			</view>
			<view class="" style="width: 200rpx;height: 6vh;background-color: #55ffff;">
				<text style="color: fff;">↑↑↑↑↑↑↑↑↑↑↑↑↑</text>
			</view>
			<view class="" style="width: 200rpx;height: 6vh;background-color: #aaff7f;">
				<text style="color: fff;">↑↑↑↑↑↑↑↑↑↑↑↑↑</text>
			</view>
			<view class="" style="width: 200rpx;height: 6vh;background-color: #55ffff;">
				<text style="color: fff;">↑↑↑↑↑↑↑↑↑↑↑↑↑</text>
			</view>
			<view class="" style="width: 200rpx;height: 6vh;background-color: #aaff7f;">
				<text style="color: fff;">↑↑↑↑↑↑↑↑↑↑↑↑↑</text>
			</view>
			<view class="" style="width: 200rpx;height: 6vh;background-color: #55ffff;">
				<text style="color: fff;">↑↑↑↑↑↑↑↑↑↑↑↑↑</text>
			</view>
			<view class="" style="width: 200rpx;height: 6vh;background-color: #aaff7f;">
				<text style="color: fff;">↑↑↑↑↑↑↑↑↑↑↑↑↑</text>
			</view>
			<view class="" style="width: 200rpx;height: 6vh;background-color: #55ffff;">
				<text style="color: fff;">↑↑↑↑↑↑↑↑↑↑↑↑↑</text>
			</view>
			<view class="" style="width: 200rpx;height: 6vh;background-color: #aaff7f;">
				<text style="color: fff;">↑↑↑↑↑↑↑↑↑↑↑↑↑</text>
			</view>
			<view class="" style="width: 200rpx;height: 6vh;background-color: #55ffff;">
				<text style="color: fff;">↑↑↑↑↑↑↑↑↑↑↑↑↑</text>
			</view>
			<view class="" style="width: 200rpx;height: 6vh;background-color: #aaff7f;">
				<text style="color: fff;">↑↑↑↑↑↑↑↑↑↑↑↑↑</text>
			</view>
			<view class="" style="width: 200rpx;height: 6vh;background-color: #55ffff;">
				<text style="color: fff;">↑↑↑↑↑↑↑↑↑↑↑↑↑</text>
			</view>
			<view class="" style="width: 200rpx;height: 6vh;background-color: #aaff7f;">
				<text style="color: fff;">↑↑↑↑↑↑↑↑↑↑↑↑↑</text>
			</view>
			
		</view>
	</view>
</template>

JS:

<script>
export default {
	data() {
		return {
			fw1:'font-weight:400',
			fw2:'font-weight:400',
			fw3:'font-weight:400',
			color1:'color:#323232',
			color2:'color:#323232',
			color3:'color:#323232',
			bgcolor:''
			// flag: false,
		};
	},
	methods: {
		sp(){
			this.fw1="font-weight:bold"
			this.fw2="font-weight:400"
			this.fw3="font-weight:400"
			this.color1="color:#00ce88"
			this.color2="color:#323232"
			this.color3="color:#323232"
		},
		xq(){
			this.fw1="font-weight:400"
			this.fw2="font-weight:bold"
			this.fw3="font-weight:400"
			this.color1="color:#323232"
			this.color2="color:#00ce88"
			this.color3="color:#323232"
		},
		tj(){
			this.fw1="font-weight:400"
			this.fw2="font-weight:400"
			this.fw3="font-weight:bold"
			this.color1="color:#323232"
			this.color2="color:#323232"
			this.color3="color:#00ce88"
		},
		// 通过这个方法,监听滑动的距离,通过距离,动态修改透明的
		onPageScroll(e) { 
		    if (e.scrollTop >= 0 && e.scrollTop<=8) { 
		        // this.flag = true
				this.bgcolor="opacity:0"
		    } else if(e.scrollTop > 8 && e.scrollTop<=16){
		        // this.flag = false
				this.bgcolor="opacity:0.1"
		    }else if(e.scrollTop >16 && e.scrollTop<=24){
				this.bgcolor="opacity:0.2"
			}else if(e.scrollTop >24 && e.scrollTop<=32){
				this.bgcolor="opacity:0.3"
			}else if(e.scrollTop >32 && e.scrollTop<=40){
				this.bgcolor="opacity:0.4"
			}else if(e.scrollTop >40 && e.scrollTop<=48){
				this.bgcolor="opacity:0.5"
			}else if(e.scrollTop >48 && e.scrollTop<=56){
				this.bgcolor="opacity:0.6"
			}else if(e.scrollTop >56 && e.scrollTop<=64){
				this.bgcolor="opacity:0.7"
			}else if(e.scrollTop >64 && e.scrollTop<=72){
				this.bgcolor="opacity:0.8"
			}else if(e.scrollTop >72 && e.scrollTop<=80){
				this.bgcolor="opacity:0.9"
			}else if(e.scrollTop >80 ){
				this.bgcolor="opacity:1"
			}
		}
	},
	// 页面加载,默认opacity:0,意为透明不显示
	onLoad() {
		this.fw1="font-weight:bold"
		this.fw2="font-weight:400"
		this.fw3="font-weight:400"
		this.color1="color:#00ce88"
		this.color2="color:#323232"
		this.color3="color:#323232"
		this.bgcolor="opacity:0"
	}
};
</script>

CSS:

<style scoped>
	.baikuang{
	position: fixed;
	height: 88rpx;
	width: 100%;
	top: 0;
	background-color: #fff;
	}
.head {
	width: 100%;
	height: 88rpx;
	display: flex;
	justify-content: space-around;
	background-color: #fff;
	position: fixed;
	top: 88rpx;
	background-color: #fff;
}
.left {
	height: 50rpx;
	width: 48rpx;
	position: absolute;
	bottom: 20rpx;
	left: 30rpx;
}
.left image {
	height: 100%;
	width: 100%;
}
.title {
	width: 40%;
	height: 40rpx;
	font-size: 28rpx;
	font-weight: bold;
	font-family: 黑体;
	margin: 0 auto;
	color: #323232;
	display: flex;
	justify-content: space-between;
	font-family: 黑体;
	margin-top: 28rpx;
}
</style>

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值