例如,从网站的首页点击跳转到指定页面的底部。

首页 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

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐