Skip to content

Instantly share code, notes, and snippets.

@hyeonseok
Last active August 2, 2018 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hyeonseok/242f198688cc7494713c to your computer and use it in GitHub Desktop.
Save hyeonseok/242f198688cc7494713c to your computer and use it in GitHub Desktop.
Swap window scroll from main content to modal window, and vise versa.
// Needs to be clean up.
var mainContent = document.getElementById('main');
var open = function (target) {
var closeButton = target.querySelector('p.button button');
mainContent.style.top = -1 * document.body.scrollTop + 'px';
mainContent.style.position = 'fixed';
target.setAttribute('aria-hidden', 'false');
document.body.scrollTop = 0;
closeButton.addEventListener('click', close);
};
var close = function (event) {
var element = event.currentTarget;
var target = element.parentNode.parentNode;
var scrollTop = parseInt(mainContent.style.top.replace('px', '')) * -1;
mainContent.style.position = 'static';
mainContent.style.top = 0;
target.setAttribute('aria-hidden', 'true');
document.body.scrollTop = scrollTop;
element.removeEventListener('click', close);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment