我想让浏览器将页面滚动到给定的锚点,只需使用JavaScript即可。
我在HTML代码中指定了name或id属性:
<a name="anchorName">..</a>或
<h1 id="anchorName2">..</h1>我希望获得与您导航到http://server.com/path#anchorName相同的效果。应该滚动页面,使锚点位于页面可见部分的顶部附近。
发布于 2019-02-13 22:41:36
Vue.js 2解决方案...添加一个简单的数据属性来简单地强制更新:
const app = new Vue({
...
, updated: function() {
this.$nextTick(function() {
var uri = window.location.href
var anchor = ( uri.indexOf('#') === -1 ) ? '' : uri.split('#')[1]
if ( String(anchor).length > 0 && this.updater === 'page_load' ) {
this.updater = "" // only on page-load !
location.href = "#"+String(anchor)
}
})
}
});
app.updater = "page_load"
/* Smooth scrolling in CSS - it works in HTML5 only */
html, body {
scroll-behavior: smooth;
}https://stackoverflow.com/questions/3163615
复制相似问题