xiaoxiaoxun

Vue跨页面锚点定位

App公共页面

 

 

 <dd @click="goto(\'/\',\'productModel\')">产品模型</dd>
goto(page, selectID) {
      console.log(this.$route.path, "this.$route.path");
      if (page == this.$route.path) {  //如果当前已经在这个页面不跳转 直接去
        let toElement = document.getElementById(selectID);
        toElement.scrollIntoView(true);
      } else {  //否则跳转路由
        localStorage.setItem("toId", selectID);
        this.$router.push({ path: page });
      }
    },

B页面

 created() {
    //创建时执行跳转锚点位置
    this.$nextTick(() => {
      this.getlocal();
    });
  },
 //从我本地找到id
    getlocal() {
      //找到锚点id
      let selectId = localStorage.getItem("toId");
      let toElement = document.getElementById(selectId);
      //如果对应id存在,就跳转
      if (selectId) {
        console.log(toElement,\'toElement\')
        toElement.scrollIntoView(true)
      }
    },
    //离开页面进行对localStorage  id销毁,避免其他入口进来有锚点问题
    destroyed() {
      localStorage.setItem("toId", "");
    },

 

发表于 2020-12-05 11:10  z_xun  阅读(547)  评论(0编辑  收藏  举报
 

分类:

技术点:

相关文章:

  • 2022-02-16
  • 2022-01-12
  • 2022-01-09
  • 2021-12-15
  • 2022-01-10
  • 2021-12-18
  • 2022-12-23
猜你喜欢
  • 2022-01-13
  • 2021-04-08
  • 2021-12-09
  • 2021-04-09
  • 2022-02-05
  • 2021-11-19
  • 2021-12-17
相关资源
相似解决方案