scrollTop not working in Safari

2019-09-17 03:26发布

问题:

Firs time this is happening to me, i'm aware that scrollTop have some issues with some browsers but this time is only in Safari, Firefox and Chrome are ok. Here is my code

$(window).scroll(function()
{
    var s = $('html, body').scrollTop();
    console.log(s) //ok on FF and Chrome, but Safari returns 0
}

回答1:

Sadly you must check for both the <html> and <body> element separately.

$(window).scroll(function()
{
    var s = $('html').scrollTop() || $('body').scrollTop();
    console.log(s);
});