vue获取滚动条滚动位置
记一次踩的坑,百度的很多方法都不能用
需求:上滑到全部、热门位置,全部、热门按钮固定,下拉到所在位置跟随下来
上图:
我给html,body给了高度100%,获取body的滚动条位置一直是0;
给当前页面给高度100%;
.beat_home {
height: 100%;
overflow: hidden;
overflow-y: scroll;
}
然后获取固定位置到上面的距离
<div :class="[scrollTop>posiHeight?'beat_home_top_1':'beat_home_top']">
<div class="beat_search_n">
<div @click="switchType('all')" :class="sortType=='all'?'switch_active':''">全部</div>
<div @click="switchType('hot')" :class="sortType=='hot'?'switch_active':''">热门</div>
</div>
this.posiHeight = this.$refs.partake_b.clientHeight(顶部图片的高度) + this.$refs.partake.clientHeight(描述的高度);
document.getElementById('beat_home').addEventListener('scroll', function () {
that.$nextTick(()=>{
that.scrollTop = that.$refs.beatHome.scrollTop;
})
}, true);
OK,问题解决