0

I have a function which detects the section of a single-page site the user is currently on and, depending on the variable passed to it, scrolls to the next section or the previous. This is working fine in Safari and Chrome. However, in Firefox, regardless of where the scroll position is, it treats the browser as if it's at scroll 0.

function scrollToSection(dir) {
    if( !$("body").hasClass("scrolling") ) {
        $("body").addClass("scrolling");

        var headerH = $(".page-header").outerHeight();
        var scrollTop = $("body").scrollTop();

        $("section").each(function() {
            var sectionH = $(this).outerHeight(true);
            var sectionOffset = $(this).offset();
            var sectionOffsetTop = sectionOffset.top - headerH;
            if( (sectionOffsetTop <= scrollTop) && (sectionOffsetTop + sectionH >= scrollTop) ) {
                var prevSection = $(this).prev();
                var nextSection = $(this).next();
                if( dir === "up" ) {
                    $.scrollTo( prevSection, {
                            duration : 1000,
                            offset : {
                                top : -50
                            },
                            onAfter: function() {
                                $("body").removeClass("scrolling");
                            }

                        });
                } else {
                    $.scrollTo( nextSection, {
                            duration : 1000,
                            offset : {
                                top : -50
                            },
                            onAfter: function() {
                                $("body").removeClass("scrolling");
                            }
                        });
                }
                return false;
            }
        });
    }
}
Fisu
  • 3,294
  • 9
  • 39
  • 61

3 Answers3

3

FireFox needs the scrolltop set and read from the html element. Other browsers store this on the body element.

do:

var scrollTop = $("html,body").scrollTop();
Tom
  • 582
  • 3
  • 13
1

Instead of:

$("body").scrollTop();

Try using:

$("body,html").scrollTop();
A. Wolff
  • 74,033
  • 9
  • 94
  • 155
0

Try to change $('body') in $('body,html') . It should work

Darkito
  • 96
  • 7