使用js獲取元素高度時弄诲,一直顯示為0:
let myPage = document.getElementsById('my-page');
console.log(this.myPage.clientHeight);
這是因為此時的元素還沒有得到渲染,可以將獲取高度的代碼放在$nextTick中:
this.$nextTick(()=>{
let myPage = document.getElementsById('my-page');
console.log(this.myPage.clientHeight);
});
$nextTick()介紹:
Vue.nextTick( [callback, context] )
-
參數(shù):
{Function} [callback]
{Object} [context]
-
用法:
在下次 DOM 更新循環(huán)結束之后執(zhí)行延遲回調妓柜。在修改數(shù)據之后立即使用這個方法,獲取更新后的 DOM。