滚动到页面上的 ID 未正确计算偏移量

Scroll to ID on page not calculating offset correctly

本文关键字:计算 偏移量 ID 滚动      更新时间:2023-09-26
$('.home-slide-button').on('click', function (e) {
var matches = window.location.hash.match(/^#([0-9]+)$/);
if (matches) {
    var number = matches[1];
    $('html, body').animate({
        scrollTop: $('#' + number).offset().top -150
    }, 1500);
}
});

当单击"主页"页面(http://buchbinderei.it/)中的产品链接时,它应重定向到"产品"页面(http://buchbinderei.it/produkte/),并顺利向下滚动到单击的产品。一些它如何不计算前 -150 偏移量......有什么建议吗?

实际错误的偏移位置:https://www.dropbox.com/s/m3cbinuyl4d3pgv/Screenshot%202016-01-24%2022.47.55.png?dl=0

所需偏移位置:https://www.dropbox.com/s/y0lhldjvult9kdg/Screenshot%202016-01-24%2022.48.49.png?dl=0

这解决了我的问题:

if (location.hash) {
    setTimeout(function() {
        var matches = window.location.hash.match(/^#([0-9]+)$/);
        if (matches) {
            var number = matches[1];
            $('html, body').animate({
                scrollTop: $('#' + number).offset().top -180
            }, 500);
            console.log('enter');
        }
    }, 1);
}