Window.scrollBy fires before scrolling a hash / anchor in Chrome and Safari

Here is the problem. I use the following in a script inside a jQuery download block:

window.scrollBy(0,-100);

I do this because I set the div to be fixed at the top of the page by scrolling, and this line will compensate so that the anchor you clicked on (http: //page.html#foo) is displayed where it should to be.

It works great on Firefox. In Chrome and Safari, this does not happen, because the download event appears before the browser scrolls to the anchor.

Any suggestions?

+3
source share
2 answers

I ran into the same problem and this is my job. (Actually hacked)

// 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);
}

This seems to be a Safari bug.

+1
source

. -, 0 , , . :

window.setTimeout()
{
    window.scrollBy(0, -100);
}, 0);
0