vue滚动固定顶部及修改样式的实例代码

2020-06-12 21:18:27易采站长站整理

滚动固定位置有多种方法

1 css3  粘性定位


position:sticky;
top:20px;

2直接position:fixed;给顶部盒子设置一个margin-top刚好是需要固定的盒子的高度

3事件监听更改style中的position属性

**
修改样式

滚动监听事件中使用

this.$refs.xxx.style.color=‘xxxx' 
这种方式会报错

Uncaught TypeError: Cannot read property 'style' of undefined

所以通过动态绑定来解决。

具体:

**
html绑定


<p class="ptop1" :style="pstyle">
<img @click="back()" :src='topimgs'>
</p>

data中设置属性


data() {
return {
pstyle:{background:'#595552'},
pastyle:{background:'#595552'},
pbstyle:{background:'#fff'},
};
},

mounted中添加事件监听

window.addEventListener(“scroll”, this.handleScroll);

methods中添加方法,在方法中修改


let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
if(scrollTop>100){
this.pstyle=this.pbstyle;
}else{
this.pstyle=this.pastyle;
}

总结

以上所述是小编给大家介绍的vue滚动固定顶部及修改样式的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对软件开发网网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!