function handleScrolling() {
    // Get current page scroll
    var $scroller      = getScroller(),
        scrollTop      = $scroller.scrollTop(),
        scrollHeight   = $scroller.prop('scrollHeight'),
        clientHeight   = $scroller.prop('clientHeight'),
        nbChapters     = $chapters.length,
        $chapter       = null;

    // Find each title position in reverse order
    $($chapters.get().reverse()).each(function(index) {
        var titleId = getChapterHash($(this)),
            titleTop;

        if (!!titleId && !$chapter) {
            titleTop = getElementTopPosition(titleId);

            // Set current chapter as active if scroller passed it
            if (scrollTop >= titleTop) {
                $chapter = $(this);
            }
        }
        // If no active chapter when reaching first chapter, set it as active
        if (index == (nbChapters - 1) && !$chapter) {
            $chapter = $(this);
        }
    });

    // ScrollTop is at 0, set first chapter anyway
    if (!$chapter && !scrollTop) {
        $chapter = $chapters.first();
    }

    // Set last chapter as active if scrolled to bottom of page
    if (!!scrollTop && (scrollHeight - scrollTop == clientHeight)) {
        $chapter = $chapters.last();
    }

    setChapterActive($chapter);
}
function handleScrolling() {
    
    var $scroller      = getScroller(),
        scrollTop      = $scroller.scrollTop(),
        scrollHeight   = $scroller.prop('scrollHeight'),
        clientHeight   = $scroller.prop('clientHeight'),
        nbChapters     = $chapters.length,
        $chapter       = null;

    
    $($chapters.get().reverse()).each(function(index) {
        var titleId = getChapterHash($(this)),
            titleTop;

        if (!!titleId && !$chapter) {
            titleTop = getElementTopPosition(titleId);

            
            if (scrollTop >= titleTop) {
                $chapter = $(this);
            }
        }
        
        if (index == (nbChapters - 1) && !$chapter) {
            $chapter = $(this);
        }
    });

    
    if (!$chapter && !scrollTop) {
        $chapter = $chapters.first();
    }

    
    if (!!scrollTop && (scrollHeight - scrollTop == clientHeight)) {
        $chapter = $chapters.last();
    }

    setChapterActive($chapter);
}
function handleScrolling ( ) { // Get current page scroll var $scroller = getScroller ( ) , scrollTop = $scroller . scrollTop ( ) , scrollHeight = $scroller . prop ( 'scrollHeight' ) , clientHeight = $scroller . prop ( 'clientHeight' ) , nbChapters = $chapters . length , $chapter = null ; // Find each title position in reverse order $ ( $chapters . get ( ) . reverse ( ) ) . each ( function ( index ) { var titleId = getChapterHash ( $ ( this ) ) , titleTop ; if ( ! ! titleId && ! $chapter ) { titleTop = getElementTopPosition ( titleId ) ; // Set current chapter as active if scroller passed it if ( scrollTop >= titleTop ) { $chapter = $ ( this ) ; } } // If no active chapter when reaching first chapter, set it as active if ( index == ( nbChapters - 1 ) && ! $chapter ) { $chapter = $ ( this ) ; } } ) ; // ScrollTop is at 0, set first chapter anyway if ( ! $chapter && ! scrollTop ) { $chapter = $chapters . first ( ) ; } // Set last chapter as active if scrolled to bottom of page if ( ! ! scrollTop && ( scrollHeight - scrollTop == clientHeight ) ) { $chapter = $chapters . last ( ) ; } setChapterActive ( $chapter ) ; }
Handle user scrolling
Handle user scrolling