Vue 跨页面#锚链接跳转

home页面 -链接- list页面
第一步:在home页面用a标签设置锚链接
<!-- home.vue-->
<a href="/list#Scientists">跳去列表页 科学家</a>
<a href="/list#Innovators">跳去列表页 创新者</a>
第二步:在list页面设置id属性
<!-- list.vue-->
<p id="Scientists">
      列表页科学家内容
      ...
</p>
<p id="Scientists">
      列表页创新者内容
      ...
</p>
第三步:判断当前页面的 window.location.hash标签值),如果有就执行方法goAnchor()滚动到元素位置

mounted() {
	/** 判断当前页面的标签值,如果有就执行方法*/
    if (window.location.hash) {
      this.$nextTick(() => {
        this.goAnchor(window.location.hash);
      });
    }
  },
methods: {
	/** 页面滚动到元素位置 */
    goAnchor (selector) {
      let anchor = this.$el.querySelector(selector)
      document.body.scrollTop = anchor.offsetTop; // chrome
      document.documentElement.scrollTop = anchor.offsetTop; // firefox
    },
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值