index.vue
<view class="box" :class="temp==1?'boxStyle':''"></view>
index.css
<style> .boxStyle{ position: fixed;top: 0;left: 0; } </style>
index.js
export default {
data() {
return {
temp:0,
myScroll:0,
}
},
onLoad(){
//獲取目標板塊到頂部的距離
uni.createSelectorQuery().select('.box').boundingClientRect(res=>{
this.myScroll =res.top;
}).exec();
},
onPageScroll(e){
if(e.scrollTop > this.myScroll){
this.temp= 1
}else{
this.temp= 0
}
}
}