Ticket #30366: 30366.2.diff
File 30366.2.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 topOffset = ( $sidebar.offset().top > 0 ) ? $sidebar.offset().top : 0; 75 $sidebar.attr( 'style', 'top: ' + topOffset + 'px;' ); 76 } else if ( ! bottom && windowPos + windowHeight > sidebarHeight + $sidebar.offset().top ) { 77 bottom = true; 78 $sidebar.attr( 'style', 'position: fixed;bottom: 0;' ); 79 } 80 } else if ( windowPos < lastWindowPos ) { 81 if ( bottom ) { 82 bottom = false; 83 topOffset = ( $sidebar.offset().top > 0 ) ? $sidebar.offset().top : 0; 84 $sidebar.attr( 'style', 'top: ' + topOffset + 'px;' ); 85 } else if ( ! top && windowPos + adminbarOffset < $sidebar.offset().top ) { 86 top = true; 87 $sidebar.attr( 'style', 'position: fixed;' ); 88 } 89 } else { 90 top = bottom = false; 91 topOffset = ( $sidebar.offset().top > 0 ) ? $sidebar.offset().top : 0; 92 $sidebar.attr( 'style', 'top: ' + topOffset + 'px;' ); 93 } 94 } else if ( ! top ) { 95 top = true; 96 $sidebar.attr( 'style', 'position: fixed;' ); 59 97 } 60 } else {61 $body.removeClass( 'sidebar-fixed' );62 98 } 99 100 lastWindowPos = windowPos; 63 101 } 64 102 65 function debouncedFixedOrScrolledSidebar() { 66 var timeout; 67 return function() { 68 clearTimeout( timeout ); 69 timeout = setTimeout( function() { 70 timeout = null; 71 fixedOrScrolledSidebar(); 72 }, 150 ); 73 }; 103 function resizeAndScroll() { 104 resize(); 105 scroll(); 74 106 } 75 107 76 77 108 $( 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 } 109 $body = $( 'body' ); 110 $window = $( window ); 111 $document = $( document ); 112 $sidebar = $( '#sidebar' ).first(); 113 adminbarOffset = $body.is( '.admin-bar' ) ? $( '#wpadminbar' ).height() : 0; 82 114 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; 115 $window 116 .on( 'scroll.twentyfifteen', scroll ) 117 .on( 'resize.twentyfifteen', function() { 118 clearTimeout( resizeTimer ); 119 resizeTimer = setTimeout( resizeAndScroll, 200 ); 120 } ); 88 121 89 $window 90 .on( 'load.twentyfifteen', fixedOrScrolledSidebar ) 91 .on( 'resize.twentyfifteen', debouncedFixedOrScrolledSidebar() ); 122 resizeAndScroll(); 123 124 for ( var i = 1; i < 6; i++ ) { 125 setTimeout( resizeAndScroll, 100 * i ); 126 } 92 127 } ); 93 128 94 129 } )( jQuery ); 130 No newline at end of file