之前項目是pc端是使用router的方式實現置頂的
//main.js
router.afterEach((to, from, next) => { window.scrollTo(0, 0) })
但是改了移動端就沒有效果了,稍微查了一下,好像說是要body里才有用。
可能與我使用了vux-ui有關
在深究router方式還是找新方法的選擇上,我選了后者,
//自定義的common.js
// 這個方法通過遞歸找到滾動的元素,用於置頂頁面 function getScrollParent (node) { if (node == null) { return null } if (node.scrollHeight > node.clientHeight) { return node } else { return getScrollParent(node.parentNode) } }
export {getScrollParent}
// 頁面文件,例如hello.vue
// 引入 import {isEmptyObj, getScrollParent} from '@/common/utils/common' //在mounted鈎子函數調用 mounted () { const element = getScrollParent(this.$el) element.scrollTop = 0 this.initCanvas() },
用以上方法,解決問題