jQuery - 刷新页面后不起作用

jQuery - After refreshing the page doesn't work

目前,我正在学习 javascript 和 jquery 的写作。但是,我写了一个简单的 jquery 代码,当您输入 page/website 时,它会自动滚动到特定的 div。当我加载 page/website 并自动滚动到我想要的 div 时,脚本工作正常。当我刷新浏览器时,问题就来了。刷新页面后,脚本不想滚动到我在代码中指向的 div。如果有人能帮助我,我将不胜感激并提前致谢。

在 Chrome 和 Opera 上,脚本即使在刷新后也能正常工作。但是,我不能对 Firefox、IE 和 Edge 说同样的话。

$(document).ready(function() {
    $(".Left_Container").animate({
       scrollTop: $(".Home_Left").offset().top
    }, 0);
});

PS:我正在使用 "Left_Container" 而不是 "HTML,body" 因为我只想滚动那个特定的 div.

此致,

乔治·S.

有些浏览器会在页面重新加载后保存您的滚动位置,因此这不是您的代码问题。

您可以做的就是强制浏览器立即滚动到顶部,然后像这样为您的 div 设置动画:

$(document).ready(function() {
    $(".Left_Container").scrollTop(0); // go to the top instantly (no animation)
    $(".Left_Container").animate({ // then animate down
       scrollTop: $(".Home_Left").offset().top
    }, 0);
});