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
This commit is contained in:
Andrew Nacin 2013-12-05 22:21:15 +00:00
parent 7ee0d2c59c
commit ddc9ed51e8

View File

@ -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,