前端实现吸顶 uniapp实现吸顶 置顶的效果

uni-App置顶吸顶

这里要注意不能嵌套 看图
在这里插入图片描述

.questionbag{
	  position: -webkit-sticky;
	  position: sticky;
	  top: var(--window-top);
	  z-index: 99;
  }

web同样可以实现这种效果

top: var(–window-top);其中–window-top这个方法是uni提供的css变量,在web页面可以直接写px,比如写80px他会在距离上边距80px的时候悬浮,可以尝试下

.questionbag{
	  position: -webkit-sticky;
	  position: sticky;
	  top: 80rpx;
	  z-index: 99;
  }

最后的样子
在这里插入图片描述
在这里插入图片描述

uniapp实现吸顶效果可以通过以下步骤: 1. 在需要实现吸顶效果的组件或页面中,添加一个固定位置的容器,比如`<view>`。 2. 给该容器设置一个固定的高度,并设置其`position`属性为`fixed`,使其固定在页面顶部。 3. 监听页面的滚动事件,在页面滚动到一定位置时,将容器的`position`属性改为`relative`,使其跟随页面滚动。 下面是一个简单的示例代码: ```html <template> <view> <!-- 固定在页面顶部的容器 --> <view class="sticky-container" :style="{height: containerHeight + 'px', position: sticky ? 'fixed' : 'relative'}"> <!-- 容器中的内容 --> <view class="content">我是吸顶的内容</view> </view> <!-- 页面内容 --> <view class="page-content"></view> </view> </template> <script> export default { data() { return { sticky: false, // 是否启用吸顶效果 containerHeight: 50 // 容器的高度 }; }, mounted() { // 监听页面滚动事件 uni.pageScrollTo({ scrollTop: 0, // 滚动到页面顶部 duration: 0 // 立即滚动,不需要动画效果 }); uni.onPageScroll((e) => { // 当页面滚动到一定位置时启用吸顶效果 if (e.scrollTop >= 100) { this.sticky = true; } else { this.sticky = false; } }); } }; </script> <style> .sticky-container { background-color: #f2f2f2; z-index: 100; } .content { text-align: center; line-height: 50px; } .page-content { height: 1000px; } </style> ``` 在上述示例代码中,我们通过监听`uni.onPageScroll`事件来实现页面滚动的监控,并通过改变`sticky`数据来改变容器的`position`属性。当页面滚动到一定位置时,容器会固定在页面顶部,实现吸顶效果
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值