Lock and unlock page scroll with jQuery

Scroll the page before opening the demo, then check the restored position when you close it.

Try page scroll locking

Open the modal, scroll its content, then close it. The background page is locked while the modal is open and restored afterward.

Use Close and unlock inside the modal or press Escape to return to the article.

Loading. JavaScript, jQuery, and native dialog support are required.

Background scroll is locked

Try scrolling this panel while the background stays still. Close the dialog to restore page scrolling.

Try the inner scrollbar

Scroll this modal panel. The page underneath should remain in place while the dialog is open.

Background position

The demo remembers the page offset before applying the lock. It restores that position when you close the dialog.

Always provide an exit

The Close and unlock button stays above the scrollable content. Escape also closes this native modal dialog.

Keep content reachable

Long modal content belongs in a bounded scrolling region. It should remain reachable with a mouse, keyboard, or touch input.

Restore the original styles

Unlocking restores the inline properties changed by the demo rather than assigning a new overflow value to every page.

Coordinate multiple overlays

A production site with several modals should use one shared scroll-lock manager. This tutorial demonstrates one lock owner.

Test on your target devices

Check mobile Safari and Chrome, virtual keyboards, zoom, sticky headers, and other theme components in your final integration.

End of modal content

You reached the bottom. Scroll upward again or use Close and unlock to return to the article.

Close and unlock or press Escape. No data is collected by this demo.

Background page section 1

This extra content makes the document scrollable. Open the modal from the top, scroll inside its panel, and close with the button or Escape. The document should return to its original position.

The demo keeps its logic local, uses the bundled jQuery file, and changes only the properties it records for restoration.

Background page section 2

This extra content makes the document scrollable. Open the modal from the top, scroll inside its panel, and close with the button or Escape. The document should return to its original position.

The demo keeps its logic local, uses the bundled jQuery file, and changes only the properties it records for restoration.

Background page section 3

This extra content makes the document scrollable. Open the modal from the top, scroll inside its panel, and close with the button or Escape. The document should return to its original position.

The demo keeps its logic local, uses the bundled jQuery file, and changes only the properties it records for restoration.

Background page section 4

This extra content makes the document scrollable. Open the modal from the top, scroll inside its panel, and close with the button or Escape. The document should return to its original position.

The demo keeps its logic local, uses the bundled jQuery file, and changes only the properties it records for restoration.

Background page section 5

This extra content makes the document scrollable. Open the modal from the top, scroll inside its panel, and close with the button or Escape. The document should return to its original position.

The demo keeps its logic local, uses the bundled jQuery file, and changes only the properties it records for restoration.

Background page section 6

This extra content makes the document scrollable. Open the modal from the top, scroll inside its panel, and close with the button or Escape. The document should return to its original position.

The demo keeps its logic local, uses the bundled jQuery file, and changes only the properties it records for restoration.

Background page section 7

This extra content makes the document scrollable. Open the modal from the top, scroll inside its panel, and close with the button or Escape. The document should return to its original position.

The demo keeps its logic local, uses the bundled jQuery file, and changes only the properties it records for restoration.

Background page section 8

This extra content makes the document scrollable. Open the modal from the top, scroll inside its panel, and close with the button or Escape. The document should return to its original position.

The demo keeps its logic local, uses the bundled jQuery file, and changes only the properties it records for restoration.