Ticket #30366: 30366.diff
File 30366.diff, 4.5 KB (added by , 10 years ago) |
---|
-
src/wp-content/themes/twentyfifteen/js/functions.js
6 6 */ 7 7 8 8 ( function( $ ) { 9 var $body, $window, sidebar, toolbarOffset; 9 var $body, $window, $document, $sidebar, adminbarOffset, top = false, 10 bottom = false, windowWidth, windowHeight, lastWindowPos = 0, 11 topOffset = 0, documentHeight, sidebarWidth, sidebarHeight, resizeTimer; 10 12 11 13 // Add dropdown toggle that display child menu items. 12 14 $( '.main-navigation .page_item_has_children > a, .main-navigation .menu-item-has-children > a' ).after( '<button class="dropdown-toggle" aria-expanded="false">' + screenReaderText.expand + '</button>' ); … … 32 34 return; 33 35 } 34 36 35 // Hide button if there is no widgets and menu ismissing or empty.37 // Hide button if there are no widgets and the menus are missing or empty. 36 38 menu = secondary.find( '.nav-menu' ); 37 39 widgets = secondary.find( '#widget-area' ); 38 40 social = secondary.find( '#social-navigation' ); … … 48 50 } ); 49 51 } )(); 50 52 53 // Sidebar scrolling. 54 function resize() { 55 windowWidth = $window.width(); 56 windowHeight = $window.height(); 57 documentHeight = $document.height(); 58 sidebarHeight = $sidebar.height(); 51 59 52 // Sidebar (un)fixing: fix when short, un-fix when scroll needed 53 function fixedOrScrolledSidebar() { 54 if ( $window.width() >= 955 ) { 55 if ( sidebar.scrollHeight < ( $window.height() - toolbarOffset ) ) { 56 $body.addClass( 'sidebar-fixed' ); 57 } else { 58 $body.removeClass( 'sidebar-fixed' ); 60 if ( 955 >= windowWidth ) { 61 top = bottom = false; 62 $sidebar.removeAttr( 'style' ); 63 } 64 } 65 66 function scroll() { 67 var windowPos = $window.scrollTop(); 68 69 if ( 955 <= windowWidth && sidebarHeight + adminbarOffset < documentHeight ) { 70 if ( sidebarHeight + adminbarOffset > windowHeight ) { 71 if ( windowPos > lastWindowPos ) { 72 if ( top ) { 73 top = false; 74 $sidebar.attr( 'style', 'top: ' + $sidebar.offset().top + 'px;' ); 75 } else if ( ! bottom && windowPos + windowHeight > sidebarHeight + $sidebar.offset().top ) { 76 bottom = true; 77 $sidebar.attr( 'style', 'position: fixed;bottom: 0;' ); 78 } 79 } else if ( windowPos < lastWindowPos ) { 80 if ( bottom ) { 81 bottom = false; 82 topOffset = ( $sidebar.offset().top > 0 ) ? $sidebar.offset().top : 0; 83 $sidebar.attr( 'style', 'top: ' + topOffset + 'px;' ); 84 } else if ( ! top && windowPos + adminbarOffset < $sidebar.offset().top ) { 85 top = true; 86 $sidebar.attr( 'style', 'position: fixed;' ); 87 } 88 } else { 89 top = bottom = false; 90 topOffset = ( $sidebar.offset().top > 0 ) ? $sidebar.offset().top : 0; 91 $sidebar.attr( 'style', 'top: ' + topOffset + 'px;' ); 92 } 93 } else if ( ! top ) { 94 top = true; 95 $sidebar.attr( 'style', 'position: fixed;' ); 59 96 } 60 } else {61 $body.removeClass( 'sidebar-fixed' );62 97 } 98 99 lastWindowPos = windowPos; 63 100 } 64 101 65 function debouncedFixedOrScrolledSidebar() { 66 var timeout; 67 return function() { 68 clearTimeout( timeout ); 69 timeout = setTimeout( function() { 70 timeout = null; 71 fixedOrScrolledSidebar(); 72 }, 150 ); 73 }; 102 function resizeAndScroll() { 103 resize(); 104 scroll(); 74 105 } 75 106 76 77 107 $( document ).ready( function() { 78 // But! We only want to allow fixed sidebars when there are no submenus. 79 if ( $( '#site-navigation .sub-menu' ).length ) { 80 return; 81 } 108 $body = $( 'body' ); 109 $window = $( window ); 110 $document = $( document ); 111 $sidebar = $( '#sidebar' ).first(); 112 adminbarOffset = $body.is( '.admin-bar' ) ? $( '#wpadminbar' ).height() : 0; 82 113 83 // only initialize 'em if we need 'em 84 $body = $( 'body' ); 85 $window = $( window ); 86 sidebar = $( '#sidebar' )[0]; 87 toolbarOffset = $body.is( '.admin-bar' ) ? $( '#wpadminbar' ).height() : 0; 114 $window 115 .on( 'scroll.twentyfifteen', scroll ) 116 .on( 'resize.twentyfifteen', function() { 117 clearTimeout( resizeTimer ); 118 resizeTimer = setTimeout( resizeAndScroll, 200 ); 119 } ); 88 120 89 $window 90 .on( 'load.twentyfifteen', fixedOrScrolledSidebar ) 91 .on( 'resize.twentyfifteen', debouncedFixedOrScrolledSidebar() ); 121 resizeAndScroll(); 122 123 for ( var i = 1; i < 6; i++ ) { 124 setTimeout( resizeAndScroll, 500 * i ); 125 } 92 126 } ); 93 127 94 128 } )( jQuery ); 129 No newline at end of file