Make WordPress Core

Ticket #37536: 37536.2.2.diff

File 37536.2.2.diff, 3.6 KB (added by DvanKooten, 9 years ago)

Fix non-stickiness for small sidebars.

  • wp-content/themes/twentyfifteen/js/functions.js

     
    66 */
    77
    88( function( $ ) {
    9         var $body, $window, $sidebar, adminbarOffset, top = false,
    10             bottom = false, windowWidth, windowHeight, lastWindowPos = 0,
    11             topOffset = 0, bodyHeight, sidebarHeight, resizeTimer,
    12             secondary, button;
     9        var $body, $window, $sidebar, resizeTimer,
     10                secondary, button;
    1311
    1412        function initMainNavigation( container ) {
    1513                // Add dropdown toggle that display child menu items.
     
    9593        }
    9694
    9795        // Sidebar scrolling.
    98         function resize() {
    99                 windowWidth = $window.width();
     96        function resizeAndScroll() {
     97                var windowPos = $window.scrollTop(),
     98                        windowHeight = $window.height(),
     99                        sidebarHeight = $sidebar.height(),
     100                        bodyHeight = $body.height();
    100101
    101                 if ( 955 > windowWidth ) {
    102                         top = bottom = false;
    103                         $sidebar.removeAttr( 'style' );
    104                 }
    105         }
     102                if( 955 < $window.width()
     103                        && bodyHeight > sidebarHeight
     104                        && ( windowPos + windowHeight ) >= sidebarHeight ) {
    106105
    107         function scroll() {
    108                 var windowPos = $window.scrollTop();
    109 
    110                 if ( 955 > windowWidth ) {
    111                         return;
     106                        $sidebar.css({
     107                                position: "fixed",
     108                                bottom: sidebarHeight > windowHeight ? 0 : 'auto'
     109                        });
     110                } else {
     111                        $sidebar.css('position', 'relative')
    112112                }
    113 
    114                 sidebarHeight = $sidebar.height();
    115                 windowHeight  = $window.height();
    116                 bodyHeight    = $body.height();
    117 
    118                 if ( sidebarHeight + adminbarOffset > windowHeight ) {
    119                         if ( windowPos > lastWindowPos ) {
    120                                 if ( top ) {
    121                                         top = false;
    122                                         topOffset = ( $sidebar.offset().top > 0 ) ? $sidebar.offset().top - adminbarOffset : 0;
    123                                         $sidebar.attr( 'style', 'top: ' + topOffset + 'px;' );
    124                                 } else if ( ! bottom && windowPos + windowHeight > sidebarHeight + $sidebar.offset().top && sidebarHeight + adminbarOffset < bodyHeight ) {
    125                                         bottom = true;
    126                                         $sidebar.attr( 'style', 'position: fixed; bottom: 0;' );
    127                                 }
    128                         } else if ( windowPos < lastWindowPos ) {
    129                                 if ( bottom ) {
    130                                         bottom = false;
    131                                         topOffset = ( $sidebar.offset().top > 0 ) ? $sidebar.offset().top - adminbarOffset : 0;
    132                                         $sidebar.attr( 'style', 'top: ' + topOffset + 'px;' );
    133                                 } else if ( ! top && windowPos + adminbarOffset < $sidebar.offset().top ) {
    134                                         top = true;
    135                                         $sidebar.attr( 'style', 'position: fixed;' );
    136                                 }
    137                         } else {
    138                                 top = bottom = false;
    139                                 topOffset = ( $sidebar.offset().top > 0 ) ? $sidebar.offset().top - adminbarOffset : 0;
    140                                 $sidebar.attr( 'style', 'top: ' + topOffset + 'px;' );
    141                         }
    142                 } else if ( ! top ) {
    143                         top = true;
    144                         $sidebar.attr( 'style', 'position: fixed;' );
    145                 }
    146 
    147                 lastWindowPos = windowPos;
    148113        }
    149114
    150         function resizeAndScroll() {
    151                 resize();
    152                 scroll();
    153         }
    154 
    155115        $( document ).ready( function() {
    156116                $body          = $( document.body );
    157117                $window        = $( window );
    158118                $sidebar       = $( '#sidebar' ).first();
    159                 adminbarOffset = $body.is( '.admin-bar' ) ? $( '#wpadminbar' ).height() : 0;
    160119
    161120                $window
    162                         .on( 'scroll.twentyfifteen', scroll )
     121                        .on( 'scroll.twentyfifteen', resizeAndScroll )
    163122                        .on( 'load.twentyfifteen', onResizeARIA )
    164123                        .on( 'resize.twentyfifteen', function() {
    165124                                clearTimeout( resizeTimer );
     
    168127                        } );
    169128                $sidebar.on( 'click.twentyfifteen keydown.twentyfifteen', 'button', resizeAndScroll );
    170129
    171                 resizeAndScroll();
    172 
    173                 for ( var i = 1; i < 6; i++ ) {
     130                for ( var i = 0; i < 6; i++ ) {
    174131                        setTimeout( resizeAndScroll, 100 * i );
    175132                }
    176133        } );