diff --git a/src/wp-admin/js/widgets.js b/src/wp-admin/js/widgets.js index 1fcf9301d7..8b8607ef6d 100644 --- a/src/wp-admin/js/widgets.js +++ b/src/wp-admin/js/widgets.js @@ -440,9 +440,24 @@ wpWidgets = { // No longer "new" widget widget.find( 'input.add_new' ).val(''); - $( 'html, body' ).animate({ - scrollTop: sidebar.offset().top - 130 - }, 200 ); + /* + * Check if any part of the sidebar is visible in the viewport. If it is, don't scroll. + * Otherwise, scroll up to so the sidebar is in view. + * + * We do this by comparing the top and bottom, of the sidebar so see if they are within + * the bounds of the viewport. + */ + var viewport_top = $(window).scrollTop(), + viewport_bottom = viewport_top + $(window).height(), + sidebar_bounds = sidebar.offset(); + + sidebar_bounds.bottom = sidebar_bounds.top + sidebar.outerHeight(); + + if ( viewport_top > sidebar_bounds.bottom || viewport_bottom < sidebar_bounds.top ) { + $( 'html, body' ).animate({ + scrollTop: sidebar.offset().top - 130 + }, 200 ); + } window.setTimeout( function() { // Cannot use a callback in the animation above as it fires twice,