VUE实现从一个页面跳转到另一个页面的指定位置
例如,从网站的首页点击跳转到指定页面的底部。首页 home<div @click="toPath('/targetPage#target')"><p>点击跳转</p></div>methods:{//点击跳转方法toPath(path) {this.$router.push({path: path})}}跳转到的页面targetPage<div
·
例如,从网站的首页点击跳转到指定页面的底部。
首页 home
<div @click="toPath('/targetPage#target')">
<p>点击跳转</p>
</div>
methods:{
//点击跳转方法
toPath(path) {
this.$router.push({path: path})
}
}
跳转到的页面 targetPage
<div class="location" id="target">
<p>指定位置</p>
</div>
//在mounted里
mounted() {
var hash = window.location.hash;
var index = hash.lastIndexOf("#");
if (index != -1) {
var id = hash.substring(index + 1, hash.length + 1);
var div = document.getElementById(id);
if (div) {
setTimeout(function () {
console.log($(div).offset().top);
$('html, body').animate({scrollTop: $(div).offset().top - 43}, 500)
}, 500);
}
}
}
亲测有效 :D
更多推荐
已为社区贡献2条内容
所有评论(0)