版权声明:本文为博主原创文章,未经博主允许不可转载。转载请注明出处 https://blog.csdn.net/qq_32442967/article/details/82355360
//锚点向下偏移
点击a链接后,含有锚点的div显示位置,距离浏览器顶部的间距
$('a[href^=#][href!=#]').click(function () {
var target = document.getElementById(this.hash.slice(1));
if (!target) return;
var targetOffset = $(target).offset().top - 120; //120为离浏览器顶部的距离
$('html,body').animate({scrollTop: targetOffset}, 400);
return false;
});