之前项目是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()
  },

用以上方法,解决问题

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-17
  • 2021-08-05
  • 2022-01-15
  • 2021-05-30
  • 2021-11-06
猜你喜欢
  • 2022-12-23
  • 2018-11-12
  • 2021-06-14
  • 2022-12-23
  • 2022-12-23
  • 2021-08-03
  • 2022-12-23
相关资源
相似解决方案