滚动固定位置有多种方法
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滚动固定顶部及修改样式的实例代码,希望对大家有所帮助
如对本文有疑问, 点击进行留言回复!!
轻松解决 org.apache.taglibs.standard.tlv.JstlCoreTLV 困惑
vert实践五——Json?Protocol Buffer?FlatBuffers?
[基于tensorflow的人脸检测] 基于神经网络的人脸检测8——验证训练好的神经网络
selenium + ajax抓取英雄联盟全部英雄的详细信息及多线程保存全部皮肤图片到本地
网友评论