RsD0p9BK
7/13/2016 - 6:01 AM

fixSidebar.js

// зафиксировать сайдбар
function fixSidebar ($sidebar, $content, maxHeight) {
    var $sidebar      = $sidebar,
        $content      = $content,
        contentHeight = $('html').height() - ($('html').height() - $content.outerHeight()),
        headerHight   = $content.offset().top,
        maxHeight     = maxHeight || true, // maxHeight of sidebar = window.height()
        offset        = $sidebar.offset(),
        scrollTop     = $(window).scrollTop(),
        sidebarHeight = $sidebar.outerHeight();

    if (maxHeight) {
        $sidebar.css({'maxHeight': $(window).height()});
    }

    if (scrollTop - headerHight < 0 || contentHeight - sidebarHeight < 0) {
        margin = 0;
    }

    else if (scrollTop + sidebarHeight > contentHeight) {
        margin = contentHeight - $sidebar.outerHeight();
    }

    else {
        margin = scrollTop - headerHight;
    }

    return $sidebar.stop().animate({
        marginTop: margin
    }, 500);
}

$(function() {
    $(window).scroll(function() {
        fixSidebar($("#tree_structur"), $('.menu_structur'), true);
    });
});

// http://stackoverflow.com/questions/18880159/use-jquery-to-check-mousewheel-event-without-scrollbar