js+vue,前端关于页面滚动让头部菜单淡入淡出实现原理

今天遇到个需求:我这里借用小米商城的详情页做个比喻吧。

刚开始其商品详情页是这样的:

当滚动到一定高度时,是这样的:

可以看到当滚动到轮播图底下的时候,详情页的菜单完全显现出来。

以下上代码:

HTML:

html 复制代码
<div class="detail-header">
			<div class="left operate" :style="{'background-color':iconStyle.BackgroundColor,'color':iconStyle.color}">
				<span class="iconfont icon-fanhui"></span>
			</div>
			<div class="active-box" :style="{'opacity':Opacity}">
				<div>商品</div>
				<div>评价</div>
                <div>评价</div>
                <div>详情</div>
                <div>推荐</div>
			</div>
			<div class="right operate" :style="{'background-color':iconStyle.BackgroundColor,'color':iconStyle.color}">
				<span class="iconfont icon-home"></span>
			</div>
			<div class="header-bg" :style="{'opacity':Opacity}"></div>
		</div>

css:

css 复制代码
.detail-header{
			position: absolute;
			left: 0;
			right: 0;
			height: 1.173333rem;
			z-index: 999;
			.active-box{
				position: absolute;
				left: 50%;
				transform: translateX(-50%);
				white-space: nowrap;
				display: flex;
				align-items: center;
				justify-content: center;
				line-height: 1.173333rem;
				font-size: 0.426667rem;
				opacity:0;
				z-index:67;
				>div{
					padding: 0 0.4rem;
				}
			}
			.operate{
				position: absolute;
				width: 0.906667rem;
				height: 0.906667rem;
				line-height: 0.906667rem;
				border-radius: 50%;
				background-color: rgba(0,0,0,.3);
				color: #fff;
				text-align: center;
				margin: 0.133333rem;
				display: table;
				z-index: 58;
				.iconfont{
					display: table-cell;
					font-size: 0.4rem;
					vertical-align: middle;
				}
			}
			.left{
				left:0;
			}
			.right{
				right: 0;
			}
			.header-bg{
				background-color: #fff;
				height: 100%;
				opacity: 0;
				z-index: 66;
			}
		}

js:

javascript 复制代码
data(){
			return{
				Opacity:0,
				iconStyle:{
					BackgroundColor:'rgba(0, 0, 0, 0.3)',
					color:'#fff'
				}
			}
		},
ContentScroll(e){
				let top = (e.target.scrollTop / 120);
				this.Opacity = top >= 1?1:top;
				console.log(top)
				if(top >= 1){
					this.iconStyle.BackgroundColor = 'unset'
					this.iconStyle.color = '#333'
				}else{
					this.iconStyle.BackgroundColor = 'rgba(0, 0, 0, 0.3)'
					this.iconStyle.color = '#fff'
				}
			}

在上面代码中可以看出,头部代码是绝对定位的,头部不要设置背景图,因为我么是通过设置opacity来控制元素的显示隐藏的,所以我们用元素来代替背景的作用。只要设置好图层z-index就不会发生:操作菜单和按钮被遮挡的情况。记住要设置定位哦!

这里的e.target.scrollTop / 120 ;120是你轮播图的高度,根据自己的情况而定。

注意:这里主要是记录实现原理,js代码没那么完整,大家复制的时候注意。还有就是做的没小米商城那么丝滑,大家可以自行优化以下。

相关推荐
moshuying16 分钟前
块扩散 + GDN 改造:Clover 把「并行生成」塞进了 Agent 的长上下文里
前端·算法
kyriewen18 分钟前
我算了笔账:AI 编程工具一年烧掉两台 MacBook
前端·人工智能·程序员
moshuying1 小时前
扩散模型开始写代码、开网页了:350 亿参数,速度比 Jev 快一倍
前端·算法
qq_401700411 小时前
100 个 IoT 小项目,给 ESP32 入门补上实操
前端·网络·stm32
输出输入2 小时前
学习前端主流书单推荐
前端
anyup2 小时前
uView Pro X 1.0.0 发布:全面支持 uni-app x 蒸汽模式,80+ 组件
前端·uni-app·ai编程
apollo_qwe2 小时前
大模型 API 接通了,AI 聊天页面为什么还是不好用?
前端·vue.js·人工智能
爱勇宝2 小时前
你可能成不了技术大佬,然后呢?
前端·后端·程序员
疯华整猫3 小时前
从 MVP 到 MVI,从 Java 到 Kotlin:老项目升级的避坑指南
前端
徐小夕@趣谈前端3 小时前
Web 端 OFD 编辑器踩坑记:从“只能预览”到“在线改”
前端·编辑器