Make WordPress Core

Ticket #37536: 37536.2.diff

File 37536.2.diff, 3.4 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' );
     102                if( 955 < $window.width()
     103                        && sidebarHeight > windowHeight
     104                        && bodyHeight > sidebarHeight
     105                        && ( windowPos + windowHeight ) >= sidebarHeight ) {
     106                        $sidebar.css({
     107                                position: "fixed",
     108                                bottom: 0
     109                        });
     110                } else {
     111                        $sidebar.css({
     112                                position: "relative"
     113                        })
    104114                }
    105115        }
    106116
    107         function scroll() {
    108                 var windowPos = $window.scrollTop();
    109 
    110                 if ( 955 > windowWidth ) {
    111                         return;
    112                 }
    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;
    148         }
    149 
    150         function resizeAndScroll() {
    151                 resize();
    152                 scroll();
    153         }
    154 
    155117        $( document ).ready( function() {
    156118                $body          = $( document.body );
    157119                $window        = $( window );
    158120                $sidebar       = $( '#sidebar' ).first();
    159                 adminbarOffset = $body.is( '.admin-bar' ) ? $( '#wpadminbar' ).height() : 0;
    160121
    161122                $window
    162123                        .on( 'scroll.twentyfifteen', scroll )
     
    168129                        } );
    169130                $sidebar.on( 'click.twentyfifteen keydown.twentyfifteen', 'button', resizeAndScroll );
    170131
    171                 resizeAndScroll();
    172 
    173                 for ( var i = 1; i < 6; i++ ) {
     132                for ( var i = 0; i < 6; i++ ) {
    174133                        setTimeout( resizeAndScroll, 100 * i );
    175134                }
    176135        } );