components_affix.js 2.67 KB
Newer Older
Luis Gangas committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
/* ------------------------------------------------------------------------------
*
*  # Affix and scrollspy
*
*  Specific JS code additions for components_affix.html page
*
*  Version: 1.1
*  Latest update: Feb 25, 2016
*
* ---------------------------------------------------------------------------- */

$(function() {


	// Resize sidebar on scroll
    // ------------------------------

    // Resize fixed sidebar vertically when bottom reached
    function resizeFixed() {
        $(window).on('load scroll', function() {
          if ($(window).scrollTop() > $(document).height() - $(window).height() - 70) {
            $('.sidebar-fixed').addClass('fixed-sidebar-space');
          }
          else {
            $('.sidebar-fixed').removeClass('fixed-sidebar-space');
          }
        });
    }


    // Nice scroll
    // ------------------------------

	// Setup
	function initScroll() {
	    $(".sidebar-fixed .sidebar-content").niceScroll({
	        mousescrollstep: 100,
	        cursorcolor: '#ccc',
	        cursorborder: '',
	        cursorwidth: 3,
	        hidecursordelay: 100,
	        autohidemode: 'scroll',
	        horizrailenabled: false,
	        preservenativescrolling: false,
	        railpadding: {
	        	right: 0.5,
	        	top: 1.5,
	        	bottom: 1.5
	        }
	    });
	}

	// Resize
	function resizeScroll() {
		$('.sidebar-fixed .sidebar-content').getNiceScroll().resize();
	}

	// Remove
	function removeScroll() {
		$(".sidebar-fixed .sidebar-content").getNiceScroll().remove();
		$(".sidebar-fixed .sidebar-content").removeAttr('style').removeAttr('tabindex');
	}


    // Affix detached sidebar
    // ------------------------------

    // Init nicescroll when sidebar affixed
	$('.sidebar-fixed').on('affix.bs.affix', function() {
		initScroll();
		resizeFixed();
	});

    // When effixed top, remove scrollbar and its data
    $('.sidebar-fixed').on('affix-top.bs.affix', function() {
		removeScroll();

        $(".sidebar-fixed .sidebar-content").removeAttr('style').removeAttr('tabindex');
    });

	// Attach BS affix component to the sidebar
	$('.sidebar-fixed').affix({
		offset: {
			top: $('.sidebar-fixed').offset().top - 20 // top offset - computed line height
		}
	});


    // Remove affix and scrollbar on mobile
    $(window).on('resize', function() {
        setTimeout(function() {            
            if($(window).width() <= 768) {

                // Remove nicescroll on mobiles
                removeScroll();

                // Remove affix on mobile
                $(window).off('.affix')
                $('.sidebar-fixed').removeData('affix').removeClass('affix affix-top affix-bottom');
            }
        }, 100);
    }).resize();

});