Make WordPress Core

Changeset 26695


Ignore:
Timestamp:
12/05/2013 10:21:15 PM (11 years ago)
Author:
nacin
Message:

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

props shaunandrews.
see #25821.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/js/widgets.js

    r26651 r26695  
    441441        widget.find( 'input.add_new' ).val('');
    442442
    443         $( 'html, body' ).animate({
    444             scrollTop: sidebar.offset().top - 130
    445         }, 200 );
     443        /*
     444         * Check if any part of the sidebar is visible in the viewport. If it is, don't scroll.
     445         * Otherwise, scroll up to so the sidebar is in view.
     446         *
     447         * We do this by comparing the top and bottom, of the sidebar so see if they are within
     448         * the bounds of the viewport.
     449         */
     450        var viewport_top = $(window).scrollTop(),
     451            viewport_bottom = viewport_top + $(window).height(),
     452            sidebar_bounds = sidebar.offset();
     453       
     454        sidebar_bounds.bottom = sidebar_bounds.top + sidebar.outerHeight();
     455
     456        if ( viewport_top > sidebar_bounds.bottom || viewport_bottom < sidebar_bounds.top ) {
     457            $( 'html, body' ).animate({
     458                scrollTop: sidebar.offset().top - 130
     459            }, 200 );
     460        }
    446461
    447462        window.setTimeout( function() {
Note: See TracChangeset for help on using the changeset viewer.