Widgets: Only scroll to the newly clicked-and-added widget if it is out of the viewport.

props shaunandrews.
see #25821.

Built from https://develop.svn.wordpress.org/trunk@26695


git-svn-id: http://core.svn.wordpress.org/trunk@26585 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Nacin 2013-12-05 22:22:11 +00:00
parent 63c82dd11d
commit b8882b3511
2 changed files with 19 additions and 4 deletions

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,

File diff suppressed because one or more lines are too long