javascript - 内容在第二次单击固定标题上的相同锚链接时更改其位置
问题描述
我正在开发一个带有固定标题的单页网站。我面临的问题是,当我单击导航栏链接时,它会完全转到内容,但是当我再次单击同一链接时。它稍微改变了内容的位置。
JS: var windows = $(window); var 粘性 = $('#sticker');
windows.on('scroll', function() {
var scroll = windows.scrollTop();
if (scroll < 400) {
sticky.removeClass('stick');
}else{
sticky.addClass('stick');
}
});
$('a.pagess:not(:first)').click(function(e){
e.preventDefault();
var y = 125;
$('html ,body').animate({enter code here
scrollTop: $(this.hash).offset().top - y
});
});
解决方案
没关系,我知道了,实际上我的计算有点错误。所以我稍微更改了代码,现在一切正常。
$('a.pagess:not(:first)').click(function(e){
e.preventDefault();
var y = 125;
if (windows.scrollTop() > 400 ){
y = 55;
}
$('html,body').animate({
scrollTop: $(this.hash).offset().top - y
});
window.history.pushState(null, null, this.href);
});
推荐阅读
- sql - 特定用户购买的所有产品
- r - 合并相同的列组合
- localhost - Firefox 通过 UNC 访问存储在本地计算机上的文件
- php - CodeIgniter 路由问题以访问前端和后端文件夹
- authentication - Redux Navigation with Redux ,一旦通过身份验证导航到主屏幕
- ruby-on-rails - 如何从选择标签栏搜索数据
- windows - aws s3 sync --recursive 在 Windows 中不起作用
- keycloak - Keycloak: Admin-cli Add SMTP server details?
- java - 在java中从对象转换为字符串
- python - Pythonic方式以并行方式迭代字典内的列表作为字典