Fork me on GitHub

vue中操作DOM

方案1

可以在mounted中挂载

1
2
3
4
5
mounted:function(){
this.$nextTick(function(){ //this.$nextTick是在下次DOM更新循环结束时调用延迟回调函数。异步函数
this.loadData();          //DOM加载就绪,后调用loadData方法进行数据更新<br>        //想要更新后的获取dom              //此时若获取更新后dom数据将会报错,数据为undefined;
})
}

方案2

1
2
3
4
5
6
7
8
9
10
11
12
if(document.addEventListener){
document.addEventListener('DOMMouseScroll',()=>{
},false);
}//W3C
window.onmousewheel=document.onmousewheel=()=>{
let top = document.getElementById("bottoms").scrollTop;
console.log(top);
this.$refs.add.style.marginTop = top + 10 + "px";
// document.getElementById("rights").style.marginTop = top+"px";
console.log(this.$refs.add.style);

};//IE/Opera/Chrome
显示 Gitment 评论