While working with SharePoint 2013’s modal dialog there were a few cases where the dialog lost its scroll bar position and jumped back to the top whenever a resize ocurred. For those who may have stumbled here looking for the resize code it begins as follows:
var dlg = SP.UI.ModalDialog.get_childDialog(); dlg.autoSize();
If the user has scrolled down to activate a part of the form that may expand its always handy to try to get the dialog to expand as much as possible to fit on the users screen. as mentioned it will cause the scroll bar to to go to the very top, this can be frustrating from a UX perspective. In my solution I used jQuery to help out.
var dlg = SP.UI.ModalDialog.get_childDialog(); var origScroll = $('#s4-workspace', $(dlg.get_frameElement()).contents()).scrollTop(); dlg.autoSize(); $('#s4-workspace', $(dlg.get_frameElement()).contents()).scrollTop(origScroll);
The origScroll calls a jQuery selector that looks within the dialogs frame element’s contents for element id s4-workspace and grabs the current position of the scrollbar. Following the autosize it just calls the same element and sets the scrollTop to what it was before.