之前项目是 pc 端是使用 router 的方式实现置顶的
- //main.JS
- router.afterEach((to, from, next) => {
- Windows.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()
- },
用以上方法, 解决问题
来源: http://www.bubuko.com/infodetail-3304211.html