function toggleScrollLock(open) { var offset = window.scrollY; var body = document.querySelector('body'); if (open) { // Fix the body when menu is open body.classList.add('fixed'); body.style.top = `-${offset}px`; } else { // Scroll back to original position upon close const scrollY = document.body.style.top; body.classList.remove('fixed'); body.style.top = ''; window.scrollTo(0, parseInt(scrollY || '0') * -1); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter