3

这是一个令人沮丧的问题。我在 jQuery 加载块内的脚本中使用以下内容:

window.scrollBy(0,-100);

我这样做是因为我通过滚动将 div 设置为固定在页面顶部,并且此行将进行补偿,以便您单击的锚点 (http://page.html#foo) 可以看到它应该出现的位置是。

它在 Firefox 中运行良好。在 Chrome 和 Safari 中,它不会,因为加载事件似乎发生在浏览器滚动到锚点之前。

有什么建议么?

4

2 回答 2

1

我遇到了同样的问题,这是我的工作。(实际上是一个黑客)

// Clicking on the navigation bar
$('.navbar-nav a').click(function(e) {
    // Blocking default clicking event
    e.preventDefault();
    // Scroll to the anchored element with a delay
    setTimeout(function() {$('#talks')[0].scrollIntoView();}, 5);
    // Compensate the scrolling with another delay
    setTimeout(function() {scrollBy(0, -$('#info').height())}, 10);
}

这似乎是一个 Safari 错误。

于 2013-08-09T15:44:51.023 回答
0

我也遇到过这个问题。使用超时,即使是 0 毫秒,似乎也可以在没有明显跳跃的情况下工作。尝试这个:

window.setTimeout()
{
    window.scrollBy(0, -100);
}, 0);
于 2017-11-05T13:41:35.913 回答