前端页面跳转到页面指定位置

//跳转到相应位置
      to() {
        if (this.$route.query.id != null) {
          var toEl = document.getElementById(this.$route.query.id);
          if (toEl != null) {
            if (toEl != null && toEl != "") {
              // toEl 为指定跳转到该位置的DOM节点
              let bridgeCms = toEl;
              let bodyTop = document.body;
              let heightCms = 0;
              // 计算该 DOM 节点到 bodyTop 顶部距离
              do {
                heightCms += bridgeCms.offsetTop;
                bridgeCms = bridgeCms.offsetParent;
              } while (bridgeCms !== bodyTop)
              // 滚动到指定位置
              window.scrollTo({
                top: heightCms,
                behavior: 'smooth'
              })
            }
          }
        }
      },

 

posted @ 2022-01-21 18:38  Ning-  阅读(696)  评论(0编辑  收藏  举报