0

I have written a simple custom section snapping script, works great in chrome and safari, but after nothing happens in firefox...

What it is does:

When scrolling stops it checks the direction and location of each secrion... if the top of a section is within a certain range either go to the top of the page or bottom. (scroll directions is also checked). Also, it accounts for the height of a fixed header. Like I said works in Chrome and Safari. Any ideas what's wrong?

$( document ).ready(function() {
    var animating = false;
    var mainHeader = $('#main-header');

    var items = $("section");
    var lastOffset = 0;
    var scrollDir = 'none';


    $(window).scroll(function() {
        var windowHeight = $(this).height();
        var currOffset = $(this).scrollTop();
        var headerHeight = mainHeader.outerHeight();

        if (currOffset > lastOffset) {
            scrollDir = 'down';
        } else {
            scrollDir = 'up';
        }
        lastOffset = currOffset;



        clearTimeout($.data(this, 'scrollTimer'));
        if (!animating) {
            $.data(this, 'scrollTimer', setTimeout(function() {
                items.each(function(key, value) {
                        var currentItem = $(value);
                        var sectionOffset = currentItem.offset().top;
                        var sectionDist = sectionOffset - currOffset;
                    if ( scrollDir === 'up' && sectionDist > windowHeight*0.15 && sectionDist < windowHeight ) {
                        animating = true;
                        $('body').animate( { scrollTop: sectionOffset-windowHeight + 'px' }, 250);
                        setTimeout(function() { animating = false; }, 300);
                        return false;
                    }
                    else if ( scrollDir === 'down' && sectionDist < windowHeight*0.85 && sectionDist > 0 ) {
                        animating = true;
                        $('body').animate( { scrollTop: sectionOffset-headerHeight + 'px' }, 250);
                        setTimeout(function() { animating = false; }, 300);
                        return false;
                    }
              });
            }, 200));
        }
    });
});
Andrew
  • 1
  • Just to let you know. This feature is available in [fullPage.js](http://alvarotrigo.com/fullPage/) when using the option `fitToSection` and `autoScrolling:false` as [in this example](http://alvarotrigo.com/fullPage/examples/normalScroll.html). – Alvaro May 21 '15 at 09:34
  • Thanks @Alvaro but I did actually want the auto scrolling to happen at certain times. – Andrew Jul 15 '15 at 23:39

1 Answers1

0

Found the answer here... Animate scrollTop not working in firefox

Firefox places the overflow at the html level, unless specifically styled to behave differently.

To get it to work in Firefox, use

$('body,html').animate( ... );
Community
  • 1
  • 1
Andrew
  • 1