再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>