| 50 | | } )( jQuery ); |
| 51 | | No newline at end of file |
| | 52 | |
| | 53 | // Sidebar scrolling. |
| | 54 | |
| | 55 | $( function() { |
| | 56 | var $window = $( window ), |
| | 57 | lastWindowPos = 0, |
| | 58 | $document = $( document ), |
| | 59 | $adminbar = $( '#wpadminbar' ), |
| | 60 | $sidebar = $( '#sidebar' ), |
| | 61 | top = false, |
| | 62 | bottom = false, |
| | 63 | windowWidth, windowHeight, documentHeight, |
| | 64 | adminBarHeight, sidebarWidth, sidebarHeight, |
| | 65 | resizeTimer; |
| | 66 | |
| | 67 | function resize() { |
| | 68 | windowWidth = $window.width(); |
| | 69 | windowHeight = $window.height(); |
| | 70 | documentHeight = $document.height(); |
| | 71 | adminBarHeight = $adminbar.height(); |
| | 72 | sidebarWidth = $sidebar.width(); |
| | 73 | sidebarHeight = $sidebar.height(); |
| | 74 | |
| | 75 | if ( sidebarWidth === windowWidth ) { |
| | 76 | top = bottom = false; |
| | 77 | $sidebar.removeAttr( 'style' ); |
| | 78 | } |
| | 79 | } |
| | 80 | |
| | 81 | function scroll() { |
| | 82 | var windowPos = $window.scrollTop(); |
| | 83 | |
| | 84 | if ( sidebarWidth < windowWidth && sidebarHeight + adminBarHeight < documentHeight ) { |
| | 85 | if ( windowPos > lastWindowPos ) { |
| | 86 | if ( top ) { |
| | 87 | top = false; |
| | 88 | $sidebar.attr( 'style', 'top: ' + $sidebar.offset().top + 'px;' ); |
| | 89 | } else if ( ! bottom && windowPos + windowHeight > sidebarHeight + $sidebar.offset().top ) { |
| | 90 | bottom = true; |
| | 91 | $sidebar.attr( 'style', 'position: fixed;bottom: 0;' ); |
| | 92 | } |
| | 93 | } else if ( windowPos < lastWindowPos ) { |
| | 94 | if ( bottom ) { |
| | 95 | bottom = false; |
| | 96 | $sidebar.attr( 'style', 'top: ' + $sidebar.offset().top + 'px;' ); |
| | 97 | } else if ( ! top && windowPos + adminBarHeight < $sidebar.offset().top ) { |
| | 98 | top = true; |
| | 99 | $sidebar.attr( 'style', 'position: fixed;' ); |
| | 100 | } |
| | 101 | } else { |
| | 102 | top = bottom = false; |
| | 103 | $sidebar.attr( 'style', 'top: ' + $sidebar.offset().top + 'px;' ); |
| | 104 | } |
| | 105 | } |
| | 106 | |
| | 107 | lastWindowPos = windowPos; |
| | 108 | } |
| | 109 | |
| | 110 | function resizeAndScroll() { |
| | 111 | resize(); |
| | 112 | scroll(); |
| | 113 | } |
| | 114 | |
| | 115 | $window.on( 'scroll.pin-sidebar', scroll ); |
| | 116 | |
| | 117 | $window.resize( function() { |
| | 118 | clearTimeout( resizeTimer ); |
| | 119 | |
| | 120 | resizeTimer = setTimeout( resizeAndScroll, 200 ); |
| | 121 | } ); |
| | 122 | |
| | 123 | resizeAndScroll(); |
| | 124 | |
| | 125 | for ( var i = 1; i < 6; i++ ) { |
| | 126 | setTimeout( resizeAndScroll, 500 * i ); |
| | 127 | } |
| | 128 | } ); |
| | 129 | } )( jQuery ); |