| 175 | |
| 176 | $( window ).scroll( function () { |
| 177 | if ( wpWidgets.sidebarTimeout ) clearTimeout( wpWidgets.sidebarTimeout ); |
| 178 | |
| 179 | wpWidgets.sidebarTimeout = setTimeout( function () { |
| 180 | // Don't try and be helpful when the user is dragging something. |
| 181 | if ( $( '.ui-draggable-dragging' ).length > 0 ) return; |
| 182 | |
| 183 | var sidebar = $( '#widgets-right' ); |
| 184 | var relativeScroll = $( window ).scrollTop() - ( sidebar.parent().offset().top - $( '#wpadminbar' ).height() ); |
| 185 | |
| 186 | if ( relativeScroll > 0 ) { |
| 187 | var currentMarginTop = parseInt( sidebar.css( 'marginTop' ), 10 ); |
| 188 | |
| 189 | if ( relativeScroll < currentMarginTop ) { |
| 190 | sidebar.stop().animate( { marginTop : relativeScroll } ); |
| 191 | } |
| 192 | else { |
| 193 | var windowHeight = $( window ).height(); |
| 194 | var sidebarHeight = sidebar.height(); |
| 195 | |
| 196 | if ( windowHeight > sidebarHeight ) { |
| 197 | sidebar.stop().animate( { marginTop : relativeScroll } ); |
| 198 | } |
| 199 | else if ( windowHeight + relativeScroll - sidebarHeight + currentMarginTop > 0 ) { |
| 200 | sidebar.stop().animate( { marginTop : windowHeight + relativeScroll - sidebarHeight } ); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | else { |
| 205 | sidebar.stop().animate( { marginTop : 0 } ); |
| 206 | } |
| 207 | }, 250 ); |
| 208 | } ); |