我在我的Nuxt应用程序中使用了一个<nuxt-link>组件,我遇到了以下问题:
当我第一次在链接上单击
我如何覆盖此行为,以便当我单击nuxt-link时,它会滚动到所需的部分,而不管我以前单击了什么?(不刷新页面)
以下是我的nuxt-link的样子
<nuxt-link :to="{ path: localePath('/'), hash: '#homepage' }"
>
<span>Homepage</span>
</nuxt-link>发布于 2020-09-08 20:26:25
让它与@Merc建议的绑定到click.native事件的函数一起工作。
handleScroll(anchorId: string): void {
if (this.$route.hash) {
const anchor = document.querySelector(`#${anchorId}`)
// Check if the anchor has been found
if (anchor) {
window.scrollTo({
// Scroll so that the anchor is at the top of the view
top: anchor.getBoundingClientRect().top + window.pageYOffset
})
}
}
}发布于 2020-09-06 18:41:30
我相信您应该能够在nuxt-链接中添加一个单击处理程序。
<nuxt-link
:to="{ path: localePath('/'), hash: '#homepage' }"
@click.native="scroll"
>
Homepage
</nuxt-link>(不确定@click.native,如果不起作用,只需尝试@click)
然后在你的方法中:
methods: {
scroll() {
// your scroll function, probably reading document.documentElement.scrollTop or similar
// if necessary check this.$route if the hash is already present, if so only do it in that case...
},
}希望这能给你点开场白?!干杯
https://stackoverflow.com/questions/63732673
复制相似问题