Last active
December 4, 2017 07:35
-
-
Save blackmiaool/0cca935eb68061edd0257f125f771290 to your computer and use it in GitHub Desktop.
preventBodyScrolling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.fn.snapshot = function (property) { | |
const snapshot = this.attr(property); | |
return () => { | |
this.attr(property, snapshot); | |
}; | |
}; | |
export function preventBodyScrolling($wrap) { | |
const $body = $("body"); | |
const cbArr = []; | |
cbArr.push($body.snapshot('style')); | |
cbArr.push($('html').snapshot('style')); | |
$wrap.css('pointer-events', 'all'); | |
const scrollTop = $body.scrollTop(); | |
$('body>*').each(function () { | |
const $dom = $(this); | |
if ($dom.css('position') !== 'fixed') { | |
cbArr.push($dom.snapshot('style')); | |
$dom.css('transform', `translateY(-${scrollTop}px)`); | |
} | |
}); | |
$wrap.on('touchmove', (e) => { | |
e.stopPropagation(); | |
}); | |
$('html,body').css('height', '100%').css('overflow', 'hidden'); | |
$body.css('pointer-events', 'none'); | |
return () => { | |
setTimeout(() => { | |
cbArr.forEach(cb => cb()); | |
$body.scrollTop(scrollTop); | |
}); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment