From ddc9ed51e887f75d0de2570a9d96d16e8ee304d9 Mon Sep 17 00:00:00 2001 From: Andrew Nacin Date: Thu, 5 Dec 2013 22:21:15 +0000 Subject: [PATCH] Widgets: Only scroll to the newly clicked-and-added widget if it is out of the viewport. props shaunandrews. see #25821. git-svn-id: https://develop.svn.wordpress.org/trunk@26695 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/js/widgets.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) 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,