当前位置: 移动技术网 > IT编程>开发语言>JavaScript > vue滚动固定顶部及修改样式的实例代码

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

2019年07月19日  | 移动技术网IT编程  | 我要评论

滚动固定位置有多种方法

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滚动固定顶部及修改样式的实例代码,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网