From 0f3c162db6aea170e79cdba32af74981e7784e7b Mon Sep 17 00:00:00 2001 From: Mark Jaquith Date: Tue, 4 Dec 2012 17:58:24 +0000 Subject: [PATCH] Fix a Firefox "scroll to bottom" bug when launching the media modal. * Records main document scroll position when launching media modal. * Restores position when media modal is closed. * Also locks background document scrolling while media modal is open, preventing inadvertent scrolling there. props koopersmith. fixes #22716 git-svn-id: https://develop.svn.wordpress.org/trunk@23029 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-includes/js/media-views.js | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/wp-includes/js/media-views.js b/wp-includes/js/media-views.js index 2ba5a45305..175bd5b4f7 100644 --- a/wp-includes/js/media-views.js +++ b/wp-includes/js/media-views.js @@ -1866,7 +1866,8 @@ _.defaults( this.options, { container: document.body, title: '', - propagate: true + propagate: true, + freeze: document.body }); }, @@ -1902,23 +1903,47 @@ }, open: function() { - if ( this.$el.is(':visible') ) + var $el = this.$el, + options = this.options, + $freeze; + + if ( $el.is(':visible') ) return this; if ( ! this.views.attached ) this.attach(); - this.$el.show().focus(); + // If the `freeze` option is set, record the window's scroll + // position and the body's overflow, and then set overflow to hidden. + if ( options.freeze ) { + $freeze = $( options.freeze ); + this._freeze = { + overflow: $freeze.css('overflow'), + scrollTop: $( window ).scrollTop() + }; + $freeze.css( 'overflow', 'hidden' ); + } + + $el.show().focus(); return this.propagate('open'); }, close: function( options ) { + var freeze = this._freeze; + if ( ! this.views.attached || ! this.$el.is(':visible') ) return this; this.$el.hide(); this.propagate('close'); + // If the `freeze` option is set, restore the container's scroll + // position and overflow property. + if ( freeze ) { + $( this.options.freeze ).css( 'overflow', freeze.overflow ); + $( window ).scrollTop( freeze.scrollTop ); + } + if ( options && options.escape ) this.propagate('escape');