<template>
<view class="content">
<view class="wrap">
<view class="list" v-for="i in list_data">
<text>{
{
i }}</text>
</view>
</view>
<view class="switchSign"></view>
<view class="tagTop" :class="{'topfixed-active':topfixed==1}">固定在顶部</view>
<view class="wrap" :class="{'paTop80':topfixed==1}">
<view class="list" v-for="i in list_data_new">
<text>{
{
i }}</text>
</view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
list_data: [0, 1, 2, 3, 4],
list_data_new: [0, 1, 2, 3, 4, 5, 6, 7, 8],
topfixed: 0,
}
},
onPageScroll(res) {
var _this = this
var temptop;
//uni.createSelectorQuery()返回一个 SelectorQuery 对象实例。
//可以在这个实例上使用 select 等方法选择节点,
const query = uni.createSelectorQuery();
//select在当前页面下选择第一个匹配选择器的节点,
//boundingClientRect添加节点的布局位置的查询请求。其功能类似于 DOM 的 getBoundingClientRect。
query.select('.switchSign').boundingClientRect();
//selectViewport选择显示区域,可用于获取显示区域的尺寸、滚动位置等信息
//scrollOffset添加节点的滚动位置查询请求。
query.selectViewport().scrollOffset();
//exec执行所有的请求。请求结果按请求次序构成数组,在callback的第一个参数中返回。
query.exec(function(res) {
console.log(res);
res[0].top // .switchSign节点距离上边界的坐标
res[1].scrollTop // 显示区域的竖直滚动位置
temptop = res[0].top;
if (temptop <= '2') {
_this.topfixed = 1;
} else {
_this.topfixed = 0;
}
})
},
}
</script>
<style>
.content {
width: 100%;
margin: 0 auto;
}
.wrap .list {
width: 700upx;
height: 200upx;
line-height: 200upx;
margin: 0 auto;
text-align: center;
border-bottom: 2upx solid #EEEEEE;
}
.wrap .list text {
font-size: 36upx;
font-weight: bold;
color: #333333;
}
.topfixed-active {
width: 100%;
padding: 0 25upx;
position: fixed;
top: var(--window-top);
left: 0;
background: #fff;
z-index: 9;
box-sizing: border-box;
}
.tagTop {
height: 80upx;
line-height: 80upx;
background-color: #EEEEEE;
text-align: center;
}
.paTop80 {
padding-top: 80upx;
}
</style>
转载
原文链接:https://blog.csdn.net/qq_59795720/article/details/125003839?spm=1001.2014.3001.5501