第一种:
可以透過 vue-router 裡面的 scrollBehavior 實現(前提是你是使用 History 模式)
具体可以参考vue-router官网
也可以借鉴参考github dome
第二种:
如果沒辦法使用 History 模式 也沒關係,可以用其他方式:
html 代码
<div>
<div><a href="javascript:void(0)" @click="goAnchor('#anchor-'+index)" v-for="index in 20"> {{index}} </a></div>
<div :id="'anchor-'+index" class="item" v-for="index in 20">{{index}}</div>
</div>
html 代码
methods: {
goAnchor(selector) {
var anchor = this.$el.querySelector(selector)
document.body.scrollTop = anchor.offsetTop
}
}